isl_output.c: reuse print_disjunct for printing in Omega format
[isl.git] / isl_tab_pip.c
blob23fe6c6d8c957afb60bd2fe9d0536496f80f5fc8
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)
568 goto error;
569 isl_assert(bset->ctx, sol->empty, goto error);
571 sol->empty = isl_set_grow(sol->empty, 1);
572 bset = isl_basic_set_simplify(bset);
573 bset = isl_basic_set_finalize(bset);
574 sol->empty = isl_set_add_basic_set(sol->empty, isl_basic_set_copy(bset));
575 if (!sol->empty)
576 goto error;
577 isl_basic_set_free(bset);
578 return;
579 error:
580 isl_basic_set_free(bset);
581 sol->sol.error = 1;
584 static void sol_map_add_empty_wrap(struct isl_sol *sol,
585 struct isl_basic_set *bset)
587 sol_map_add_empty((struct isl_sol_map *)sol, bset);
590 /* Given a basic map "dom" that represents the context and an affine
591 * matrix "M" that maps the dimensions of the context to the
592 * output variables, construct a basic map with the same parameters
593 * and divs as the context, the dimensions of the context as input
594 * dimensions and a number of output dimensions that is equal to
595 * the number of output dimensions in the input map.
597 * The constraints and divs of the context are simply copied
598 * from "dom". For each row
599 * x = c + e(y)
600 * an equality
601 * c + e(y) - d x = 0
602 * is added, with d the common denominator of M.
604 static void sol_map_add(struct isl_sol_map *sol,
605 struct isl_basic_set *dom, struct isl_mat *M)
607 int i;
608 struct isl_basic_map *bmap = NULL;
609 unsigned n_eq;
610 unsigned n_ineq;
611 unsigned nparam;
612 unsigned total;
613 unsigned n_div;
614 unsigned n_out;
616 if (sol->sol.error || !dom || !M)
617 goto error;
619 n_out = sol->sol.n_out;
620 n_eq = dom->n_eq + n_out;
621 n_ineq = dom->n_ineq;
622 n_div = dom->n_div;
623 nparam = isl_basic_set_total_dim(dom) - n_div;
624 total = isl_map_dim(sol->map, isl_dim_all);
625 bmap = isl_basic_map_alloc_space(isl_map_get_space(sol->map),
626 n_div, n_eq, 2 * n_div + n_ineq);
627 if (!bmap)
628 goto error;
629 if (sol->sol.rational)
630 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
631 for (i = 0; i < dom->n_div; ++i) {
632 int k = isl_basic_map_alloc_div(bmap);
633 if (k < 0)
634 goto error;
635 isl_seq_cpy(bmap->div[k], dom->div[i], 1 + 1 + nparam);
636 isl_seq_clr(bmap->div[k] + 1 + 1 + nparam, total - nparam);
637 isl_seq_cpy(bmap->div[k] + 1 + 1 + total,
638 dom->div[i] + 1 + 1 + nparam, i);
640 for (i = 0; i < dom->n_eq; ++i) {
641 int k = isl_basic_map_alloc_equality(bmap);
642 if (k < 0)
643 goto error;
644 isl_seq_cpy(bmap->eq[k], dom->eq[i], 1 + nparam);
645 isl_seq_clr(bmap->eq[k] + 1 + nparam, total - nparam);
646 isl_seq_cpy(bmap->eq[k] + 1 + total,
647 dom->eq[i] + 1 + nparam, n_div);
649 for (i = 0; i < dom->n_ineq; ++i) {
650 int k = isl_basic_map_alloc_inequality(bmap);
651 if (k < 0)
652 goto error;
653 isl_seq_cpy(bmap->ineq[k], dom->ineq[i], 1 + nparam);
654 isl_seq_clr(bmap->ineq[k] + 1 + nparam, total - nparam);
655 isl_seq_cpy(bmap->ineq[k] + 1 + total,
656 dom->ineq[i] + 1 + nparam, n_div);
658 for (i = 0; i < M->n_row - 1; ++i) {
659 int k = isl_basic_map_alloc_equality(bmap);
660 if (k < 0)
661 goto error;
662 isl_seq_cpy(bmap->eq[k], M->row[1 + i], 1 + nparam);
663 isl_seq_clr(bmap->eq[k] + 1 + nparam, n_out);
664 isl_int_neg(bmap->eq[k][1 + nparam + i], M->row[0][0]);
665 isl_seq_cpy(bmap->eq[k] + 1 + nparam + n_out,
666 M->row[1 + i] + 1 + nparam, n_div);
668 bmap = isl_basic_map_simplify(bmap);
669 bmap = isl_basic_map_finalize(bmap);
670 sol->map = isl_map_grow(sol->map, 1);
671 sol->map = isl_map_add_basic_map(sol->map, bmap);
672 isl_basic_set_free(dom);
673 isl_mat_free(M);
674 if (!sol->map)
675 sol->sol.error = 1;
676 return;
677 error:
678 isl_basic_set_free(dom);
679 isl_mat_free(M);
680 isl_basic_map_free(bmap);
681 sol->sol.error = 1;
684 static void sol_map_add_wrap(struct isl_sol *sol,
685 struct isl_basic_set *dom, struct isl_mat *M)
687 sol_map_add((struct isl_sol_map *)sol, dom, M);
691 /* Store the "parametric constant" of row "row" of tableau "tab" in "line",
692 * i.e., the constant term and the coefficients of all variables that
693 * appear in the context tableau.
694 * Note that the coefficient of the big parameter M is NOT copied.
695 * The context tableau may not have a big parameter and even when it
696 * does, it is a different big parameter.
698 static void get_row_parameter_line(struct isl_tab *tab, int row, isl_int *line)
700 int i;
701 unsigned off = 2 + tab->M;
703 isl_int_set(line[0], tab->mat->row[row][1]);
704 for (i = 0; i < tab->n_param; ++i) {
705 if (tab->var[i].is_row)
706 isl_int_set_si(line[1 + i], 0);
707 else {
708 int col = tab->var[i].index;
709 isl_int_set(line[1 + i], tab->mat->row[row][off + col]);
712 for (i = 0; i < tab->n_div; ++i) {
713 if (tab->var[tab->n_var - tab->n_div + i].is_row)
714 isl_int_set_si(line[1 + tab->n_param + i], 0);
715 else {
716 int col = tab->var[tab->n_var - tab->n_div + i].index;
717 isl_int_set(line[1 + tab->n_param + i],
718 tab->mat->row[row][off + col]);
723 /* Check if rows "row1" and "row2" have identical "parametric constants",
724 * as explained above.
725 * In this case, we also insist that the coefficients of the big parameter
726 * be the same as the values of the constants will only be the same
727 * if these coefficients are also the same.
729 static int identical_parameter_line(struct isl_tab *tab, int row1, int row2)
731 int i;
732 unsigned off = 2 + tab->M;
734 if (isl_int_ne(tab->mat->row[row1][1], tab->mat->row[row2][1]))
735 return 0;
737 if (tab->M && isl_int_ne(tab->mat->row[row1][2],
738 tab->mat->row[row2][2]))
739 return 0;
741 for (i = 0; i < tab->n_param + tab->n_div; ++i) {
742 int pos = i < tab->n_param ? i :
743 tab->n_var - tab->n_div + i - tab->n_param;
744 int col;
746 if (tab->var[pos].is_row)
747 continue;
748 col = tab->var[pos].index;
749 if (isl_int_ne(tab->mat->row[row1][off + col],
750 tab->mat->row[row2][off + col]))
751 return 0;
753 return 1;
756 /* Return an inequality that expresses that the "parametric constant"
757 * should be non-negative.
758 * This function is only called when the coefficient of the big parameter
759 * is equal to zero.
761 static struct isl_vec *get_row_parameter_ineq(struct isl_tab *tab, int row)
763 struct isl_vec *ineq;
765 ineq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_param + tab->n_div);
766 if (!ineq)
767 return NULL;
769 get_row_parameter_line(tab, row, ineq->el);
770 if (ineq)
771 ineq = isl_vec_normalize(ineq);
773 return ineq;
776 /* Normalize a div expression of the form
778 * [(g*f(x) + c)/(g * m)]
780 * with c the constant term and f(x) the remaining coefficients, to
782 * [(f(x) + [c/g])/m]
784 static void normalize_div(__isl_keep isl_vec *div)
786 isl_ctx *ctx = isl_vec_get_ctx(div);
787 int len = div->size - 2;
789 isl_seq_gcd(div->el + 2, len, &ctx->normalize_gcd);
790 isl_int_gcd(ctx->normalize_gcd, ctx->normalize_gcd, div->el[0]);
792 if (isl_int_is_one(ctx->normalize_gcd))
793 return;
795 isl_int_divexact(div->el[0], div->el[0], ctx->normalize_gcd);
796 isl_int_fdiv_q(div->el[1], div->el[1], ctx->normalize_gcd);
797 isl_seq_scale_down(div->el + 2, div->el + 2, ctx->normalize_gcd, len);
800 /* Return a integer division for use in a parametric cut based on the given row.
801 * In particular, let the parametric constant of the row be
803 * \sum_i a_i y_i
805 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
806 * The div returned is equal to
808 * floor(\sum_i {-a_i} y_i) = floor((\sum_i (-a_i mod d) y_i)/d)
810 static struct isl_vec *get_row_parameter_div(struct isl_tab *tab, int row)
812 struct isl_vec *div;
814 div = isl_vec_alloc(tab->mat->ctx, 1 + 1 + tab->n_param + tab->n_div);
815 if (!div)
816 return NULL;
818 isl_int_set(div->el[0], tab->mat->row[row][0]);
819 get_row_parameter_line(tab, row, div->el + 1);
820 isl_seq_neg(div->el + 1, div->el + 1, div->size - 1);
821 normalize_div(div);
822 isl_seq_fdiv_r(div->el + 1, div->el + 1, div->el[0], div->size - 1);
824 return div;
827 /* Return a integer division for use in transferring an integrality constraint
828 * to the context.
829 * In particular, let the parametric constant of the row be
831 * \sum_i a_i y_i
833 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
834 * The the returned div is equal to
836 * floor(\sum_i {a_i} y_i) = floor((\sum_i (a_i mod d) y_i)/d)
838 static struct isl_vec *get_row_split_div(struct isl_tab *tab, int row)
840 struct isl_vec *div;
842 div = isl_vec_alloc(tab->mat->ctx, 1 + 1 + tab->n_param + tab->n_div);
843 if (!div)
844 return NULL;
846 isl_int_set(div->el[0], tab->mat->row[row][0]);
847 get_row_parameter_line(tab, row, div->el + 1);
848 normalize_div(div);
849 isl_seq_fdiv_r(div->el + 1, div->el + 1, div->el[0], div->size - 1);
851 return div;
854 /* Construct and return an inequality that expresses an upper bound
855 * on the given div.
856 * In particular, if the div is given by
858 * d = floor(e/m)
860 * then the inequality expresses
862 * m d <= e
864 static struct isl_vec *ineq_for_div(struct isl_basic_set *bset, unsigned div)
866 unsigned total;
867 unsigned div_pos;
868 struct isl_vec *ineq;
870 if (!bset)
871 return NULL;
873 total = isl_basic_set_total_dim(bset);
874 div_pos = 1 + total - bset->n_div + div;
876 ineq = isl_vec_alloc(bset->ctx, 1 + total);
877 if (!ineq)
878 return NULL;
880 isl_seq_cpy(ineq->el, bset->div[div] + 1, 1 + total);
881 isl_int_neg(ineq->el[div_pos], bset->div[div][0]);
882 return ineq;
885 /* Given a row in the tableau and a div that was created
886 * using get_row_split_div and that has been constrained to equality, i.e.,
888 * d = floor(\sum_i {a_i} y_i) = \sum_i {a_i} y_i
890 * replace the expression "\sum_i {a_i} y_i" in the row by d,
891 * i.e., we subtract "\sum_i {a_i} y_i" and add 1 d.
892 * The coefficients of the non-parameters in the tableau have been
893 * verified to be integral. We can therefore simply replace coefficient b
894 * by floor(b). For the coefficients of the parameters we have
895 * floor(a_i) = a_i - {a_i}, while for the other coefficients, we have
896 * floor(b) = b.
898 static struct isl_tab *set_row_cst_to_div(struct isl_tab *tab, int row, int div)
900 isl_seq_fdiv_q(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
901 tab->mat->row[row][0], 1 + tab->M + tab->n_col);
903 isl_int_set_si(tab->mat->row[row][0], 1);
905 if (tab->var[tab->n_var - tab->n_div + div].is_row) {
906 int drow = tab->var[tab->n_var - tab->n_div + div].index;
908 isl_assert(tab->mat->ctx,
909 isl_int_is_one(tab->mat->row[drow][0]), goto error);
910 isl_seq_combine(tab->mat->row[row] + 1,
911 tab->mat->ctx->one, tab->mat->row[row] + 1,
912 tab->mat->ctx->one, tab->mat->row[drow] + 1,
913 1 + tab->M + tab->n_col);
914 } else {
915 int dcol = tab->var[tab->n_var - tab->n_div + div].index;
917 isl_int_add_ui(tab->mat->row[row][2 + tab->M + dcol],
918 tab->mat->row[row][2 + tab->M + dcol], 1);
921 return tab;
922 error:
923 isl_tab_free(tab);
924 return NULL;
927 /* Check if the (parametric) constant of the given row is obviously
928 * negative, meaning that we don't need to consult the context tableau.
929 * If there is a big parameter and its coefficient is non-zero,
930 * then this coefficient determines the outcome.
931 * Otherwise, we check whether the constant is negative and
932 * all non-zero coefficients of parameters are negative and
933 * belong to non-negative parameters.
935 static int is_obviously_neg(struct isl_tab *tab, int row)
937 int i;
938 int col;
939 unsigned off = 2 + tab->M;
941 if (tab->M) {
942 if (isl_int_is_pos(tab->mat->row[row][2]))
943 return 0;
944 if (isl_int_is_neg(tab->mat->row[row][2]))
945 return 1;
948 if (isl_int_is_nonneg(tab->mat->row[row][1]))
949 return 0;
950 for (i = 0; i < tab->n_param; ++i) {
951 /* Eliminated parameter */
952 if (tab->var[i].is_row)
953 continue;
954 col = tab->var[i].index;
955 if (isl_int_is_zero(tab->mat->row[row][off + col]))
956 continue;
957 if (!tab->var[i].is_nonneg)
958 return 0;
959 if (isl_int_is_pos(tab->mat->row[row][off + col]))
960 return 0;
962 for (i = 0; i < tab->n_div; ++i) {
963 if (tab->var[tab->n_var - tab->n_div + i].is_row)
964 continue;
965 col = tab->var[tab->n_var - tab->n_div + i].index;
966 if (isl_int_is_zero(tab->mat->row[row][off + col]))
967 continue;
968 if (!tab->var[tab->n_var - tab->n_div + i].is_nonneg)
969 return 0;
970 if (isl_int_is_pos(tab->mat->row[row][off + col]))
971 return 0;
973 return 1;
976 /* Check if the (parametric) constant of the given row is obviously
977 * non-negative, meaning that we don't need to consult the context tableau.
978 * If there is a big parameter and its coefficient is non-zero,
979 * then this coefficient determines the outcome.
980 * Otherwise, we check whether the constant is non-negative and
981 * all non-zero coefficients of parameters are positive and
982 * belong to non-negative parameters.
984 static int is_obviously_nonneg(struct isl_tab *tab, int row)
986 int i;
987 int col;
988 unsigned off = 2 + tab->M;
990 if (tab->M) {
991 if (isl_int_is_pos(tab->mat->row[row][2]))
992 return 1;
993 if (isl_int_is_neg(tab->mat->row[row][2]))
994 return 0;
997 if (isl_int_is_neg(tab->mat->row[row][1]))
998 return 0;
999 for (i = 0; i < tab->n_param; ++i) {
1000 /* Eliminated parameter */
1001 if (tab->var[i].is_row)
1002 continue;
1003 col = tab->var[i].index;
1004 if (isl_int_is_zero(tab->mat->row[row][off + col]))
1005 continue;
1006 if (!tab->var[i].is_nonneg)
1007 return 0;
1008 if (isl_int_is_neg(tab->mat->row[row][off + col]))
1009 return 0;
1011 for (i = 0; i < tab->n_div; ++i) {
1012 if (tab->var[tab->n_var - tab->n_div + i].is_row)
1013 continue;
1014 col = tab->var[tab->n_var - tab->n_div + i].index;
1015 if (isl_int_is_zero(tab->mat->row[row][off + col]))
1016 continue;
1017 if (!tab->var[tab->n_var - tab->n_div + i].is_nonneg)
1018 return 0;
1019 if (isl_int_is_neg(tab->mat->row[row][off + col]))
1020 return 0;
1022 return 1;
1025 /* Given a row r and two columns, return the column that would
1026 * lead to the lexicographically smallest increment in the sample
1027 * solution when leaving the basis in favor of the row.
1028 * Pivoting with column c will increment the sample value by a non-negative
1029 * constant times a_{V,c}/a_{r,c}, with a_{V,c} the elements of column c
1030 * corresponding to the non-parametric variables.
1031 * If variable v appears in a column c_v, the a_{v,c} = 1 iff c = c_v,
1032 * with all other entries in this virtual row equal to zero.
1033 * If variable v appears in a row, then a_{v,c} is the element in column c
1034 * of that row.
1036 * Let v be the first variable with a_{v,c1}/a_{r,c1} != a_{v,c2}/a_{r,c2}.
1037 * Then if a_{v,c1}/a_{r,c1} < a_{v,c2}/a_{r,c2}, i.e.,
1038 * a_{v,c2} a_{r,c1} - a_{v,c1} a_{r,c2} > 0, c1 results in the minimal
1039 * increment. Otherwise, it's c2.
1041 static int lexmin_col_pair(struct isl_tab *tab,
1042 int row, int col1, int col2, isl_int tmp)
1044 int i;
1045 isl_int *tr;
1047 tr = tab->mat->row[row] + 2 + tab->M;
1049 for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
1050 int s1, s2;
1051 isl_int *r;
1053 if (!tab->var[i].is_row) {
1054 if (tab->var[i].index == col1)
1055 return col2;
1056 if (tab->var[i].index == col2)
1057 return col1;
1058 continue;
1061 if (tab->var[i].index == row)
1062 continue;
1064 r = tab->mat->row[tab->var[i].index] + 2 + tab->M;
1065 s1 = isl_int_sgn(r[col1]);
1066 s2 = isl_int_sgn(r[col2]);
1067 if (s1 == 0 && s2 == 0)
1068 continue;
1069 if (s1 < s2)
1070 return col1;
1071 if (s2 < s1)
1072 return col2;
1074 isl_int_mul(tmp, r[col2], tr[col1]);
1075 isl_int_submul(tmp, r[col1], tr[col2]);
1076 if (isl_int_is_pos(tmp))
1077 return col1;
1078 if (isl_int_is_neg(tmp))
1079 return col2;
1081 return -1;
1084 /* Given a row in the tableau, find and return the column that would
1085 * result in the lexicographically smallest, but positive, increment
1086 * in the sample point.
1087 * If there is no such column, then return tab->n_col.
1088 * If anything goes wrong, return -1.
1090 static int lexmin_pivot_col(struct isl_tab *tab, int row)
1092 int j;
1093 int col = tab->n_col;
1094 isl_int *tr;
1095 isl_int tmp;
1097 tr = tab->mat->row[row] + 2 + tab->M;
1099 isl_int_init(tmp);
1101 for (j = tab->n_dead; j < tab->n_col; ++j) {
1102 if (tab->col_var[j] >= 0 &&
1103 (tab->col_var[j] < tab->n_param ||
1104 tab->col_var[j] >= tab->n_var - tab->n_div))
1105 continue;
1107 if (!isl_int_is_pos(tr[j]))
1108 continue;
1110 if (col == tab->n_col)
1111 col = j;
1112 else
1113 col = lexmin_col_pair(tab, row, col, j, tmp);
1114 isl_assert(tab->mat->ctx, col >= 0, goto error);
1117 isl_int_clear(tmp);
1118 return col;
1119 error:
1120 isl_int_clear(tmp);
1121 return -1;
1124 /* Return the first known violated constraint, i.e., a non-negative
1125 * constraint that currently has an either obviously negative value
1126 * or a previously determined to be negative value.
1128 * If any constraint has a negative coefficient for the big parameter,
1129 * if any, then we return one of these first.
1131 static int first_neg(struct isl_tab *tab)
1133 int row;
1135 if (tab->M)
1136 for (row = tab->n_redundant; row < tab->n_row; ++row) {
1137 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
1138 continue;
1139 if (!isl_int_is_neg(tab->mat->row[row][2]))
1140 continue;
1141 if (tab->row_sign)
1142 tab->row_sign[row] = isl_tab_row_neg;
1143 return row;
1145 for (row = tab->n_redundant; row < tab->n_row; ++row) {
1146 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
1147 continue;
1148 if (tab->row_sign) {
1149 if (tab->row_sign[row] == 0 &&
1150 is_obviously_neg(tab, row))
1151 tab->row_sign[row] = isl_tab_row_neg;
1152 if (tab->row_sign[row] != isl_tab_row_neg)
1153 continue;
1154 } else if (!is_obviously_neg(tab, row))
1155 continue;
1156 return row;
1158 return -1;
1161 /* Check whether the invariant that all columns are lexico-positive
1162 * is satisfied. This function is not called from the current code
1163 * but is useful during debugging.
1165 static void check_lexpos(struct isl_tab *tab) __attribute__ ((unused));
1166 static void check_lexpos(struct isl_tab *tab)
1168 unsigned off = 2 + tab->M;
1169 int col;
1170 int var;
1171 int row;
1173 for (col = tab->n_dead; col < tab->n_col; ++col) {
1174 if (tab->col_var[col] >= 0 &&
1175 (tab->col_var[col] < tab->n_param ||
1176 tab->col_var[col] >= tab->n_var - tab->n_div))
1177 continue;
1178 for (var = tab->n_param; var < tab->n_var - tab->n_div; ++var) {
1179 if (!tab->var[var].is_row) {
1180 if (tab->var[var].index == col)
1181 break;
1182 else
1183 continue;
1185 row = tab->var[var].index;
1186 if (isl_int_is_zero(tab->mat->row[row][off + col]))
1187 continue;
1188 if (isl_int_is_pos(tab->mat->row[row][off + col]))
1189 break;
1190 fprintf(stderr, "lexneg column %d (row %d)\n",
1191 col, row);
1193 if (var >= tab->n_var - tab->n_div)
1194 fprintf(stderr, "zero column %d\n", col);
1198 /* Report to the caller that the given constraint is part of an encountered
1199 * conflict.
1201 static int report_conflicting_constraint(struct isl_tab *tab, int con)
1203 return tab->conflict(con, tab->conflict_user);
1206 /* Given a conflicting row in the tableau, report all constraints
1207 * involved in the row to the caller. That is, the row itself
1208 * (if it represents a constraint) and all constraint columns with
1209 * non-zero (and therefore negative) coefficients.
1211 static int report_conflict(struct isl_tab *tab, int row)
1213 int j;
1214 isl_int *tr;
1216 if (!tab->conflict)
1217 return 0;
1219 if (tab->row_var[row] < 0 &&
1220 report_conflicting_constraint(tab, ~tab->row_var[row]) < 0)
1221 return -1;
1223 tr = tab->mat->row[row] + 2 + tab->M;
1225 for (j = tab->n_dead; j < tab->n_col; ++j) {
1226 if (tab->col_var[j] >= 0 &&
1227 (tab->col_var[j] < tab->n_param ||
1228 tab->col_var[j] >= tab->n_var - tab->n_div))
1229 continue;
1231 if (!isl_int_is_neg(tr[j]))
1232 continue;
1234 if (tab->col_var[j] < 0 &&
1235 report_conflicting_constraint(tab, ~tab->col_var[j]) < 0)
1236 return -1;
1239 return 0;
1242 /* Resolve all known or obviously violated constraints through pivoting.
1243 * In particular, as long as we can find any violated constraint, we
1244 * look for a pivoting column that would result in the lexicographically
1245 * smallest increment in the sample point. If there is no such column
1246 * then the tableau is infeasible.
1248 static int restore_lexmin(struct isl_tab *tab) WARN_UNUSED;
1249 static int restore_lexmin(struct isl_tab *tab)
1251 int row, col;
1253 if (!tab)
1254 return -1;
1255 if (tab->empty)
1256 return 0;
1257 while ((row = first_neg(tab)) != -1) {
1258 col = lexmin_pivot_col(tab, row);
1259 if (col >= tab->n_col) {
1260 if (report_conflict(tab, row) < 0)
1261 return -1;
1262 if (isl_tab_mark_empty(tab) < 0)
1263 return -1;
1264 return 0;
1266 if (col < 0)
1267 return -1;
1268 if (isl_tab_pivot(tab, row, col) < 0)
1269 return -1;
1271 return 0;
1274 /* Given a row that represents an equality, look for an appropriate
1275 * pivoting column.
1276 * In particular, if there are any non-zero coefficients among
1277 * the non-parameter variables, then we take the last of these
1278 * variables. Eliminating this variable in terms of the other
1279 * variables and/or parameters does not influence the property
1280 * that all column in the initial tableau are lexicographically
1281 * positive. The row corresponding to the eliminated variable
1282 * will only have non-zero entries below the diagonal of the
1283 * initial tableau. That is, we transform
1285 * I I
1286 * 1 into a
1287 * I I
1289 * If there is no such non-parameter variable, then we are dealing with
1290 * pure parameter equality and we pick any parameter with coefficient 1 or -1
1291 * for elimination. This will ensure that the eliminated parameter
1292 * always has an integer value whenever all the other parameters are integral.
1293 * If there is no such parameter then we return -1.
1295 static int last_var_col_or_int_par_col(struct isl_tab *tab, int row)
1297 unsigned off = 2 + tab->M;
1298 int i;
1300 for (i = tab->n_var - tab->n_div - 1; i >= 0 && i >= tab->n_param; --i) {
1301 int col;
1302 if (tab->var[i].is_row)
1303 continue;
1304 col = tab->var[i].index;
1305 if (col <= tab->n_dead)
1306 continue;
1307 if (!isl_int_is_zero(tab->mat->row[row][off + col]))
1308 return col;
1310 for (i = tab->n_dead; i < tab->n_col; ++i) {
1311 if (isl_int_is_one(tab->mat->row[row][off + i]))
1312 return i;
1313 if (isl_int_is_negone(tab->mat->row[row][off + i]))
1314 return i;
1316 return -1;
1319 /* Add an equality that is known to be valid to the tableau.
1320 * We first check if we can eliminate a variable or a parameter.
1321 * If not, we add the equality as two inequalities.
1322 * In this case, the equality was a pure parameter equality and there
1323 * is no need to resolve any constraint violations.
1325 static struct isl_tab *add_lexmin_valid_eq(struct isl_tab *tab, isl_int *eq)
1327 int i;
1328 int r;
1330 if (!tab)
1331 return NULL;
1332 r = isl_tab_add_row(tab, eq);
1333 if (r < 0)
1334 goto error;
1336 r = tab->con[r].index;
1337 i = last_var_col_or_int_par_col(tab, r);
1338 if (i < 0) {
1339 tab->con[r].is_nonneg = 1;
1340 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1341 goto error;
1342 isl_seq_neg(eq, eq, 1 + tab->n_var);
1343 r = isl_tab_add_row(tab, eq);
1344 if (r < 0)
1345 goto error;
1346 tab->con[r].is_nonneg = 1;
1347 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1348 goto error;
1349 } else {
1350 if (isl_tab_pivot(tab, r, i) < 0)
1351 goto error;
1352 if (isl_tab_kill_col(tab, i) < 0)
1353 goto error;
1354 tab->n_eq++;
1357 return tab;
1358 error:
1359 isl_tab_free(tab);
1360 return NULL;
1363 /* Check if the given row is a pure constant.
1365 static int is_constant(struct isl_tab *tab, int row)
1367 unsigned off = 2 + tab->M;
1369 return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1370 tab->n_col - tab->n_dead) == -1;
1373 /* Add an equality that may or may not be valid to the tableau.
1374 * If the resulting row is a pure constant, then it must be zero.
1375 * Otherwise, the resulting tableau is empty.
1377 * If the row is not a pure constant, then we add two inequalities,
1378 * each time checking that they can be satisfied.
1379 * In the end we try to use one of the two constraints to eliminate
1380 * a column.
1382 static int add_lexmin_eq(struct isl_tab *tab, isl_int *eq) WARN_UNUSED;
1383 static int add_lexmin_eq(struct isl_tab *tab, isl_int *eq)
1385 int r1, r2;
1386 int row;
1387 struct isl_tab_undo *snap;
1389 if (!tab)
1390 return -1;
1391 snap = isl_tab_snap(tab);
1392 r1 = isl_tab_add_row(tab, eq);
1393 if (r1 < 0)
1394 return -1;
1395 tab->con[r1].is_nonneg = 1;
1396 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r1]) < 0)
1397 return -1;
1399 row = tab->con[r1].index;
1400 if (is_constant(tab, row)) {
1401 if (!isl_int_is_zero(tab->mat->row[row][1]) ||
1402 (tab->M && !isl_int_is_zero(tab->mat->row[row][2]))) {
1403 if (isl_tab_mark_empty(tab) < 0)
1404 return -1;
1405 return 0;
1407 if (isl_tab_rollback(tab, snap) < 0)
1408 return -1;
1409 return 0;
1412 if (restore_lexmin(tab) < 0)
1413 return -1;
1414 if (tab->empty)
1415 return 0;
1417 isl_seq_neg(eq, eq, 1 + tab->n_var);
1419 r2 = isl_tab_add_row(tab, eq);
1420 if (r2 < 0)
1421 return -1;
1422 tab->con[r2].is_nonneg = 1;
1423 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r2]) < 0)
1424 return -1;
1426 if (restore_lexmin(tab) < 0)
1427 return -1;
1428 if (tab->empty)
1429 return 0;
1431 if (!tab->con[r1].is_row) {
1432 if (isl_tab_kill_col(tab, tab->con[r1].index) < 0)
1433 return -1;
1434 } else if (!tab->con[r2].is_row) {
1435 if (isl_tab_kill_col(tab, tab->con[r2].index) < 0)
1436 return -1;
1439 if (tab->bmap) {
1440 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1441 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1442 return -1;
1443 isl_seq_neg(eq, eq, 1 + tab->n_var);
1444 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1445 isl_seq_neg(eq, eq, 1 + tab->n_var);
1446 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1447 return -1;
1448 if (!tab->bmap)
1449 return -1;
1452 return 0;
1455 /* Add an inequality to the tableau, resolving violations using
1456 * restore_lexmin.
1458 static struct isl_tab *add_lexmin_ineq(struct isl_tab *tab, isl_int *ineq)
1460 int r;
1462 if (!tab)
1463 return NULL;
1464 if (tab->bmap) {
1465 tab->bmap = isl_basic_map_add_ineq(tab->bmap, ineq);
1466 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1467 goto error;
1468 if (!tab->bmap)
1469 goto error;
1471 r = isl_tab_add_row(tab, ineq);
1472 if (r < 0)
1473 goto error;
1474 tab->con[r].is_nonneg = 1;
1475 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1476 goto error;
1477 if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1478 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1479 goto error;
1480 return tab;
1483 if (restore_lexmin(tab) < 0)
1484 goto error;
1485 if (!tab->empty && tab->con[r].is_row &&
1486 isl_tab_row_is_redundant(tab, tab->con[r].index))
1487 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1488 goto error;
1489 return tab;
1490 error:
1491 isl_tab_free(tab);
1492 return NULL;
1495 /* Check if the coefficients of the parameters are all integral.
1497 static int integer_parameter(struct isl_tab *tab, int row)
1499 int i;
1500 int col;
1501 unsigned off = 2 + tab->M;
1503 for (i = 0; i < tab->n_param; ++i) {
1504 /* Eliminated parameter */
1505 if (tab->var[i].is_row)
1506 continue;
1507 col = tab->var[i].index;
1508 if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
1509 tab->mat->row[row][0]))
1510 return 0;
1512 for (i = 0; i < tab->n_div; ++i) {
1513 if (tab->var[tab->n_var - tab->n_div + i].is_row)
1514 continue;
1515 col = tab->var[tab->n_var - tab->n_div + i].index;
1516 if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
1517 tab->mat->row[row][0]))
1518 return 0;
1520 return 1;
1523 /* Check if the coefficients of the non-parameter variables are all integral.
1525 static int integer_variable(struct isl_tab *tab, int row)
1527 int i;
1528 unsigned off = 2 + tab->M;
1530 for (i = tab->n_dead; i < tab->n_col; ++i) {
1531 if (tab->col_var[i] >= 0 &&
1532 (tab->col_var[i] < tab->n_param ||
1533 tab->col_var[i] >= tab->n_var - tab->n_div))
1534 continue;
1535 if (!isl_int_is_divisible_by(tab->mat->row[row][off + i],
1536 tab->mat->row[row][0]))
1537 return 0;
1539 return 1;
1542 /* Check if the constant term is integral.
1544 static int integer_constant(struct isl_tab *tab, int row)
1546 return isl_int_is_divisible_by(tab->mat->row[row][1],
1547 tab->mat->row[row][0]);
1550 #define I_CST 1 << 0
1551 #define I_PAR 1 << 1
1552 #define I_VAR 1 << 2
1554 /* Check for next (non-parameter) variable after "var" (first if var == -1)
1555 * that is non-integer and therefore requires a cut and return
1556 * the index of the variable.
1557 * For parametric tableaus, there are three parts in a row,
1558 * the constant, the coefficients of the parameters and the rest.
1559 * For each part, we check whether the coefficients in that part
1560 * are all integral and if so, set the corresponding flag in *f.
1561 * If the constant and the parameter part are integral, then the
1562 * current sample value is integral and no cut is required
1563 * (irrespective of whether the variable part is integral).
1565 static int next_non_integer_var(struct isl_tab *tab, int var, int *f)
1567 var = var < 0 ? tab->n_param : var + 1;
1569 for (; var < tab->n_var - tab->n_div; ++var) {
1570 int flags = 0;
1571 int row;
1572 if (!tab->var[var].is_row)
1573 continue;
1574 row = tab->var[var].index;
1575 if (integer_constant(tab, row))
1576 ISL_FL_SET(flags, I_CST);
1577 if (integer_parameter(tab, row))
1578 ISL_FL_SET(flags, I_PAR);
1579 if (ISL_FL_ISSET(flags, I_CST) && ISL_FL_ISSET(flags, I_PAR))
1580 continue;
1581 if (integer_variable(tab, row))
1582 ISL_FL_SET(flags, I_VAR);
1583 *f = flags;
1584 return var;
1586 return -1;
1589 /* Check for first (non-parameter) variable that is non-integer and
1590 * therefore requires a cut and return the corresponding row.
1591 * For parametric tableaus, there are three parts in a row,
1592 * the constant, the coefficients of the parameters and the rest.
1593 * For each part, we check whether the coefficients in that part
1594 * are all integral and if so, set the corresponding flag in *f.
1595 * If the constant and the parameter part are integral, then the
1596 * current sample value is integral and no cut is required
1597 * (irrespective of whether the variable part is integral).
1599 static int first_non_integer_row(struct isl_tab *tab, int *f)
1601 int var = next_non_integer_var(tab, -1, f);
1603 return var < 0 ? -1 : tab->var[var].index;
1606 /* Add a (non-parametric) cut to cut away the non-integral sample
1607 * value of the given row.
1609 * If the row is given by
1611 * m r = f + \sum_i a_i y_i
1613 * then the cut is
1615 * c = - {-f/m} + \sum_i {a_i/m} y_i >= 0
1617 * The big parameter, if any, is ignored, since it is assumed to be big
1618 * enough to be divisible by any integer.
1619 * If the tableau is actually a parametric tableau, then this function
1620 * is only called when all coefficients of the parameters are integral.
1621 * The cut therefore has zero coefficients for the parameters.
1623 * The current value is known to be negative, so row_sign, if it
1624 * exists, is set accordingly.
1626 * Return the row of the cut or -1.
1628 static int add_cut(struct isl_tab *tab, int row)
1630 int i;
1631 int r;
1632 isl_int *r_row;
1633 unsigned off = 2 + tab->M;
1635 if (isl_tab_extend_cons(tab, 1) < 0)
1636 return -1;
1637 r = isl_tab_allocate_con(tab);
1638 if (r < 0)
1639 return -1;
1641 r_row = tab->mat->row[tab->con[r].index];
1642 isl_int_set(r_row[0], tab->mat->row[row][0]);
1643 isl_int_neg(r_row[1], tab->mat->row[row][1]);
1644 isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
1645 isl_int_neg(r_row[1], r_row[1]);
1646 if (tab->M)
1647 isl_int_set_si(r_row[2], 0);
1648 for (i = 0; i < tab->n_col; ++i)
1649 isl_int_fdiv_r(r_row[off + i],
1650 tab->mat->row[row][off + i], tab->mat->row[row][0]);
1652 tab->con[r].is_nonneg = 1;
1653 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1654 return -1;
1655 if (tab->row_sign)
1656 tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
1658 return tab->con[r].index;
1661 #define CUT_ALL 1
1662 #define CUT_ONE 0
1664 /* Given a non-parametric tableau, add cuts until an integer
1665 * sample point is obtained or until the tableau is determined
1666 * to be integer infeasible.
1667 * As long as there is any non-integer value in the sample point,
1668 * we add appropriate cuts, if possible, for each of these
1669 * non-integer values and then resolve the violated
1670 * cut constraints using restore_lexmin.
1671 * If one of the corresponding rows is equal to an integral
1672 * combination of variables/constraints plus a non-integral constant,
1673 * then there is no way to obtain an integer point and we return
1674 * a tableau that is marked empty.
1675 * The parameter cutting_strategy controls the strategy used when adding cuts
1676 * to remove non-integer points. CUT_ALL adds all possible cuts
1677 * before continuing the search. CUT_ONE adds only one cut at a time.
1679 static struct isl_tab *cut_to_integer_lexmin(struct isl_tab *tab,
1680 int cutting_strategy)
1682 int var;
1683 int row;
1684 int flags;
1686 if (!tab)
1687 return NULL;
1688 if (tab->empty)
1689 return tab;
1691 while ((var = next_non_integer_var(tab, -1, &flags)) != -1) {
1692 do {
1693 if (ISL_FL_ISSET(flags, I_VAR)) {
1694 if (isl_tab_mark_empty(tab) < 0)
1695 goto error;
1696 return tab;
1698 row = tab->var[var].index;
1699 row = add_cut(tab, row);
1700 if (row < 0)
1701 goto error;
1702 if (cutting_strategy == CUT_ONE)
1703 break;
1704 } while ((var = next_non_integer_var(tab, var, &flags)) != -1);
1705 if (restore_lexmin(tab) < 0)
1706 goto error;
1707 if (tab->empty)
1708 break;
1710 return tab;
1711 error:
1712 isl_tab_free(tab);
1713 return NULL;
1716 /* Check whether all the currently active samples also satisfy the inequality
1717 * "ineq" (treated as an equality if eq is set).
1718 * Remove those samples that do not.
1720 static struct isl_tab *check_samples(struct isl_tab *tab, isl_int *ineq, int eq)
1722 int i;
1723 isl_int v;
1725 if (!tab)
1726 return NULL;
1728 isl_assert(tab->mat->ctx, tab->bmap, goto error);
1729 isl_assert(tab->mat->ctx, tab->samples, goto error);
1730 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, goto error);
1732 isl_int_init(v);
1733 for (i = tab->n_outside; i < tab->n_sample; ++i) {
1734 int sgn;
1735 isl_seq_inner_product(ineq, tab->samples->row[i],
1736 1 + tab->n_var, &v);
1737 sgn = isl_int_sgn(v);
1738 if (eq ? (sgn == 0) : (sgn >= 0))
1739 continue;
1740 tab = isl_tab_drop_sample(tab, i);
1741 if (!tab)
1742 break;
1744 isl_int_clear(v);
1746 return tab;
1747 error:
1748 isl_tab_free(tab);
1749 return NULL;
1752 /* Check whether the sample value of the tableau is finite,
1753 * i.e., either the tableau does not use a big parameter, or
1754 * all values of the variables are equal to the big parameter plus
1755 * some constant. This constant is the actual sample value.
1757 static int sample_is_finite(struct isl_tab *tab)
1759 int i;
1761 if (!tab->M)
1762 return 1;
1764 for (i = 0; i < tab->n_var; ++i) {
1765 int row;
1766 if (!tab->var[i].is_row)
1767 return 0;
1768 row = tab->var[i].index;
1769 if (isl_int_ne(tab->mat->row[row][0], tab->mat->row[row][2]))
1770 return 0;
1772 return 1;
1775 /* Check if the context tableau of sol has any integer points.
1776 * Leave tab in empty state if no integer point can be found.
1777 * If an integer point can be found and if moreover it is finite,
1778 * then it is added to the list of sample values.
1780 * This function is only called when none of the currently active sample
1781 * values satisfies the most recently added constraint.
1783 static struct isl_tab *check_integer_feasible(struct isl_tab *tab)
1785 struct isl_tab_undo *snap;
1787 if (!tab)
1788 return NULL;
1790 snap = isl_tab_snap(tab);
1791 if (isl_tab_push_basis(tab) < 0)
1792 goto error;
1794 tab = cut_to_integer_lexmin(tab, CUT_ALL);
1795 if (!tab)
1796 goto error;
1798 if (!tab->empty && sample_is_finite(tab)) {
1799 struct isl_vec *sample;
1801 sample = isl_tab_get_sample_value(tab);
1803 tab = isl_tab_add_sample(tab, sample);
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;
2080 tab = isl_tab_alloc(bmap->ctx, 2 * bmap->n_eq + bmap->n_ineq + 1,
2081 isl_basic_map_total_dim(bmap), M);
2082 if (!tab)
2083 return NULL;
2085 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
2086 if (dom) {
2087 tab->n_param = isl_basic_set_total_dim(dom) - dom->n_div;
2088 tab->n_div = dom->n_div;
2089 tab->row_sign = isl_calloc_array(bmap->ctx,
2090 enum isl_tab_row_sign, tab->mat->n_row);
2091 if (tab->mat->n_row && !tab->row_sign)
2092 goto error;
2094 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
2095 if (isl_tab_mark_empty(tab) < 0)
2096 goto error;
2097 return tab;
2100 for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
2101 tab->var[i].is_nonneg = 1;
2102 tab->var[i].frozen = 1;
2104 for (i = 0; i < bmap->n_eq; ++i) {
2105 if (max)
2106 isl_seq_neg(bmap->eq[i] + 1 + tab->n_param,
2107 bmap->eq[i] + 1 + tab->n_param,
2108 tab->n_var - tab->n_param - tab->n_div);
2109 tab = add_lexmin_valid_eq(tab, bmap->eq[i]);
2110 if (max)
2111 isl_seq_neg(bmap->eq[i] + 1 + tab->n_param,
2112 bmap->eq[i] + 1 + tab->n_param,
2113 tab->n_var - tab->n_param - tab->n_div);
2114 if (!tab || tab->empty)
2115 return tab;
2117 if (bmap->n_eq && restore_lexmin(tab) < 0)
2118 goto error;
2119 for (i = 0; i < bmap->n_ineq; ++i) {
2120 if (max)
2121 isl_seq_neg(bmap->ineq[i] + 1 + tab->n_param,
2122 bmap->ineq[i] + 1 + tab->n_param,
2123 tab->n_var - tab->n_param - tab->n_div);
2124 tab = add_lexmin_ineq(tab, bmap->ineq[i]);
2125 if (max)
2126 isl_seq_neg(bmap->ineq[i] + 1 + tab->n_param,
2127 bmap->ineq[i] + 1 + tab->n_param,
2128 tab->n_var - tab->n_param - tab->n_div);
2129 if (!tab || tab->empty)
2130 return tab;
2132 return tab;
2133 error:
2134 isl_tab_free(tab);
2135 return NULL;
2138 /* Given a main tableau where more than one row requires a split,
2139 * determine and return the "best" row to split on.
2141 * Given two rows in the main tableau, if the inequality corresponding
2142 * to the first row is redundant with respect to that of the second row
2143 * in the current tableau, then it is better to split on the second row,
2144 * since in the positive part, both row will be positive.
2145 * (In the negative part a pivot will have to be performed and just about
2146 * anything can happen to the sign of the other row.)
2148 * As a simple heuristic, we therefore select the row that makes the most
2149 * of the other rows redundant.
2151 * Perhaps it would also be useful to look at the number of constraints
2152 * that conflict with any given constraint.
2154 static int best_split(struct isl_tab *tab, struct isl_tab *context_tab)
2156 struct isl_tab_undo *snap;
2157 int split;
2158 int row;
2159 int best = -1;
2160 int best_r;
2162 if (isl_tab_extend_cons(context_tab, 2) < 0)
2163 return -1;
2165 snap = isl_tab_snap(context_tab);
2167 for (split = tab->n_redundant; split < tab->n_row; ++split) {
2168 struct isl_tab_undo *snap2;
2169 struct isl_vec *ineq = NULL;
2170 int r = 0;
2171 int ok;
2173 if (!isl_tab_var_from_row(tab, split)->is_nonneg)
2174 continue;
2175 if (tab->row_sign[split] != isl_tab_row_any)
2176 continue;
2178 ineq = get_row_parameter_ineq(tab, split);
2179 if (!ineq)
2180 return -1;
2181 ok = isl_tab_add_ineq(context_tab, ineq->el) >= 0;
2182 isl_vec_free(ineq);
2183 if (!ok)
2184 return -1;
2186 snap2 = isl_tab_snap(context_tab);
2188 for (row = tab->n_redundant; row < tab->n_row; ++row) {
2189 struct isl_tab_var *var;
2191 if (row == split)
2192 continue;
2193 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
2194 continue;
2195 if (tab->row_sign[row] != isl_tab_row_any)
2196 continue;
2198 ineq = get_row_parameter_ineq(tab, row);
2199 if (!ineq)
2200 return -1;
2201 ok = isl_tab_add_ineq(context_tab, ineq->el) >= 0;
2202 isl_vec_free(ineq);
2203 if (!ok)
2204 return -1;
2205 var = &context_tab->con[context_tab->n_con - 1];
2206 if (!context_tab->empty &&
2207 !isl_tab_min_at_most_neg_one(context_tab, var))
2208 r++;
2209 if (isl_tab_rollback(context_tab, snap2) < 0)
2210 return -1;
2212 if (best == -1 || r > best_r) {
2213 best = split;
2214 best_r = r;
2216 if (isl_tab_rollback(context_tab, snap) < 0)
2217 return -1;
2220 return best;
2223 static struct isl_basic_set *context_lex_peek_basic_set(
2224 struct isl_context *context)
2226 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2227 if (!clex->tab)
2228 return NULL;
2229 return isl_tab_peek_bset(clex->tab);
2232 static struct isl_tab *context_lex_peek_tab(struct isl_context *context)
2234 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2235 return clex->tab;
2238 static void context_lex_add_eq(struct isl_context *context, isl_int *eq,
2239 int check, int update)
2241 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2242 if (isl_tab_extend_cons(clex->tab, 2) < 0)
2243 goto error;
2244 if (add_lexmin_eq(clex->tab, eq) < 0)
2245 goto error;
2246 if (check) {
2247 int v = tab_has_valid_sample(clex->tab, eq, 1);
2248 if (v < 0)
2249 goto error;
2250 if (!v)
2251 clex->tab = check_integer_feasible(clex->tab);
2253 if (update)
2254 clex->tab = check_samples(clex->tab, eq, 1);
2255 return;
2256 error:
2257 isl_tab_free(clex->tab);
2258 clex->tab = NULL;
2261 static void context_lex_add_ineq(struct isl_context *context, isl_int *ineq,
2262 int check, int update)
2264 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2265 if (isl_tab_extend_cons(clex->tab, 1) < 0)
2266 goto error;
2267 clex->tab = add_lexmin_ineq(clex->tab, ineq);
2268 if (check) {
2269 int v = tab_has_valid_sample(clex->tab, ineq, 0);
2270 if (v < 0)
2271 goto error;
2272 if (!v)
2273 clex->tab = check_integer_feasible(clex->tab);
2275 if (update)
2276 clex->tab = check_samples(clex->tab, ineq, 0);
2277 return;
2278 error:
2279 isl_tab_free(clex->tab);
2280 clex->tab = NULL;
2283 static int context_lex_add_ineq_wrap(void *user, isl_int *ineq)
2285 struct isl_context *context = (struct isl_context *)user;
2286 context_lex_add_ineq(context, ineq, 0, 0);
2287 return context->op->is_ok(context) ? 0 : -1;
2290 /* Check which signs can be obtained by "ineq" on all the currently
2291 * active sample values. See row_sign for more information.
2293 static enum isl_tab_row_sign tab_ineq_sign(struct isl_tab *tab, isl_int *ineq,
2294 int strict)
2296 int i;
2297 int sgn;
2298 isl_int tmp;
2299 enum isl_tab_row_sign res = isl_tab_row_unknown;
2301 isl_assert(tab->mat->ctx, tab->samples, return isl_tab_row_unknown);
2302 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var,
2303 return isl_tab_row_unknown);
2305 isl_int_init(tmp);
2306 for (i = tab->n_outside; i < tab->n_sample; ++i) {
2307 isl_seq_inner_product(tab->samples->row[i], ineq,
2308 1 + tab->n_var, &tmp);
2309 sgn = isl_int_sgn(tmp);
2310 if (sgn > 0 || (sgn == 0 && strict)) {
2311 if (res == isl_tab_row_unknown)
2312 res = isl_tab_row_pos;
2313 if (res == isl_tab_row_neg)
2314 res = isl_tab_row_any;
2316 if (sgn < 0) {
2317 if (res == isl_tab_row_unknown)
2318 res = isl_tab_row_neg;
2319 if (res == isl_tab_row_pos)
2320 res = isl_tab_row_any;
2322 if (res == isl_tab_row_any)
2323 break;
2325 isl_int_clear(tmp);
2327 return res;
2330 static enum isl_tab_row_sign context_lex_ineq_sign(struct isl_context *context,
2331 isl_int *ineq, int strict)
2333 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2334 return tab_ineq_sign(clex->tab, ineq, strict);
2337 /* Check whether "ineq" can be added to the tableau without rendering
2338 * it infeasible.
2340 static int context_lex_test_ineq(struct isl_context *context, isl_int *ineq)
2342 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2343 struct isl_tab_undo *snap;
2344 int feasible;
2346 if (!clex->tab)
2347 return -1;
2349 if (isl_tab_extend_cons(clex->tab, 1) < 0)
2350 return -1;
2352 snap = isl_tab_snap(clex->tab);
2353 if (isl_tab_push_basis(clex->tab) < 0)
2354 return -1;
2355 clex->tab = add_lexmin_ineq(clex->tab, ineq);
2356 clex->tab = check_integer_feasible(clex->tab);
2357 if (!clex->tab)
2358 return -1;
2359 feasible = !clex->tab->empty;
2360 if (isl_tab_rollback(clex->tab, snap) < 0)
2361 return -1;
2363 return feasible;
2366 static int context_lex_get_div(struct isl_context *context, struct isl_tab *tab,
2367 struct isl_vec *div)
2369 return get_div(tab, context, div);
2372 /* Add a div specified by "div" to the context tableau and return
2373 * 1 if the div is obviously non-negative.
2374 * context_tab_add_div will always return 1, because all variables
2375 * in a isl_context_lex tableau are non-negative.
2376 * However, if we are using a big parameter in the context, then this only
2377 * reflects the non-negativity of the variable used to _encode_ the
2378 * div, i.e., div' = M + div, so we can't draw any conclusions.
2380 static int context_lex_add_div(struct isl_context *context, struct isl_vec *div)
2382 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2383 int nonneg;
2384 nonneg = context_tab_add_div(clex->tab, div,
2385 context_lex_add_ineq_wrap, context);
2386 if (nonneg < 0)
2387 return -1;
2388 if (clex->tab->M)
2389 return 0;
2390 return nonneg;
2393 static int context_lex_detect_equalities(struct isl_context *context,
2394 struct isl_tab *tab)
2396 return 0;
2399 static int context_lex_best_split(struct isl_context *context,
2400 struct isl_tab *tab)
2402 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2403 struct isl_tab_undo *snap;
2404 int r;
2406 snap = isl_tab_snap(clex->tab);
2407 if (isl_tab_push_basis(clex->tab) < 0)
2408 return -1;
2409 r = best_split(tab, clex->tab);
2411 if (r >= 0 && isl_tab_rollback(clex->tab, snap) < 0)
2412 return -1;
2414 return r;
2417 static int context_lex_is_empty(struct isl_context *context)
2419 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2420 if (!clex->tab)
2421 return -1;
2422 return clex->tab->empty;
2425 static void *context_lex_save(struct isl_context *context)
2427 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2428 struct isl_tab_undo *snap;
2430 snap = isl_tab_snap(clex->tab);
2431 if (isl_tab_push_basis(clex->tab) < 0)
2432 return NULL;
2433 if (isl_tab_save_samples(clex->tab) < 0)
2434 return NULL;
2436 return snap;
2439 static void context_lex_restore(struct isl_context *context, void *save)
2441 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2442 if (isl_tab_rollback(clex->tab, (struct isl_tab_undo *)save) < 0) {
2443 isl_tab_free(clex->tab);
2444 clex->tab = NULL;
2448 static void context_lex_discard(void *save)
2452 static int context_lex_is_ok(struct isl_context *context)
2454 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2455 return !!clex->tab;
2458 /* For each variable in the context tableau, check if the variable can
2459 * only attain non-negative values. If so, mark the parameter as non-negative
2460 * in the main tableau. This allows for a more direct identification of some
2461 * cases of violated constraints.
2463 static struct isl_tab *tab_detect_nonnegative_parameters(struct isl_tab *tab,
2464 struct isl_tab *context_tab)
2466 int i;
2467 struct isl_tab_undo *snap;
2468 struct isl_vec *ineq = NULL;
2469 struct isl_tab_var *var;
2470 int n;
2472 if (context_tab->n_var == 0)
2473 return tab;
2475 ineq = isl_vec_alloc(tab->mat->ctx, 1 + context_tab->n_var);
2476 if (!ineq)
2477 goto error;
2479 if (isl_tab_extend_cons(context_tab, 1) < 0)
2480 goto error;
2482 snap = isl_tab_snap(context_tab);
2484 n = 0;
2485 isl_seq_clr(ineq->el, ineq->size);
2486 for (i = 0; i < context_tab->n_var; ++i) {
2487 isl_int_set_si(ineq->el[1 + i], 1);
2488 if (isl_tab_add_ineq(context_tab, ineq->el) < 0)
2489 goto error;
2490 var = &context_tab->con[context_tab->n_con - 1];
2491 if (!context_tab->empty &&
2492 !isl_tab_min_at_most_neg_one(context_tab, var)) {
2493 int j = i;
2494 if (i >= tab->n_param)
2495 j = i - tab->n_param + tab->n_var - tab->n_div;
2496 tab->var[j].is_nonneg = 1;
2497 n++;
2499 isl_int_set_si(ineq->el[1 + i], 0);
2500 if (isl_tab_rollback(context_tab, snap) < 0)
2501 goto error;
2504 if (context_tab->M && n == context_tab->n_var) {
2505 context_tab->mat = isl_mat_drop_cols(context_tab->mat, 2, 1);
2506 context_tab->M = 0;
2509 isl_vec_free(ineq);
2510 return tab;
2511 error:
2512 isl_vec_free(ineq);
2513 isl_tab_free(tab);
2514 return NULL;
2517 static struct isl_tab *context_lex_detect_nonnegative_parameters(
2518 struct isl_context *context, struct isl_tab *tab)
2520 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2521 struct isl_tab_undo *snap;
2523 if (!tab)
2524 return NULL;
2526 snap = isl_tab_snap(clex->tab);
2527 if (isl_tab_push_basis(clex->tab) < 0)
2528 goto error;
2530 tab = tab_detect_nonnegative_parameters(tab, clex->tab);
2532 if (isl_tab_rollback(clex->tab, snap) < 0)
2533 goto error;
2535 return tab;
2536 error:
2537 isl_tab_free(tab);
2538 return NULL;
2541 static void context_lex_invalidate(struct isl_context *context)
2543 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2544 isl_tab_free(clex->tab);
2545 clex->tab = NULL;
2548 static void context_lex_free(struct isl_context *context)
2550 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2551 isl_tab_free(clex->tab);
2552 free(clex);
2555 struct isl_context_op isl_context_lex_op = {
2556 context_lex_detect_nonnegative_parameters,
2557 context_lex_peek_basic_set,
2558 context_lex_peek_tab,
2559 context_lex_add_eq,
2560 context_lex_add_ineq,
2561 context_lex_ineq_sign,
2562 context_lex_test_ineq,
2563 context_lex_get_div,
2564 context_lex_add_div,
2565 context_lex_detect_equalities,
2566 context_lex_best_split,
2567 context_lex_is_empty,
2568 context_lex_is_ok,
2569 context_lex_save,
2570 context_lex_restore,
2571 context_lex_discard,
2572 context_lex_invalidate,
2573 context_lex_free,
2576 static struct isl_tab *context_tab_for_lexmin(struct isl_basic_set *bset)
2578 struct isl_tab *tab;
2580 if (!bset)
2581 return NULL;
2582 tab = tab_for_lexmin((struct isl_basic_map *)bset, NULL, 1, 0);
2583 if (!tab)
2584 goto error;
2585 if (isl_tab_track_bset(tab, bset) < 0)
2586 goto error;
2587 tab = isl_tab_init_samples(tab);
2588 return tab;
2589 error:
2590 isl_basic_set_free(bset);
2591 return NULL;
2594 static struct isl_context *isl_context_lex_alloc(struct isl_basic_set *dom)
2596 struct isl_context_lex *clex;
2598 if (!dom)
2599 return NULL;
2601 clex = isl_alloc_type(dom->ctx, struct isl_context_lex);
2602 if (!clex)
2603 return NULL;
2605 clex->context.op = &isl_context_lex_op;
2607 clex->tab = context_tab_for_lexmin(isl_basic_set_copy(dom));
2608 if (restore_lexmin(clex->tab) < 0)
2609 goto error;
2610 clex->tab = check_integer_feasible(clex->tab);
2611 if (!clex->tab)
2612 goto error;
2614 return &clex->context;
2615 error:
2616 clex->context.op->free(&clex->context);
2617 return NULL;
2620 /* Representation of the context when using generalized basis reduction.
2622 * "shifted" contains the offsets of the unit hypercubes that lie inside the
2623 * context. Any rational point in "shifted" can therefore be rounded
2624 * up to an integer point in the context.
2625 * If the context is constrained by any equality, then "shifted" is not used
2626 * as it would be empty.
2628 struct isl_context_gbr {
2629 struct isl_context context;
2630 struct isl_tab *tab;
2631 struct isl_tab *shifted;
2632 struct isl_tab *cone;
2635 static struct isl_tab *context_gbr_detect_nonnegative_parameters(
2636 struct isl_context *context, struct isl_tab *tab)
2638 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2639 if (!tab)
2640 return NULL;
2641 return tab_detect_nonnegative_parameters(tab, cgbr->tab);
2644 static struct isl_basic_set *context_gbr_peek_basic_set(
2645 struct isl_context *context)
2647 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2648 if (!cgbr->tab)
2649 return NULL;
2650 return isl_tab_peek_bset(cgbr->tab);
2653 static struct isl_tab *context_gbr_peek_tab(struct isl_context *context)
2655 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2656 return cgbr->tab;
2659 /* Initialize the "shifted" tableau of the context, which
2660 * contains the constraints of the original tableau shifted
2661 * by the sum of all negative coefficients. This ensures
2662 * that any rational point in the shifted tableau can
2663 * be rounded up to yield an integer point in the original tableau.
2665 static void gbr_init_shifted(struct isl_context_gbr *cgbr)
2667 int i, j;
2668 struct isl_vec *cst;
2669 struct isl_basic_set *bset = isl_tab_peek_bset(cgbr->tab);
2670 unsigned dim = isl_basic_set_total_dim(bset);
2672 cst = isl_vec_alloc(cgbr->tab->mat->ctx, bset->n_ineq);
2673 if (!cst)
2674 return;
2676 for (i = 0; i < bset->n_ineq; ++i) {
2677 isl_int_set(cst->el[i], bset->ineq[i][0]);
2678 for (j = 0; j < dim; ++j) {
2679 if (!isl_int_is_neg(bset->ineq[i][1 + j]))
2680 continue;
2681 isl_int_add(bset->ineq[i][0], bset->ineq[i][0],
2682 bset->ineq[i][1 + j]);
2686 cgbr->shifted = isl_tab_from_basic_set(bset, 0);
2688 for (i = 0; i < bset->n_ineq; ++i)
2689 isl_int_set(bset->ineq[i][0], cst->el[i]);
2691 isl_vec_free(cst);
2694 /* Check if the shifted tableau is non-empty, and if so
2695 * use the sample point to construct an integer point
2696 * of the context tableau.
2698 static struct isl_vec *gbr_get_shifted_sample(struct isl_context_gbr *cgbr)
2700 struct isl_vec *sample;
2702 if (!cgbr->shifted)
2703 gbr_init_shifted(cgbr);
2704 if (!cgbr->shifted)
2705 return NULL;
2706 if (cgbr->shifted->empty)
2707 return isl_vec_alloc(cgbr->tab->mat->ctx, 0);
2709 sample = isl_tab_get_sample_value(cgbr->shifted);
2710 sample = isl_vec_ceil(sample);
2712 return sample;
2715 static struct isl_basic_set *drop_constant_terms(struct isl_basic_set *bset)
2717 int i;
2719 if (!bset)
2720 return NULL;
2722 for (i = 0; i < bset->n_eq; ++i)
2723 isl_int_set_si(bset->eq[i][0], 0);
2725 for (i = 0; i < bset->n_ineq; ++i)
2726 isl_int_set_si(bset->ineq[i][0], 0);
2728 return bset;
2731 static int use_shifted(struct isl_context_gbr *cgbr)
2733 if (!cgbr->tab)
2734 return 0;
2735 return cgbr->tab->bmap->n_eq == 0 && cgbr->tab->bmap->n_div == 0;
2738 static struct isl_vec *gbr_get_sample(struct isl_context_gbr *cgbr)
2740 struct isl_basic_set *bset;
2741 struct isl_basic_set *cone;
2743 if (isl_tab_sample_is_integer(cgbr->tab))
2744 return isl_tab_get_sample_value(cgbr->tab);
2746 if (use_shifted(cgbr)) {
2747 struct isl_vec *sample;
2749 sample = gbr_get_shifted_sample(cgbr);
2750 if (!sample || sample->size > 0)
2751 return sample;
2753 isl_vec_free(sample);
2756 if (!cgbr->cone) {
2757 bset = isl_tab_peek_bset(cgbr->tab);
2758 cgbr->cone = isl_tab_from_recession_cone(bset, 0);
2759 if (!cgbr->cone)
2760 return NULL;
2761 if (isl_tab_track_bset(cgbr->cone,
2762 isl_basic_set_copy(bset)) < 0)
2763 return NULL;
2765 if (isl_tab_detect_implicit_equalities(cgbr->cone) < 0)
2766 return NULL;
2768 if (cgbr->cone->n_dead == cgbr->cone->n_col) {
2769 struct isl_vec *sample;
2770 struct isl_tab_undo *snap;
2772 if (cgbr->tab->basis) {
2773 if (cgbr->tab->basis->n_col != 1 + cgbr->tab->n_var) {
2774 isl_mat_free(cgbr->tab->basis);
2775 cgbr->tab->basis = NULL;
2777 cgbr->tab->n_zero = 0;
2778 cgbr->tab->n_unbounded = 0;
2781 snap = isl_tab_snap(cgbr->tab);
2783 sample = isl_tab_sample(cgbr->tab);
2785 if (isl_tab_rollback(cgbr->tab, snap) < 0) {
2786 isl_vec_free(sample);
2787 return NULL;
2790 return sample;
2793 cone = isl_basic_set_dup(isl_tab_peek_bset(cgbr->cone));
2794 cone = drop_constant_terms(cone);
2795 cone = isl_basic_set_update_from_tab(cone, cgbr->cone);
2796 cone = isl_basic_set_underlying_set(cone);
2797 cone = isl_basic_set_gauss(cone, NULL);
2799 bset = isl_basic_set_dup(isl_tab_peek_bset(cgbr->tab));
2800 bset = isl_basic_set_update_from_tab(bset, cgbr->tab);
2801 bset = isl_basic_set_underlying_set(bset);
2802 bset = isl_basic_set_gauss(bset, NULL);
2804 return isl_basic_set_sample_with_cone(bset, cone);
2807 static void check_gbr_integer_feasible(struct isl_context_gbr *cgbr)
2809 struct isl_vec *sample;
2811 if (!cgbr->tab)
2812 return;
2814 if (cgbr->tab->empty)
2815 return;
2817 sample = gbr_get_sample(cgbr);
2818 if (!sample)
2819 goto error;
2821 if (sample->size == 0) {
2822 isl_vec_free(sample);
2823 if (isl_tab_mark_empty(cgbr->tab) < 0)
2824 goto error;
2825 return;
2828 cgbr->tab = isl_tab_add_sample(cgbr->tab, sample);
2830 return;
2831 error:
2832 isl_tab_free(cgbr->tab);
2833 cgbr->tab = NULL;
2836 static struct isl_tab *add_gbr_eq(struct isl_tab *tab, isl_int *eq)
2838 if (!tab)
2839 return NULL;
2841 if (isl_tab_extend_cons(tab, 2) < 0)
2842 goto error;
2844 if (isl_tab_add_eq(tab, eq) < 0)
2845 goto error;
2847 return tab;
2848 error:
2849 isl_tab_free(tab);
2850 return NULL;
2853 /* Add the equality described by "eq" to the context.
2854 * If "check" is set, then we check if the context is empty after
2855 * adding the equality.
2856 * If "update" is set, then we check if the samples are still valid.
2858 * We do not explicitly add shifted copies of the equality to
2859 * cgbr->shifted since they would conflict with each other.
2860 * Instead, we directly mark cgbr->shifted empty.
2862 static void context_gbr_add_eq(struct isl_context *context, isl_int *eq,
2863 int check, int update)
2865 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2867 cgbr->tab = add_gbr_eq(cgbr->tab, eq);
2869 if (cgbr->shifted && !cgbr->shifted->empty && use_shifted(cgbr)) {
2870 if (isl_tab_mark_empty(cgbr->shifted) < 0)
2871 goto error;
2874 if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
2875 if (isl_tab_extend_cons(cgbr->cone, 2) < 0)
2876 goto error;
2877 if (isl_tab_add_eq(cgbr->cone, eq) < 0)
2878 goto error;
2881 if (check) {
2882 int v = tab_has_valid_sample(cgbr->tab, eq, 1);
2883 if (v < 0)
2884 goto error;
2885 if (!v)
2886 check_gbr_integer_feasible(cgbr);
2888 if (update)
2889 cgbr->tab = check_samples(cgbr->tab, eq, 1);
2890 return;
2891 error:
2892 isl_tab_free(cgbr->tab);
2893 cgbr->tab = NULL;
2896 static void add_gbr_ineq(struct isl_context_gbr *cgbr, isl_int *ineq)
2898 if (!cgbr->tab)
2899 return;
2901 if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
2902 goto error;
2904 if (isl_tab_add_ineq(cgbr->tab, ineq) < 0)
2905 goto error;
2907 if (cgbr->shifted && !cgbr->shifted->empty && use_shifted(cgbr)) {
2908 int i;
2909 unsigned dim;
2910 dim = isl_basic_map_total_dim(cgbr->tab->bmap);
2912 if (isl_tab_extend_cons(cgbr->shifted, 1) < 0)
2913 goto error;
2915 for (i = 0; i < dim; ++i) {
2916 if (!isl_int_is_neg(ineq[1 + i]))
2917 continue;
2918 isl_int_add(ineq[0], ineq[0], ineq[1 + i]);
2921 if (isl_tab_add_ineq(cgbr->shifted, ineq) < 0)
2922 goto error;
2924 for (i = 0; i < dim; ++i) {
2925 if (!isl_int_is_neg(ineq[1 + i]))
2926 continue;
2927 isl_int_sub(ineq[0], ineq[0], ineq[1 + i]);
2931 if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
2932 if (isl_tab_extend_cons(cgbr->cone, 1) < 0)
2933 goto error;
2934 if (isl_tab_add_ineq(cgbr->cone, ineq) < 0)
2935 goto error;
2938 return;
2939 error:
2940 isl_tab_free(cgbr->tab);
2941 cgbr->tab = NULL;
2944 static void context_gbr_add_ineq(struct isl_context *context, isl_int *ineq,
2945 int check, int update)
2947 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2949 add_gbr_ineq(cgbr, ineq);
2950 if (!cgbr->tab)
2951 return;
2953 if (check) {
2954 int v = tab_has_valid_sample(cgbr->tab, ineq, 0);
2955 if (v < 0)
2956 goto error;
2957 if (!v)
2958 check_gbr_integer_feasible(cgbr);
2960 if (update)
2961 cgbr->tab = check_samples(cgbr->tab, ineq, 0);
2962 return;
2963 error:
2964 isl_tab_free(cgbr->tab);
2965 cgbr->tab = NULL;
2968 static int context_gbr_add_ineq_wrap(void *user, isl_int *ineq)
2970 struct isl_context *context = (struct isl_context *)user;
2971 context_gbr_add_ineq(context, ineq, 0, 0);
2972 return context->op->is_ok(context) ? 0 : -1;
2975 static enum isl_tab_row_sign context_gbr_ineq_sign(struct isl_context *context,
2976 isl_int *ineq, int strict)
2978 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2979 return tab_ineq_sign(cgbr->tab, ineq, strict);
2982 /* Check whether "ineq" can be added to the tableau without rendering
2983 * it infeasible.
2985 static int context_gbr_test_ineq(struct isl_context *context, isl_int *ineq)
2987 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2988 struct isl_tab_undo *snap;
2989 struct isl_tab_undo *shifted_snap = NULL;
2990 struct isl_tab_undo *cone_snap = NULL;
2991 int feasible;
2993 if (!cgbr->tab)
2994 return -1;
2996 if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
2997 return -1;
2999 snap = isl_tab_snap(cgbr->tab);
3000 if (cgbr->shifted)
3001 shifted_snap = isl_tab_snap(cgbr->shifted);
3002 if (cgbr->cone)
3003 cone_snap = isl_tab_snap(cgbr->cone);
3004 add_gbr_ineq(cgbr, ineq);
3005 check_gbr_integer_feasible(cgbr);
3006 if (!cgbr->tab)
3007 return -1;
3008 feasible = !cgbr->tab->empty;
3009 if (isl_tab_rollback(cgbr->tab, snap) < 0)
3010 return -1;
3011 if (shifted_snap) {
3012 if (isl_tab_rollback(cgbr->shifted, shifted_snap))
3013 return -1;
3014 } else if (cgbr->shifted) {
3015 isl_tab_free(cgbr->shifted);
3016 cgbr->shifted = NULL;
3018 if (cone_snap) {
3019 if (isl_tab_rollback(cgbr->cone, cone_snap))
3020 return -1;
3021 } else if (cgbr->cone) {
3022 isl_tab_free(cgbr->cone);
3023 cgbr->cone = NULL;
3026 return feasible;
3029 /* Return the column of the last of the variables associated to
3030 * a column that has a non-zero coefficient.
3031 * This function is called in a context where only coefficients
3032 * of parameters or divs can be non-zero.
3034 static int last_non_zero_var_col(struct isl_tab *tab, isl_int *p)
3036 int i;
3037 int col;
3039 if (tab->n_var == 0)
3040 return -1;
3042 for (i = tab->n_var - 1; i >= 0; --i) {
3043 if (i >= tab->n_param && i < tab->n_var - tab->n_div)
3044 continue;
3045 if (tab->var[i].is_row)
3046 continue;
3047 col = tab->var[i].index;
3048 if (!isl_int_is_zero(p[col]))
3049 return col;
3052 return -1;
3055 /* Look through all the recently added equalities in the context
3056 * to see if we can propagate any of them to the main tableau.
3058 * The newly added equalities in the context are encoded as pairs
3059 * of inequalities starting at inequality "first".
3061 * We tentatively add each of these equalities to the main tableau
3062 * and if this happens to result in a row with a final coefficient
3063 * that is one or negative one, we use it to kill a column
3064 * in the main tableau. Otherwise, we discard the tentatively
3065 * added row.
3067 static void propagate_equalities(struct isl_context_gbr *cgbr,
3068 struct isl_tab *tab, unsigned first)
3070 int i;
3071 struct isl_vec *eq = NULL;
3073 eq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
3074 if (!eq)
3075 goto error;
3077 if (isl_tab_extend_cons(tab, (cgbr->tab->bmap->n_ineq - first)/2) < 0)
3078 goto error;
3080 isl_seq_clr(eq->el + 1 + tab->n_param,
3081 tab->n_var - tab->n_param - tab->n_div);
3082 for (i = first; i < cgbr->tab->bmap->n_ineq; i += 2) {
3083 int j;
3084 int r;
3085 struct isl_tab_undo *snap;
3086 snap = isl_tab_snap(tab);
3088 isl_seq_cpy(eq->el, cgbr->tab->bmap->ineq[i], 1 + tab->n_param);
3089 isl_seq_cpy(eq->el + 1 + tab->n_var - tab->n_div,
3090 cgbr->tab->bmap->ineq[i] + 1 + tab->n_param,
3091 tab->n_div);
3093 r = isl_tab_add_row(tab, eq->el);
3094 if (r < 0)
3095 goto error;
3096 r = tab->con[r].index;
3097 j = last_non_zero_var_col(tab, tab->mat->row[r] + 2 + tab->M);
3098 if (j < 0 || j < tab->n_dead ||
3099 !isl_int_is_one(tab->mat->row[r][0]) ||
3100 (!isl_int_is_one(tab->mat->row[r][2 + tab->M + j]) &&
3101 !isl_int_is_negone(tab->mat->row[r][2 + tab->M + j]))) {
3102 if (isl_tab_rollback(tab, snap) < 0)
3103 goto error;
3104 continue;
3106 if (isl_tab_pivot(tab, r, j) < 0)
3107 goto error;
3108 if (isl_tab_kill_col(tab, j) < 0)
3109 goto error;
3111 if (restore_lexmin(tab) < 0)
3112 goto error;
3115 isl_vec_free(eq);
3117 return;
3118 error:
3119 isl_vec_free(eq);
3120 isl_tab_free(cgbr->tab);
3121 cgbr->tab = NULL;
3124 static int context_gbr_detect_equalities(struct isl_context *context,
3125 struct isl_tab *tab)
3127 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3128 struct isl_ctx *ctx;
3129 unsigned n_ineq;
3131 ctx = cgbr->tab->mat->ctx;
3133 if (!cgbr->cone) {
3134 struct isl_basic_set *bset = isl_tab_peek_bset(cgbr->tab);
3135 cgbr->cone = isl_tab_from_recession_cone(bset, 0);
3136 if (!cgbr->cone)
3137 goto error;
3138 if (isl_tab_track_bset(cgbr->cone,
3139 isl_basic_set_copy(bset)) < 0)
3140 goto error;
3142 if (isl_tab_detect_implicit_equalities(cgbr->cone) < 0)
3143 goto error;
3145 n_ineq = cgbr->tab->bmap->n_ineq;
3146 cgbr->tab = isl_tab_detect_equalities(cgbr->tab, cgbr->cone);
3147 if (!cgbr->tab)
3148 return -1;
3149 if (cgbr->tab->bmap->n_ineq > n_ineq)
3150 propagate_equalities(cgbr, tab, n_ineq);
3152 return 0;
3153 error:
3154 isl_tab_free(cgbr->tab);
3155 cgbr->tab = NULL;
3156 return -1;
3159 static int context_gbr_get_div(struct isl_context *context, struct isl_tab *tab,
3160 struct isl_vec *div)
3162 return get_div(tab, context, div);
3165 static int context_gbr_add_div(struct isl_context *context, struct isl_vec *div)
3167 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3168 if (cgbr->cone) {
3169 int k;
3171 if (isl_tab_extend_cons(cgbr->cone, 3) < 0)
3172 return -1;
3173 if (isl_tab_extend_vars(cgbr->cone, 1) < 0)
3174 return -1;
3175 if (isl_tab_allocate_var(cgbr->cone) <0)
3176 return -1;
3178 cgbr->cone->bmap = isl_basic_map_extend_space(cgbr->cone->bmap,
3179 isl_basic_map_get_space(cgbr->cone->bmap), 1, 0, 2);
3180 k = isl_basic_map_alloc_div(cgbr->cone->bmap);
3181 if (k < 0)
3182 return -1;
3183 isl_seq_cpy(cgbr->cone->bmap->div[k], div->el, div->size);
3184 if (isl_tab_push(cgbr->cone, isl_tab_undo_bmap_div) < 0)
3185 return -1;
3187 return context_tab_add_div(cgbr->tab, div,
3188 context_gbr_add_ineq_wrap, context);
3191 static int context_gbr_best_split(struct isl_context *context,
3192 struct isl_tab *tab)
3194 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3195 struct isl_tab_undo *snap;
3196 int r;
3198 snap = isl_tab_snap(cgbr->tab);
3199 r = best_split(tab, cgbr->tab);
3201 if (r >= 0 && isl_tab_rollback(cgbr->tab, snap) < 0)
3202 return -1;
3204 return r;
3207 static int context_gbr_is_empty(struct isl_context *context)
3209 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3210 if (!cgbr->tab)
3211 return -1;
3212 return cgbr->tab->empty;
3215 struct isl_gbr_tab_undo {
3216 struct isl_tab_undo *tab_snap;
3217 struct isl_tab_undo *shifted_snap;
3218 struct isl_tab_undo *cone_snap;
3221 static void *context_gbr_save(struct isl_context *context)
3223 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3224 struct isl_gbr_tab_undo *snap;
3226 if (!cgbr->tab)
3227 return NULL;
3229 snap = isl_alloc_type(cgbr->tab->mat->ctx, struct isl_gbr_tab_undo);
3230 if (!snap)
3231 return NULL;
3233 snap->tab_snap = isl_tab_snap(cgbr->tab);
3234 if (isl_tab_save_samples(cgbr->tab) < 0)
3235 goto error;
3237 if (cgbr->shifted)
3238 snap->shifted_snap = isl_tab_snap(cgbr->shifted);
3239 else
3240 snap->shifted_snap = NULL;
3242 if (cgbr->cone)
3243 snap->cone_snap = isl_tab_snap(cgbr->cone);
3244 else
3245 snap->cone_snap = NULL;
3247 return snap;
3248 error:
3249 free(snap);
3250 return NULL;
3253 static void context_gbr_restore(struct isl_context *context, void *save)
3255 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3256 struct isl_gbr_tab_undo *snap = (struct isl_gbr_tab_undo *)save;
3257 if (!snap)
3258 goto error;
3259 if (isl_tab_rollback(cgbr->tab, snap->tab_snap) < 0) {
3260 isl_tab_free(cgbr->tab);
3261 cgbr->tab = NULL;
3264 if (snap->shifted_snap) {
3265 if (isl_tab_rollback(cgbr->shifted, snap->shifted_snap) < 0)
3266 goto error;
3267 } else if (cgbr->shifted) {
3268 isl_tab_free(cgbr->shifted);
3269 cgbr->shifted = NULL;
3272 if (snap->cone_snap) {
3273 if (isl_tab_rollback(cgbr->cone, snap->cone_snap) < 0)
3274 goto error;
3275 } else if (cgbr->cone) {
3276 isl_tab_free(cgbr->cone);
3277 cgbr->cone = NULL;
3280 free(snap);
3282 return;
3283 error:
3284 free(snap);
3285 isl_tab_free(cgbr->tab);
3286 cgbr->tab = NULL;
3289 static void context_gbr_discard(void *save)
3291 struct isl_gbr_tab_undo *snap = (struct isl_gbr_tab_undo *)save;
3292 free(snap);
3295 static int context_gbr_is_ok(struct isl_context *context)
3297 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3298 return !!cgbr->tab;
3301 static void context_gbr_invalidate(struct isl_context *context)
3303 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3304 isl_tab_free(cgbr->tab);
3305 cgbr->tab = NULL;
3308 static void context_gbr_free(struct isl_context *context)
3310 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3311 isl_tab_free(cgbr->tab);
3312 isl_tab_free(cgbr->shifted);
3313 isl_tab_free(cgbr->cone);
3314 free(cgbr);
3317 struct isl_context_op isl_context_gbr_op = {
3318 context_gbr_detect_nonnegative_parameters,
3319 context_gbr_peek_basic_set,
3320 context_gbr_peek_tab,
3321 context_gbr_add_eq,
3322 context_gbr_add_ineq,
3323 context_gbr_ineq_sign,
3324 context_gbr_test_ineq,
3325 context_gbr_get_div,
3326 context_gbr_add_div,
3327 context_gbr_detect_equalities,
3328 context_gbr_best_split,
3329 context_gbr_is_empty,
3330 context_gbr_is_ok,
3331 context_gbr_save,
3332 context_gbr_restore,
3333 context_gbr_discard,
3334 context_gbr_invalidate,
3335 context_gbr_free,
3338 static struct isl_context *isl_context_gbr_alloc(struct isl_basic_set *dom)
3340 struct isl_context_gbr *cgbr;
3342 if (!dom)
3343 return NULL;
3345 cgbr = isl_calloc_type(dom->ctx, struct isl_context_gbr);
3346 if (!cgbr)
3347 return NULL;
3349 cgbr->context.op = &isl_context_gbr_op;
3351 cgbr->shifted = NULL;
3352 cgbr->cone = NULL;
3353 cgbr->tab = isl_tab_from_basic_set(dom, 1);
3354 cgbr->tab = isl_tab_init_samples(cgbr->tab);
3355 if (!cgbr->tab)
3356 goto error;
3357 check_gbr_integer_feasible(cgbr);
3359 return &cgbr->context;
3360 error:
3361 cgbr->context.op->free(&cgbr->context);
3362 return NULL;
3365 static struct isl_context *isl_context_alloc(struct isl_basic_set *dom)
3367 if (!dom)
3368 return NULL;
3370 if (dom->ctx->opt->context == ISL_CONTEXT_LEXMIN)
3371 return isl_context_lex_alloc(dom);
3372 else
3373 return isl_context_gbr_alloc(dom);
3376 /* Construct an isl_sol_map structure for accumulating the solution.
3377 * If track_empty is set, then we also keep track of the parts
3378 * of the context where there is no solution.
3379 * If max is set, then we are solving a maximization, rather than
3380 * a minimization problem, which means that the variables in the
3381 * tableau have value "M - x" rather than "M + x".
3383 static struct isl_sol *sol_map_init(struct isl_basic_map *bmap,
3384 struct isl_basic_set *dom, int track_empty, int max)
3386 struct isl_sol_map *sol_map = NULL;
3388 if (!bmap)
3389 goto error;
3391 sol_map = isl_calloc_type(bmap->ctx, struct isl_sol_map);
3392 if (!sol_map)
3393 goto error;
3395 sol_map->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
3396 sol_map->sol.dec_level.callback.run = &sol_dec_level_wrap;
3397 sol_map->sol.dec_level.sol = &sol_map->sol;
3398 sol_map->sol.max = max;
3399 sol_map->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
3400 sol_map->sol.add = &sol_map_add_wrap;
3401 sol_map->sol.add_empty = track_empty ? &sol_map_add_empty_wrap : NULL;
3402 sol_map->sol.free = &sol_map_free_wrap;
3403 sol_map->map = isl_map_alloc_space(isl_basic_map_get_space(bmap), 1,
3404 ISL_MAP_DISJOINT);
3405 if (!sol_map->map)
3406 goto error;
3408 sol_map->sol.context = isl_context_alloc(dom);
3409 if (!sol_map->sol.context)
3410 goto error;
3412 if (track_empty) {
3413 sol_map->empty = isl_set_alloc_space(isl_basic_set_get_space(dom),
3414 1, ISL_SET_DISJOINT);
3415 if (!sol_map->empty)
3416 goto error;
3419 isl_basic_set_free(dom);
3420 return &sol_map->sol;
3421 error:
3422 isl_basic_set_free(dom);
3423 sol_map_free(sol_map);
3424 return NULL;
3427 /* Check whether all coefficients of (non-parameter) variables
3428 * are non-positive, meaning that no pivots can be performed on the row.
3430 static int is_critical(struct isl_tab *tab, int row)
3432 int j;
3433 unsigned off = 2 + tab->M;
3435 for (j = tab->n_dead; j < tab->n_col; ++j) {
3436 if (tab->col_var[j] >= 0 &&
3437 (tab->col_var[j] < tab->n_param ||
3438 tab->col_var[j] >= tab->n_var - tab->n_div))
3439 continue;
3441 if (isl_int_is_pos(tab->mat->row[row][off + j]))
3442 return 0;
3445 return 1;
3448 /* Check whether the inequality represented by vec is strict over the integers,
3449 * i.e., there are no integer values satisfying the constraint with
3450 * equality. This happens if the gcd of the coefficients is not a divisor
3451 * of the constant term. If so, scale the constraint down by the gcd
3452 * of the coefficients.
3454 static int is_strict(struct isl_vec *vec)
3456 isl_int gcd;
3457 int strict = 0;
3459 isl_int_init(gcd);
3460 isl_seq_gcd(vec->el + 1, vec->size - 1, &gcd);
3461 if (!isl_int_is_one(gcd)) {
3462 strict = !isl_int_is_divisible_by(vec->el[0], gcd);
3463 isl_int_fdiv_q(vec->el[0], vec->el[0], gcd);
3464 isl_seq_scale_down(vec->el + 1, vec->el + 1, gcd, vec->size-1);
3466 isl_int_clear(gcd);
3468 return strict;
3471 /* Determine the sign of the given row of the main tableau.
3472 * The result is one of
3473 * isl_tab_row_pos: always non-negative; no pivot needed
3474 * isl_tab_row_neg: always non-positive; pivot
3475 * isl_tab_row_any: can be both positive and negative; split
3477 * We first handle some simple cases
3478 * - the row sign may be known already
3479 * - the row may be obviously non-negative
3480 * - the parametric constant may be equal to that of another row
3481 * for which we know the sign. This sign will be either "pos" or
3482 * "any". If it had been "neg" then we would have pivoted before.
3484 * If none of these cases hold, we check the value of the row for each
3485 * of the currently active samples. Based on the signs of these values
3486 * we make an initial determination of the sign of the row.
3488 * all zero -> unk(nown)
3489 * all non-negative -> pos
3490 * all non-positive -> neg
3491 * both negative and positive -> all
3493 * If we end up with "all", we are done.
3494 * Otherwise, we perform a check for positive and/or negative
3495 * values as follows.
3497 * samples neg unk pos
3498 * <0 ? Y N Y N
3499 * pos any pos
3500 * >0 ? Y N Y N
3501 * any neg any neg
3503 * There is no special sign for "zero", because we can usually treat zero
3504 * as either non-negative or non-positive, whatever works out best.
3505 * However, if the row is "critical", meaning that pivoting is impossible
3506 * then we don't want to limp zero with the non-positive case, because
3507 * then we we would lose the solution for those values of the parameters
3508 * where the value of the row is zero. Instead, we treat 0 as non-negative
3509 * ensuring a split if the row can attain both zero and negative values.
3510 * The same happens when the original constraint was one that could not
3511 * be satisfied with equality by any integer values of the parameters.
3512 * In this case, we normalize the constraint, but then a value of zero
3513 * for the normalized constraint is actually a positive value for the
3514 * original constraint, so again we need to treat zero as non-negative.
3515 * In both these cases, we have the following decision tree instead:
3517 * all non-negative -> pos
3518 * all negative -> neg
3519 * both negative and non-negative -> all
3521 * samples neg pos
3522 * <0 ? Y N
3523 * any pos
3524 * >=0 ? Y N
3525 * any neg
3527 static enum isl_tab_row_sign row_sign(struct isl_tab *tab,
3528 struct isl_sol *sol, int row)
3530 struct isl_vec *ineq = NULL;
3531 enum isl_tab_row_sign res = isl_tab_row_unknown;
3532 int critical;
3533 int strict;
3534 int row2;
3536 if (tab->row_sign[row] != isl_tab_row_unknown)
3537 return tab->row_sign[row];
3538 if (is_obviously_nonneg(tab, row))
3539 return isl_tab_row_pos;
3540 for (row2 = tab->n_redundant; row2 < tab->n_row; ++row2) {
3541 if (tab->row_sign[row2] == isl_tab_row_unknown)
3542 continue;
3543 if (identical_parameter_line(tab, row, row2))
3544 return tab->row_sign[row2];
3547 critical = is_critical(tab, row);
3549 ineq = get_row_parameter_ineq(tab, row);
3550 if (!ineq)
3551 goto error;
3553 strict = is_strict(ineq);
3555 res = sol->context->op->ineq_sign(sol->context, ineq->el,
3556 critical || strict);
3558 if (res == isl_tab_row_unknown || res == isl_tab_row_pos) {
3559 /* test for negative values */
3560 int feasible;
3561 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3562 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3564 feasible = sol->context->op->test_ineq(sol->context, ineq->el);
3565 if (feasible < 0)
3566 goto error;
3567 if (!feasible)
3568 res = isl_tab_row_pos;
3569 else
3570 res = (res == isl_tab_row_unknown) ? isl_tab_row_neg
3571 : isl_tab_row_any;
3572 if (res == isl_tab_row_neg) {
3573 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3574 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3578 if (res == isl_tab_row_neg) {
3579 /* test for positive values */
3580 int feasible;
3581 if (!critical && !strict)
3582 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3584 feasible = sol->context->op->test_ineq(sol->context, ineq->el);
3585 if (feasible < 0)
3586 goto error;
3587 if (feasible)
3588 res = isl_tab_row_any;
3591 isl_vec_free(ineq);
3592 return res;
3593 error:
3594 isl_vec_free(ineq);
3595 return isl_tab_row_unknown;
3598 static void find_solutions(struct isl_sol *sol, struct isl_tab *tab);
3600 /* Find solutions for values of the parameters that satisfy the given
3601 * inequality.
3603 * We currently take a snapshot of the context tableau that is reset
3604 * when we return from this function, while we make a copy of the main
3605 * tableau, leaving the original main tableau untouched.
3606 * These are fairly arbitrary choices. Making a copy also of the context
3607 * tableau would obviate the need to undo any changes made to it later,
3608 * while taking a snapshot of the main tableau could reduce memory usage.
3609 * If we were to switch to taking a snapshot of the main tableau,
3610 * we would have to keep in mind that we need to save the row signs
3611 * and that we need to do this before saving the current basis
3612 * such that the basis has been restore before we restore the row signs.
3614 static void find_in_pos(struct isl_sol *sol, struct isl_tab *tab, isl_int *ineq)
3616 void *saved;
3618 if (!sol->context)
3619 goto error;
3620 saved = sol->context->op->save(sol->context);
3622 tab = isl_tab_dup(tab);
3623 if (!tab)
3624 goto error;
3626 sol->context->op->add_ineq(sol->context, ineq, 0, 1);
3628 find_solutions(sol, tab);
3630 if (!sol->error)
3631 sol->context->op->restore(sol->context, saved);
3632 else
3633 sol->context->op->discard(saved);
3634 return;
3635 error:
3636 sol->error = 1;
3639 /* Record the absence of solutions for those values of the parameters
3640 * that do not satisfy the given inequality with equality.
3642 static void no_sol_in_strict(struct isl_sol *sol,
3643 struct isl_tab *tab, struct isl_vec *ineq)
3645 int empty;
3646 void *saved;
3648 if (!sol->context || sol->error)
3649 goto error;
3650 saved = sol->context->op->save(sol->context);
3652 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3654 sol->context->op->add_ineq(sol->context, ineq->el, 1, 0);
3655 if (!sol->context)
3656 goto error;
3658 empty = tab->empty;
3659 tab->empty = 1;
3660 sol_add(sol, tab);
3661 tab->empty = empty;
3663 isl_int_add_ui(ineq->el[0], ineq->el[0], 1);
3665 sol->context->op->restore(sol->context, saved);
3666 return;
3667 error:
3668 sol->error = 1;
3671 /* Compute the lexicographic minimum of the set represented by the main
3672 * tableau "tab" within the context "sol->context_tab".
3673 * On entry the sample value of the main tableau is lexicographically
3674 * less than or equal to this lexicographic minimum.
3675 * Pivots are performed until a feasible point is found, which is then
3676 * necessarily equal to the minimum, or until the tableau is found to
3677 * be infeasible. Some pivots may need to be performed for only some
3678 * feasible values of the context tableau. If so, the context tableau
3679 * is split into a part where the pivot is needed and a part where it is not.
3681 * Whenever we enter the main loop, the main tableau is such that no
3682 * "obvious" pivots need to be performed on it, where "obvious" means
3683 * that the given row can be seen to be negative without looking at
3684 * the context tableau. In particular, for non-parametric problems,
3685 * no pivots need to be performed on the main tableau.
3686 * The caller of find_solutions is responsible for making this property
3687 * hold prior to the first iteration of the loop, while restore_lexmin
3688 * is called before every other iteration.
3690 * Inside the main loop, we first examine the signs of the rows of
3691 * the main tableau within the context of the context tableau.
3692 * If we find a row that is always non-positive for all values of
3693 * the parameters satisfying the context tableau and negative for at
3694 * least one value of the parameters, we perform the appropriate pivot
3695 * and start over. An exception is the case where no pivot can be
3696 * performed on the row. In this case, we require that the sign of
3697 * the row is negative for all values of the parameters (rather than just
3698 * non-positive). This special case is handled inside row_sign, which
3699 * will say that the row can have any sign if it determines that it can
3700 * attain both negative and zero values.
3702 * If we can't find a row that always requires a pivot, but we can find
3703 * one or more rows that require a pivot for some values of the parameters
3704 * (i.e., the row can attain both positive and negative signs), then we split
3705 * the context tableau into two parts, one where we force the sign to be
3706 * non-negative and one where we force is to be negative.
3707 * The non-negative part is handled by a recursive call (through find_in_pos).
3708 * Upon returning from this call, we continue with the negative part and
3709 * perform the required pivot.
3711 * If no such rows can be found, all rows are non-negative and we have
3712 * found a (rational) feasible point. If we only wanted a rational point
3713 * then we are done.
3714 * Otherwise, we check if all values of the sample point of the tableau
3715 * are integral for the variables. If so, we have found the minimal
3716 * integral point and we are done.
3717 * If the sample point is not integral, then we need to make a distinction
3718 * based on whether the constant term is non-integral or the coefficients
3719 * of the parameters. Furthermore, in order to decide how to handle
3720 * the non-integrality, we also need to know whether the coefficients
3721 * of the other columns in the tableau are integral. This leads
3722 * to the following table. The first two rows do not correspond
3723 * to a non-integral sample point and are only mentioned for completeness.
3725 * constant parameters other
3727 * int int int |
3728 * int int rat | -> no problem
3730 * rat int int -> fail
3732 * rat int rat -> cut
3734 * int rat rat |
3735 * rat rat rat | -> parametric cut
3737 * int rat int |
3738 * rat rat int | -> split context
3740 * If the parametric constant is completely integral, then there is nothing
3741 * to be done. If the constant term is non-integral, but all the other
3742 * coefficient are integral, then there is nothing that can be done
3743 * and the tableau has no integral solution.
3744 * If, on the other hand, one or more of the other columns have rational
3745 * coefficients, but the parameter coefficients are all integral, then
3746 * we can perform a regular (non-parametric) cut.
3747 * Finally, if there is any parameter coefficient that is non-integral,
3748 * then we need to involve the context tableau. There are two cases here.
3749 * If at least one other column has a rational coefficient, then we
3750 * can perform a parametric cut in the main tableau by adding a new
3751 * integer division in the context tableau.
3752 * If all other columns have integral coefficients, then we need to
3753 * enforce that the rational combination of parameters (c + \sum a_i y_i)/m
3754 * is always integral. We do this by introducing an integer division
3755 * q = floor((c + \sum a_i y_i)/m) and stipulating that its argument should
3756 * always be integral in the context tableau, i.e., m q = c + \sum a_i y_i.
3757 * Since q is expressed in the tableau as
3758 * c + \sum a_i y_i - m q >= 0
3759 * -c - \sum a_i y_i + m q + m - 1 >= 0
3760 * it is sufficient to add the inequality
3761 * -c - \sum a_i y_i + m q >= 0
3762 * In the part of the context where this inequality does not hold, the
3763 * main tableau is marked as being empty.
3765 static void find_solutions(struct isl_sol *sol, struct isl_tab *tab)
3767 struct isl_context *context;
3768 int r;
3770 if (!tab || sol->error)
3771 goto error;
3773 context = sol->context;
3775 if (tab->empty)
3776 goto done;
3777 if (context->op->is_empty(context))
3778 goto done;
3780 for (r = 0; r >= 0 && tab && !tab->empty; r = restore_lexmin(tab)) {
3781 int flags;
3782 int row;
3783 enum isl_tab_row_sign sgn;
3784 int split = -1;
3785 int n_split = 0;
3787 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3788 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
3789 continue;
3790 sgn = row_sign(tab, sol, row);
3791 if (!sgn)
3792 goto error;
3793 tab->row_sign[row] = sgn;
3794 if (sgn == isl_tab_row_any)
3795 n_split++;
3796 if (sgn == isl_tab_row_any && split == -1)
3797 split = row;
3798 if (sgn == isl_tab_row_neg)
3799 break;
3801 if (row < tab->n_row)
3802 continue;
3803 if (split != -1) {
3804 struct isl_vec *ineq;
3805 if (n_split != 1)
3806 split = context->op->best_split(context, tab);
3807 if (split < 0)
3808 goto error;
3809 ineq = get_row_parameter_ineq(tab, split);
3810 if (!ineq)
3811 goto error;
3812 is_strict(ineq);
3813 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3814 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
3815 continue;
3816 if (tab->row_sign[row] == isl_tab_row_any)
3817 tab->row_sign[row] = isl_tab_row_unknown;
3819 tab->row_sign[split] = isl_tab_row_pos;
3820 sol_inc_level(sol);
3821 find_in_pos(sol, tab, ineq->el);
3822 tab->row_sign[split] = isl_tab_row_neg;
3823 row = split;
3824 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3825 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3826 if (!sol->error)
3827 context->op->add_ineq(context, ineq->el, 0, 1);
3828 isl_vec_free(ineq);
3829 if (sol->error)
3830 goto error;
3831 continue;
3833 if (tab->rational)
3834 break;
3835 row = first_non_integer_row(tab, &flags);
3836 if (row < 0)
3837 break;
3838 if (ISL_FL_ISSET(flags, I_PAR)) {
3839 if (ISL_FL_ISSET(flags, I_VAR)) {
3840 if (isl_tab_mark_empty(tab) < 0)
3841 goto error;
3842 break;
3844 row = add_cut(tab, row);
3845 } else if (ISL_FL_ISSET(flags, I_VAR)) {
3846 struct isl_vec *div;
3847 struct isl_vec *ineq;
3848 int d;
3849 div = get_row_split_div(tab, row);
3850 if (!div)
3851 goto error;
3852 d = context->op->get_div(context, tab, div);
3853 isl_vec_free(div);
3854 if (d < 0)
3855 goto error;
3856 ineq = ineq_for_div(context->op->peek_basic_set(context), d);
3857 if (!ineq)
3858 goto error;
3859 sol_inc_level(sol);
3860 no_sol_in_strict(sol, tab, ineq);
3861 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3862 context->op->add_ineq(context, ineq->el, 1, 1);
3863 isl_vec_free(ineq);
3864 if (sol->error || !context->op->is_ok(context))
3865 goto error;
3866 tab = set_row_cst_to_div(tab, row, d);
3867 if (context->op->is_empty(context))
3868 break;
3869 } else
3870 row = add_parametric_cut(tab, row, context);
3871 if (row < 0)
3872 goto error;
3874 if (r < 0)
3875 goto error;
3876 done:
3877 sol_add(sol, tab);
3878 isl_tab_free(tab);
3879 return;
3880 error:
3881 isl_tab_free(tab);
3882 sol->error = 1;
3885 /* Does "sol" contain a pair of partial solutions that could potentially
3886 * be merged?
3888 * We currently only check that "sol" is not in an error state
3889 * and that there are at least two partial solutions of which the final two
3890 * are defined at the same level.
3892 static int sol_has_mergeable_solutions(struct isl_sol *sol)
3894 if (sol->error)
3895 return 0;
3896 if (!sol->partial)
3897 return 0;
3898 if (!sol->partial->next)
3899 return 0;
3900 return sol->partial->level == sol->partial->next->level;
3903 /* Compute the lexicographic minimum of the set represented by the main
3904 * tableau "tab" within the context "sol->context_tab".
3906 * As a preprocessing step, we first transfer all the purely parametric
3907 * equalities from the main tableau to the context tableau, i.e.,
3908 * parameters that have been pivoted to a row.
3909 * These equalities are ignored by the main algorithm, because the
3910 * corresponding rows may not be marked as being non-negative.
3911 * In parts of the context where the added equality does not hold,
3912 * the main tableau is marked as being empty.
3914 * Before we embark on the actual computation, we save a copy
3915 * of the context. When we return, we check if there are any
3916 * partial solutions that can potentially be merged. If so,
3917 * we perform a rollback to the initial state of the context.
3918 * The merging of partial solutions happens inside calls to
3919 * sol_dec_level that are pushed onto the undo stack of the context.
3920 * If there are no partial solutions that can potentially be merged
3921 * then the rollback is skipped as it would just be wasted effort.
3923 static void find_solutions_main(struct isl_sol *sol, struct isl_tab *tab)
3925 int row;
3926 void *saved;
3928 if (!tab)
3929 goto error;
3931 sol->level = 0;
3933 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3934 int p;
3935 struct isl_vec *eq;
3937 if (tab->row_var[row] < 0)
3938 continue;
3939 if (tab->row_var[row] >= tab->n_param &&
3940 tab->row_var[row] < tab->n_var - tab->n_div)
3941 continue;
3942 if (tab->row_var[row] < tab->n_param)
3943 p = tab->row_var[row];
3944 else
3945 p = tab->row_var[row]
3946 + tab->n_param - (tab->n_var - tab->n_div);
3948 eq = isl_vec_alloc(tab->mat->ctx, 1+tab->n_param+tab->n_div);
3949 if (!eq)
3950 goto error;
3951 get_row_parameter_line(tab, row, eq->el);
3952 isl_int_neg(eq->el[1 + p], tab->mat->row[row][0]);
3953 eq = isl_vec_normalize(eq);
3955 sol_inc_level(sol);
3956 no_sol_in_strict(sol, tab, eq);
3958 isl_seq_neg(eq->el, eq->el, eq->size);
3959 sol_inc_level(sol);
3960 no_sol_in_strict(sol, tab, eq);
3961 isl_seq_neg(eq->el, eq->el, eq->size);
3963 sol->context->op->add_eq(sol->context, eq->el, 1, 1);
3965 isl_vec_free(eq);
3967 if (isl_tab_mark_redundant(tab, row) < 0)
3968 goto error;
3970 if (sol->context->op->is_empty(sol->context))
3971 break;
3973 row = tab->n_redundant - 1;
3976 saved = sol->context->op->save(sol->context);
3978 find_solutions(sol, tab);
3980 if (sol_has_mergeable_solutions(sol))
3981 sol->context->op->restore(sol->context, saved);
3982 else
3983 sol->context->op->discard(saved);
3985 sol->level = 0;
3986 sol_pop(sol);
3988 return;
3989 error:
3990 isl_tab_free(tab);
3991 sol->error = 1;
3994 /* Check if integer division "div" of "dom" also occurs in "bmap".
3995 * If so, return its position within the divs.
3996 * If not, return -1.
3998 static int find_context_div(struct isl_basic_map *bmap,
3999 struct isl_basic_set *dom, unsigned div)
4001 int i;
4002 unsigned b_dim = isl_space_dim(bmap->dim, isl_dim_all);
4003 unsigned d_dim = isl_space_dim(dom->dim, isl_dim_all);
4005 if (isl_int_is_zero(dom->div[div][0]))
4006 return -1;
4007 if (isl_seq_first_non_zero(dom->div[div] + 2 + d_dim, dom->n_div) != -1)
4008 return -1;
4010 for (i = 0; i < bmap->n_div; ++i) {
4011 if (isl_int_is_zero(bmap->div[i][0]))
4012 continue;
4013 if (isl_seq_first_non_zero(bmap->div[i] + 2 + d_dim,
4014 (b_dim - d_dim) + bmap->n_div) != -1)
4015 continue;
4016 if (isl_seq_eq(bmap->div[i], dom->div[div], 2 + d_dim))
4017 return i;
4019 return -1;
4022 /* The correspondence between the variables in the main tableau,
4023 * the context tableau, and the input map and domain is as follows.
4024 * The first n_param and the last n_div variables of the main tableau
4025 * form the variables of the context tableau.
4026 * In the basic map, these n_param variables correspond to the
4027 * parameters and the input dimensions. In the domain, they correspond
4028 * to the parameters and the set dimensions.
4029 * The n_div variables correspond to the integer divisions in the domain.
4030 * To ensure that everything lines up, we may need to copy some of the
4031 * integer divisions of the domain to the map. These have to be placed
4032 * in the same order as those in the context and they have to be placed
4033 * after any other integer divisions that the map may have.
4034 * This function performs the required reordering.
4036 static struct isl_basic_map *align_context_divs(struct isl_basic_map *bmap,
4037 struct isl_basic_set *dom)
4039 int i;
4040 int common = 0;
4041 int other;
4043 for (i = 0; i < dom->n_div; ++i)
4044 if (find_context_div(bmap, dom, i) != -1)
4045 common++;
4046 other = bmap->n_div - common;
4047 if (dom->n_div - common > 0) {
4048 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
4049 dom->n_div - common, 0, 0);
4050 if (!bmap)
4051 return NULL;
4053 for (i = 0; i < dom->n_div; ++i) {
4054 int pos = find_context_div(bmap, dom, i);
4055 if (pos < 0) {
4056 pos = isl_basic_map_alloc_div(bmap);
4057 if (pos < 0)
4058 goto error;
4059 isl_int_set_si(bmap->div[pos][0], 0);
4061 if (pos != other + i)
4062 isl_basic_map_swap_div(bmap, pos, other + i);
4064 return bmap;
4065 error:
4066 isl_basic_map_free(bmap);
4067 return NULL;
4070 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4071 * some obvious symmetries.
4073 * We make sure the divs in the domain are properly ordered,
4074 * because they will be added one by one in the given order
4075 * during the construction of the solution map.
4077 static struct isl_sol *basic_map_partial_lexopt_base(
4078 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4079 __isl_give isl_set **empty, int max,
4080 struct isl_sol *(*init)(__isl_keep isl_basic_map *bmap,
4081 __isl_take isl_basic_set *dom, int track_empty, int max))
4083 struct isl_tab *tab;
4084 struct isl_sol *sol = NULL;
4085 struct isl_context *context;
4087 if (dom->n_div) {
4088 dom = isl_basic_set_order_divs(dom);
4089 bmap = align_context_divs(bmap, dom);
4091 sol = init(bmap, dom, !!empty, max);
4092 if (!sol)
4093 goto error;
4095 context = sol->context;
4096 if (isl_basic_set_plain_is_empty(context->op->peek_basic_set(context)))
4097 /* nothing */;
4098 else if (isl_basic_map_plain_is_empty(bmap)) {
4099 if (sol->add_empty)
4100 sol->add_empty(sol,
4101 isl_basic_set_copy(context->op->peek_basic_set(context)));
4102 } else {
4103 tab = tab_for_lexmin(bmap,
4104 context->op->peek_basic_set(context), 1, max);
4105 tab = context->op->detect_nonnegative_parameters(context, tab);
4106 find_solutions_main(sol, tab);
4108 if (sol->error)
4109 goto error;
4111 isl_basic_map_free(bmap);
4112 return sol;
4113 error:
4114 sol_free(sol);
4115 isl_basic_map_free(bmap);
4116 return NULL;
4119 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4120 * some obvious symmetries.
4122 * We call basic_map_partial_lexopt_base and extract the results.
4124 static __isl_give isl_map *basic_map_partial_lexopt_base_map(
4125 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4126 __isl_give isl_set **empty, int max)
4128 isl_map *result = NULL;
4129 struct isl_sol *sol;
4130 struct isl_sol_map *sol_map;
4132 sol = basic_map_partial_lexopt_base(bmap, dom, empty, max,
4133 &sol_map_init);
4134 if (!sol)
4135 return NULL;
4136 sol_map = (struct isl_sol_map *) sol;
4138 result = isl_map_copy(sol_map->map);
4139 if (empty)
4140 *empty = isl_set_copy(sol_map->empty);
4141 sol_free(&sol_map->sol);
4142 return result;
4145 /* Structure used during detection of parallel constraints.
4146 * n_in: number of "input" variables: isl_dim_param + isl_dim_in
4147 * n_out: number of "output" variables: isl_dim_out + isl_dim_div
4148 * val: the coefficients of the output variables
4150 struct isl_constraint_equal_info {
4151 isl_basic_map *bmap;
4152 unsigned n_in;
4153 unsigned n_out;
4154 isl_int *val;
4157 /* Check whether the coefficients of the output variables
4158 * of the constraint in "entry" are equal to info->val.
4160 static int constraint_equal(const void *entry, const void *val)
4162 isl_int **row = (isl_int **)entry;
4163 const struct isl_constraint_equal_info *info = val;
4165 return isl_seq_eq((*row) + 1 + info->n_in, info->val, info->n_out);
4168 /* Check whether "bmap" has a pair of constraints that have
4169 * the same coefficients for the output variables.
4170 * Note that the coefficients of the existentially quantified
4171 * variables need to be zero since the existentially quantified
4172 * of the result are usually not the same as those of the input.
4173 * the isl_dim_out and isl_dim_div dimensions.
4174 * If so, return 1 and return the row indices of the two constraints
4175 * in *first and *second.
4177 static int parallel_constraints(__isl_keep isl_basic_map *bmap,
4178 int *first, int *second)
4180 int i;
4181 isl_ctx *ctx = isl_basic_map_get_ctx(bmap);
4182 struct isl_hash_table *table = NULL;
4183 struct isl_hash_table_entry *entry;
4184 struct isl_constraint_equal_info info;
4185 unsigned n_out;
4186 unsigned n_div;
4188 ctx = isl_basic_map_get_ctx(bmap);
4189 table = isl_hash_table_alloc(ctx, bmap->n_ineq);
4190 if (!table)
4191 goto error;
4193 info.n_in = isl_basic_map_dim(bmap, isl_dim_param) +
4194 isl_basic_map_dim(bmap, isl_dim_in);
4195 info.bmap = bmap;
4196 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4197 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4198 info.n_out = n_out + n_div;
4199 for (i = 0; i < bmap->n_ineq; ++i) {
4200 uint32_t hash;
4202 info.val = bmap->ineq[i] + 1 + info.n_in;
4203 if (isl_seq_first_non_zero(info.val, n_out) < 0)
4204 continue;
4205 if (isl_seq_first_non_zero(info.val + n_out, n_div) >= 0)
4206 continue;
4207 hash = isl_seq_get_hash(info.val, info.n_out);
4208 entry = isl_hash_table_find(ctx, table, hash,
4209 constraint_equal, &info, 1);
4210 if (!entry)
4211 goto error;
4212 if (entry->data)
4213 break;
4214 entry->data = &bmap->ineq[i];
4217 if (i < bmap->n_ineq) {
4218 *first = ((isl_int **)entry->data) - bmap->ineq;
4219 *second = i;
4222 isl_hash_table_free(ctx, table);
4224 return i < bmap->n_ineq;
4225 error:
4226 isl_hash_table_free(ctx, table);
4227 return -1;
4230 /* Given a set of upper bounds in "var", add constraints to "bset"
4231 * that make the i-th bound smallest.
4233 * In particular, if there are n bounds b_i, then add the constraints
4235 * b_i <= b_j for j > i
4236 * b_i < b_j for j < i
4238 static __isl_give isl_basic_set *select_minimum(__isl_take isl_basic_set *bset,
4239 __isl_keep isl_mat *var, int i)
4241 isl_ctx *ctx;
4242 int j, k;
4244 ctx = isl_mat_get_ctx(var);
4246 for (j = 0; j < var->n_row; ++j) {
4247 if (j == i)
4248 continue;
4249 k = isl_basic_set_alloc_inequality(bset);
4250 if (k < 0)
4251 goto error;
4252 isl_seq_combine(bset->ineq[k], ctx->one, var->row[j],
4253 ctx->negone, var->row[i], var->n_col);
4254 isl_int_set_si(bset->ineq[k][var->n_col], 0);
4255 if (j < i)
4256 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
4259 bset = isl_basic_set_finalize(bset);
4261 return bset;
4262 error:
4263 isl_basic_set_free(bset);
4264 return NULL;
4267 /* Given a set of upper bounds on the last "input" variable m,
4268 * construct a set that assigns the minimal upper bound to m, i.e.,
4269 * construct a set that divides the space into cells where one
4270 * of the upper bounds is smaller than all the others and assign
4271 * this upper bound to m.
4273 * In particular, if there are n bounds b_i, then the result
4274 * consists of n basic sets, each one of the form
4276 * m = b_i
4277 * b_i <= b_j for j > i
4278 * b_i < b_j for j < i
4280 static __isl_give isl_set *set_minimum(__isl_take isl_space *dim,
4281 __isl_take isl_mat *var)
4283 int i, k;
4284 isl_basic_set *bset = NULL;
4285 isl_ctx *ctx;
4286 isl_set *set = NULL;
4288 if (!dim || !var)
4289 goto error;
4291 ctx = isl_space_get_ctx(dim);
4292 set = isl_set_alloc_space(isl_space_copy(dim),
4293 var->n_row, ISL_SET_DISJOINT);
4295 for (i = 0; i < var->n_row; ++i) {
4296 bset = isl_basic_set_alloc_space(isl_space_copy(dim), 0,
4297 1, var->n_row - 1);
4298 k = isl_basic_set_alloc_equality(bset);
4299 if (k < 0)
4300 goto error;
4301 isl_seq_cpy(bset->eq[k], var->row[i], var->n_col);
4302 isl_int_set_si(bset->eq[k][var->n_col], -1);
4303 bset = select_minimum(bset, var, i);
4304 set = isl_set_add_basic_set(set, bset);
4307 isl_space_free(dim);
4308 isl_mat_free(var);
4309 return set;
4310 error:
4311 isl_basic_set_free(bset);
4312 isl_set_free(set);
4313 isl_space_free(dim);
4314 isl_mat_free(var);
4315 return NULL;
4318 /* Given that the last input variable of "bmap" represents the minimum
4319 * of the bounds in "cst", check whether we need to split the domain
4320 * based on which bound attains the minimum.
4322 * A split is needed when the minimum appears in an integer division
4323 * or in an equality. Otherwise, it is only needed if it appears in
4324 * an upper bound that is different from the upper bounds on which it
4325 * is defined.
4327 static int need_split_basic_map(__isl_keep isl_basic_map *bmap,
4328 __isl_keep isl_mat *cst)
4330 int i, j;
4331 unsigned total;
4332 unsigned pos;
4334 pos = cst->n_col - 1;
4335 total = isl_basic_map_dim(bmap, isl_dim_all);
4337 for (i = 0; i < bmap->n_div; ++i)
4338 if (!isl_int_is_zero(bmap->div[i][2 + pos]))
4339 return 1;
4341 for (i = 0; i < bmap->n_eq; ++i)
4342 if (!isl_int_is_zero(bmap->eq[i][1 + pos]))
4343 return 1;
4345 for (i = 0; i < bmap->n_ineq; ++i) {
4346 if (isl_int_is_nonneg(bmap->ineq[i][1 + pos]))
4347 continue;
4348 if (!isl_int_is_negone(bmap->ineq[i][1 + pos]))
4349 return 1;
4350 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + pos + 1,
4351 total - pos - 1) >= 0)
4352 return 1;
4354 for (j = 0; j < cst->n_row; ++j)
4355 if (isl_seq_eq(bmap->ineq[i], cst->row[j], cst->n_col))
4356 break;
4357 if (j >= cst->n_row)
4358 return 1;
4361 return 0;
4364 /* Given that the last set variable of "bset" represents the minimum
4365 * of the bounds in "cst", check whether we need to split the domain
4366 * based on which bound attains the minimum.
4368 * We simply call need_split_basic_map here. This is safe because
4369 * the position of the minimum is computed from "cst" and not
4370 * from "bmap".
4372 static int need_split_basic_set(__isl_keep isl_basic_set *bset,
4373 __isl_keep isl_mat *cst)
4375 return need_split_basic_map((isl_basic_map *)bset, cst);
4378 /* Given that the last set variable of "set" represents the minimum
4379 * of the bounds in "cst", check whether we need to split the domain
4380 * based on which bound attains the minimum.
4382 static int need_split_set(__isl_keep isl_set *set, __isl_keep isl_mat *cst)
4384 int i;
4386 for (i = 0; i < set->n; ++i)
4387 if (need_split_basic_set(set->p[i], cst))
4388 return 1;
4390 return 0;
4393 /* Given a set of which the last set variable is the minimum
4394 * of the bounds in "cst", split each basic set in the set
4395 * in pieces where one of the bounds is (strictly) smaller than the others.
4396 * This subdivision is given in "min_expr".
4397 * The variable is subsequently projected out.
4399 * We only do the split when it is needed.
4400 * For example if the last input variable m = min(a,b) and the only
4401 * constraints in the given basic set are lower bounds on m,
4402 * i.e., l <= m = min(a,b), then we can simply project out m
4403 * to obtain l <= a and l <= b, without having to split on whether
4404 * m is equal to a or b.
4406 static __isl_give isl_set *split(__isl_take isl_set *empty,
4407 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
4409 int n_in;
4410 int i;
4411 isl_space *dim;
4412 isl_set *res;
4414 if (!empty || !min_expr || !cst)
4415 goto error;
4417 n_in = isl_set_dim(empty, isl_dim_set);
4418 dim = isl_set_get_space(empty);
4419 dim = isl_space_drop_dims(dim, isl_dim_set, n_in - 1, 1);
4420 res = isl_set_empty(dim);
4422 for (i = 0; i < empty->n; ++i) {
4423 isl_set *set;
4425 set = isl_set_from_basic_set(isl_basic_set_copy(empty->p[i]));
4426 if (need_split_basic_set(empty->p[i], cst))
4427 set = isl_set_intersect(set, isl_set_copy(min_expr));
4428 set = isl_set_remove_dims(set, isl_dim_set, n_in - 1, 1);
4430 res = isl_set_union_disjoint(res, set);
4433 isl_set_free(empty);
4434 isl_set_free(min_expr);
4435 isl_mat_free(cst);
4436 return res;
4437 error:
4438 isl_set_free(empty);
4439 isl_set_free(min_expr);
4440 isl_mat_free(cst);
4441 return NULL;
4444 /* Given a map of which the last input variable is the minimum
4445 * of the bounds in "cst", split each basic set in the set
4446 * in pieces where one of the bounds is (strictly) smaller than the others.
4447 * This subdivision is given in "min_expr".
4448 * The variable is subsequently projected out.
4450 * The implementation is essentially the same as that of "split".
4452 static __isl_give isl_map *split_domain(__isl_take isl_map *opt,
4453 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
4455 int n_in;
4456 int i;
4457 isl_space *dim;
4458 isl_map *res;
4460 if (!opt || !min_expr || !cst)
4461 goto error;
4463 n_in = isl_map_dim(opt, isl_dim_in);
4464 dim = isl_map_get_space(opt);
4465 dim = isl_space_drop_dims(dim, isl_dim_in, n_in - 1, 1);
4466 res = isl_map_empty(dim);
4468 for (i = 0; i < opt->n; ++i) {
4469 isl_map *map;
4471 map = isl_map_from_basic_map(isl_basic_map_copy(opt->p[i]));
4472 if (need_split_basic_map(opt->p[i], cst))
4473 map = isl_map_intersect_domain(map,
4474 isl_set_copy(min_expr));
4475 map = isl_map_remove_dims(map, isl_dim_in, n_in - 1, 1);
4477 res = isl_map_union_disjoint(res, map);
4480 isl_map_free(opt);
4481 isl_set_free(min_expr);
4482 isl_mat_free(cst);
4483 return res;
4484 error:
4485 isl_map_free(opt);
4486 isl_set_free(min_expr);
4487 isl_mat_free(cst);
4488 return NULL;
4491 static __isl_give isl_map *basic_map_partial_lexopt(
4492 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4493 __isl_give isl_set **empty, int max);
4495 union isl_lex_res {
4496 void *p;
4497 isl_map *map;
4498 isl_pw_multi_aff *pma;
4501 /* This function is called from basic_map_partial_lexopt_symm.
4502 * The last variable of "bmap" and "dom" corresponds to the minimum
4503 * of the bounds in "cst". "map_space" is the space of the original
4504 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
4505 * is the space of the original domain.
4507 * We recursively call basic_map_partial_lexopt and then plug in
4508 * the definition of the minimum in the result.
4510 static __isl_give union isl_lex_res basic_map_partial_lexopt_symm_map_core(
4511 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4512 __isl_give isl_set **empty, int max, __isl_take isl_mat *cst,
4513 __isl_take isl_space *map_space, __isl_take isl_space *set_space)
4515 isl_map *opt;
4516 isl_set *min_expr;
4517 union isl_lex_res res;
4519 min_expr = set_minimum(isl_basic_set_get_space(dom), isl_mat_copy(cst));
4521 opt = basic_map_partial_lexopt(bmap, dom, empty, max);
4523 if (empty) {
4524 *empty = split(*empty,
4525 isl_set_copy(min_expr), isl_mat_copy(cst));
4526 *empty = isl_set_reset_space(*empty, set_space);
4529 opt = split_domain(opt, min_expr, cst);
4530 opt = isl_map_reset_space(opt, map_space);
4532 res.map = opt;
4533 return res;
4536 /* Given a basic map with at least two parallel constraints (as found
4537 * by the function parallel_constraints), first look for more constraints
4538 * parallel to the two constraint and replace the found list of parallel
4539 * constraints by a single constraint with as "input" part the minimum
4540 * of the input parts of the list of constraints. Then, recursively call
4541 * basic_map_partial_lexopt (possibly finding more parallel constraints)
4542 * and plug in the definition of the minimum in the result.
4544 * More specifically, given a set of constraints
4546 * a x + b_i(p) >= 0
4548 * Replace this set by a single constraint
4550 * a x + u >= 0
4552 * with u a new parameter with constraints
4554 * u <= b_i(p)
4556 * Any solution to the new system is also a solution for the original system
4557 * since
4559 * a x >= -u >= -b_i(p)
4561 * Moreover, m = min_i(b_i(p)) satisfies the constraints on u and can
4562 * therefore be plugged into the solution.
4564 static union isl_lex_res basic_map_partial_lexopt_symm(
4565 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4566 __isl_give isl_set **empty, int max, int first, int second,
4567 __isl_give union isl_lex_res (*core)(__isl_take isl_basic_map *bmap,
4568 __isl_take isl_basic_set *dom,
4569 __isl_give isl_set **empty,
4570 int max, __isl_take isl_mat *cst,
4571 __isl_take isl_space *map_space,
4572 __isl_take isl_space *set_space))
4574 int i, n, k;
4575 int *list = NULL;
4576 unsigned n_in, n_out, n_div;
4577 isl_ctx *ctx;
4578 isl_vec *var = NULL;
4579 isl_mat *cst = NULL;
4580 isl_space *map_space, *set_space;
4581 union isl_lex_res res;
4583 map_space = isl_basic_map_get_space(bmap);
4584 set_space = empty ? isl_basic_set_get_space(dom) : NULL;
4586 n_in = isl_basic_map_dim(bmap, isl_dim_param) +
4587 isl_basic_map_dim(bmap, isl_dim_in);
4588 n_out = isl_basic_map_dim(bmap, isl_dim_all) - n_in;
4590 ctx = isl_basic_map_get_ctx(bmap);
4591 list = isl_alloc_array(ctx, int, bmap->n_ineq);
4592 var = isl_vec_alloc(ctx, n_out);
4593 if ((bmap->n_ineq && !list) || (n_out && !var))
4594 goto error;
4596 list[0] = first;
4597 list[1] = second;
4598 isl_seq_cpy(var->el, bmap->ineq[first] + 1 + n_in, n_out);
4599 for (i = second + 1, n = 2; i < bmap->n_ineq; ++i) {
4600 if (isl_seq_eq(var->el, bmap->ineq[i] + 1 + n_in, n_out))
4601 list[n++] = i;
4604 cst = isl_mat_alloc(ctx, n, 1 + n_in);
4605 if (!cst)
4606 goto error;
4608 for (i = 0; i < n; ++i)
4609 isl_seq_cpy(cst->row[i], bmap->ineq[list[i]], 1 + n_in);
4611 bmap = isl_basic_map_cow(bmap);
4612 if (!bmap)
4613 goto error;
4614 for (i = n - 1; i >= 0; --i)
4615 if (isl_basic_map_drop_inequality(bmap, list[i]) < 0)
4616 goto error;
4618 bmap = isl_basic_map_add(bmap, isl_dim_in, 1);
4619 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
4620 k = isl_basic_map_alloc_inequality(bmap);
4621 if (k < 0)
4622 goto error;
4623 isl_seq_clr(bmap->ineq[k], 1 + n_in);
4624 isl_int_set_si(bmap->ineq[k][1 + n_in], 1);
4625 isl_seq_cpy(bmap->ineq[k] + 1 + n_in + 1, var->el, n_out);
4626 bmap = isl_basic_map_finalize(bmap);
4628 n_div = isl_basic_set_dim(dom, isl_dim_div);
4629 dom = isl_basic_set_add_dims(dom, isl_dim_set, 1);
4630 dom = isl_basic_set_extend_constraints(dom, 0, n);
4631 for (i = 0; i < n; ++i) {
4632 k = isl_basic_set_alloc_inequality(dom);
4633 if (k < 0)
4634 goto error;
4635 isl_seq_cpy(dom->ineq[k], cst->row[i], 1 + n_in);
4636 isl_int_set_si(dom->ineq[k][1 + n_in], -1);
4637 isl_seq_clr(dom->ineq[k] + 1 + n_in + 1, n_div);
4640 isl_vec_free(var);
4641 free(list);
4643 return core(bmap, dom, empty, max, cst, map_space, set_space);
4644 error:
4645 isl_space_free(map_space);
4646 isl_space_free(set_space);
4647 isl_mat_free(cst);
4648 isl_vec_free(var);
4649 free(list);
4650 isl_basic_set_free(dom);
4651 isl_basic_map_free(bmap);
4652 res.p = NULL;
4653 return res;
4656 static __isl_give isl_map *basic_map_partial_lexopt_symm_map(
4657 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4658 __isl_give isl_set **empty, int max, int first, int second)
4660 return basic_map_partial_lexopt_symm(bmap, dom, empty, max,
4661 first, second, &basic_map_partial_lexopt_symm_map_core).map;
4664 /* Recursive part of isl_tab_basic_map_partial_lexopt, after detecting
4665 * equalities and removing redundant constraints.
4667 * We first check if there are any parallel constraints (left).
4668 * If not, we are in the base case.
4669 * If there are parallel constraints, we replace them by a single
4670 * constraint in basic_map_partial_lexopt_symm and then call
4671 * this function recursively to look for more parallel constraints.
4673 static __isl_give isl_map *basic_map_partial_lexopt(
4674 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4675 __isl_give isl_set **empty, int max)
4677 int par = 0;
4678 int first, second;
4680 if (!bmap)
4681 goto error;
4683 if (bmap->ctx->opt->pip_symmetry)
4684 par = parallel_constraints(bmap, &first, &second);
4685 if (par < 0)
4686 goto error;
4687 if (!par)
4688 return basic_map_partial_lexopt_base_map(bmap, dom, empty, max);
4690 return basic_map_partial_lexopt_symm_map(bmap, dom, empty, max,
4691 first, second);
4692 error:
4693 isl_basic_set_free(dom);
4694 isl_basic_map_free(bmap);
4695 return NULL;
4698 /* Compute the lexicographic minimum (or maximum if "max" is set)
4699 * of "bmap" over the domain "dom" and return the result as a map.
4700 * If "empty" is not NULL, then *empty is assigned a set that
4701 * contains those parts of the domain where there is no solution.
4702 * If "bmap" is marked as rational (ISL_BASIC_MAP_RATIONAL),
4703 * then we compute the rational optimum. Otherwise, we compute
4704 * the integral optimum.
4706 * We perform some preprocessing. As the PILP solver does not
4707 * handle implicit equalities very well, we first make sure all
4708 * the equalities are explicitly available.
4710 * We also add context constraints to the basic map and remove
4711 * redundant constraints. This is only needed because of the
4712 * way we handle simple symmetries. In particular, we currently look
4713 * for symmetries on the constraints, before we set up the main tableau.
4714 * It is then no good to look for symmetries on possibly redundant constraints.
4716 struct isl_map *isl_tab_basic_map_partial_lexopt(
4717 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4718 struct isl_set **empty, int max)
4720 if (empty)
4721 *empty = NULL;
4722 if (!bmap || !dom)
4723 goto error;
4725 isl_assert(bmap->ctx,
4726 isl_basic_map_compatible_domain(bmap, dom), goto error);
4728 if (isl_basic_set_dim(dom, isl_dim_all) == 0)
4729 return basic_map_partial_lexopt(bmap, dom, empty, max);
4731 bmap = isl_basic_map_intersect_domain(bmap, isl_basic_set_copy(dom));
4732 bmap = isl_basic_map_detect_equalities(bmap);
4733 bmap = isl_basic_map_remove_redundancies(bmap);
4735 return basic_map_partial_lexopt(bmap, dom, empty, max);
4736 error:
4737 isl_basic_set_free(dom);
4738 isl_basic_map_free(bmap);
4739 return NULL;
4742 struct isl_sol_for {
4743 struct isl_sol sol;
4744 int (*fn)(__isl_take isl_basic_set *dom,
4745 __isl_take isl_aff_list *list, void *user);
4746 void *user;
4749 static void sol_for_free(struct isl_sol_for *sol_for)
4751 if (sol_for->sol.context)
4752 sol_for->sol.context->op->free(sol_for->sol.context);
4753 free(sol_for);
4756 static void sol_for_free_wrap(struct isl_sol *sol)
4758 sol_for_free((struct isl_sol_for *)sol);
4761 /* Add the solution identified by the tableau and the context tableau.
4763 * See documentation of sol_add for more details.
4765 * Instead of constructing a basic map, this function calls a user
4766 * defined function with the current context as a basic set and
4767 * a list of affine expressions representing the relation between
4768 * the input and output. The space over which the affine expressions
4769 * are defined is the same as that of the domain. The number of
4770 * affine expressions in the list is equal to the number of output variables.
4772 static void sol_for_add(struct isl_sol_for *sol,
4773 struct isl_basic_set *dom, struct isl_mat *M)
4775 int i;
4776 isl_ctx *ctx;
4777 isl_local_space *ls;
4778 isl_aff *aff;
4779 isl_aff_list *list;
4781 if (sol->sol.error || !dom || !M)
4782 goto error;
4784 ctx = isl_basic_set_get_ctx(dom);
4785 ls = isl_basic_set_get_local_space(dom);
4786 list = isl_aff_list_alloc(ctx, M->n_row - 1);
4787 for (i = 1; i < M->n_row; ++i) {
4788 aff = isl_aff_alloc(isl_local_space_copy(ls));
4789 if (aff) {
4790 isl_int_set(aff->v->el[0], M->row[0][0]);
4791 isl_seq_cpy(aff->v->el + 1, M->row[i], M->n_col);
4793 aff = isl_aff_normalize(aff);
4794 list = isl_aff_list_add(list, aff);
4796 isl_local_space_free(ls);
4798 dom = isl_basic_set_finalize(dom);
4800 if (sol->fn(isl_basic_set_copy(dom), list, sol->user) < 0)
4801 goto error;
4803 isl_basic_set_free(dom);
4804 isl_mat_free(M);
4805 return;
4806 error:
4807 isl_basic_set_free(dom);
4808 isl_mat_free(M);
4809 sol->sol.error = 1;
4812 static void sol_for_add_wrap(struct isl_sol *sol,
4813 struct isl_basic_set *dom, struct isl_mat *M)
4815 sol_for_add((struct isl_sol_for *)sol, dom, M);
4818 static struct isl_sol_for *sol_for_init(struct isl_basic_map *bmap, int max,
4819 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4820 void *user),
4821 void *user)
4823 struct isl_sol_for *sol_for = NULL;
4824 isl_space *dom_dim;
4825 struct isl_basic_set *dom = NULL;
4827 sol_for = isl_calloc_type(bmap->ctx, struct isl_sol_for);
4828 if (!sol_for)
4829 goto error;
4831 dom_dim = isl_space_domain(isl_space_copy(bmap->dim));
4832 dom = isl_basic_set_universe(dom_dim);
4834 sol_for->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
4835 sol_for->sol.dec_level.callback.run = &sol_dec_level_wrap;
4836 sol_for->sol.dec_level.sol = &sol_for->sol;
4837 sol_for->fn = fn;
4838 sol_for->user = user;
4839 sol_for->sol.max = max;
4840 sol_for->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
4841 sol_for->sol.add = &sol_for_add_wrap;
4842 sol_for->sol.add_empty = NULL;
4843 sol_for->sol.free = &sol_for_free_wrap;
4845 sol_for->sol.context = isl_context_alloc(dom);
4846 if (!sol_for->sol.context)
4847 goto error;
4849 isl_basic_set_free(dom);
4850 return sol_for;
4851 error:
4852 isl_basic_set_free(dom);
4853 sol_for_free(sol_for);
4854 return NULL;
4857 static void sol_for_find_solutions(struct isl_sol_for *sol_for,
4858 struct isl_tab *tab)
4860 find_solutions_main(&sol_for->sol, tab);
4863 int isl_basic_map_foreach_lexopt(__isl_keep isl_basic_map *bmap, int max,
4864 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4865 void *user),
4866 void *user)
4868 struct isl_sol_for *sol_for = NULL;
4870 bmap = isl_basic_map_copy(bmap);
4871 bmap = isl_basic_map_detect_equalities(bmap);
4872 if (!bmap)
4873 return -1;
4875 sol_for = sol_for_init(bmap, max, fn, user);
4876 if (!sol_for)
4877 goto error;
4879 if (isl_basic_map_plain_is_empty(bmap))
4880 /* nothing */;
4881 else {
4882 struct isl_tab *tab;
4883 struct isl_context *context = sol_for->sol.context;
4884 tab = tab_for_lexmin(bmap,
4885 context->op->peek_basic_set(context), 1, max);
4886 tab = context->op->detect_nonnegative_parameters(context, tab);
4887 sol_for_find_solutions(sol_for, tab);
4888 if (sol_for->sol.error)
4889 goto error;
4892 sol_free(&sol_for->sol);
4893 isl_basic_map_free(bmap);
4894 return 0;
4895 error:
4896 sol_free(&sol_for->sol);
4897 isl_basic_map_free(bmap);
4898 return -1;
4901 int isl_basic_set_foreach_lexopt(__isl_keep isl_basic_set *bset, int max,
4902 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4903 void *user),
4904 void *user)
4906 return isl_basic_map_foreach_lexopt(bset, max, fn, user);
4909 /* Check if the given sequence of len variables starting at pos
4910 * represents a trivial (i.e., zero) solution.
4911 * The variables are assumed to be non-negative and to come in pairs,
4912 * with each pair representing a variable of unrestricted sign.
4913 * The solution is trivial if each such pair in the sequence consists
4914 * of two identical values, meaning that the variable being represented
4915 * has value zero.
4917 static int region_is_trivial(struct isl_tab *tab, int pos, int len)
4919 int i;
4921 if (len == 0)
4922 return 0;
4924 for (i = 0; i < len; i += 2) {
4925 int neg_row;
4926 int pos_row;
4928 neg_row = tab->var[pos + i].is_row ?
4929 tab->var[pos + i].index : -1;
4930 pos_row = tab->var[pos + i + 1].is_row ?
4931 tab->var[pos + i + 1].index : -1;
4933 if ((neg_row < 0 ||
4934 isl_int_is_zero(tab->mat->row[neg_row][1])) &&
4935 (pos_row < 0 ||
4936 isl_int_is_zero(tab->mat->row[pos_row][1])))
4937 continue;
4939 if (neg_row < 0 || pos_row < 0)
4940 return 0;
4941 if (isl_int_ne(tab->mat->row[neg_row][1],
4942 tab->mat->row[pos_row][1]))
4943 return 0;
4946 return 1;
4949 /* Return the index of the first trivial region or -1 if all regions
4950 * are non-trivial.
4952 static int first_trivial_region(struct isl_tab *tab,
4953 int n_region, struct isl_region *region)
4955 int i;
4957 for (i = 0; i < n_region; ++i) {
4958 if (region_is_trivial(tab, region[i].pos, region[i].len))
4959 return i;
4962 return -1;
4965 /* Check if the solution is optimal, i.e., whether the first
4966 * n_op entries are zero.
4968 static int is_optimal(__isl_keep isl_vec *sol, int n_op)
4970 int i;
4972 for (i = 0; i < n_op; ++i)
4973 if (!isl_int_is_zero(sol->el[1 + i]))
4974 return 0;
4975 return 1;
4978 /* Add constraints to "tab" that ensure that any solution is significantly
4979 * better that that represented by "sol". That is, find the first
4980 * relevant (within first n_op) non-zero coefficient and force it (along
4981 * with all previous coefficients) to be zero.
4982 * If the solution is already optimal (all relevant coefficients are zero),
4983 * then just mark the table as empty.
4985 static int force_better_solution(struct isl_tab *tab,
4986 __isl_keep isl_vec *sol, int n_op)
4988 int i;
4989 isl_ctx *ctx;
4990 isl_vec *v = NULL;
4992 if (!sol)
4993 return -1;
4995 for (i = 0; i < n_op; ++i)
4996 if (!isl_int_is_zero(sol->el[1 + i]))
4997 break;
4999 if (i == n_op) {
5000 if (isl_tab_mark_empty(tab) < 0)
5001 return -1;
5002 return 0;
5005 ctx = isl_vec_get_ctx(sol);
5006 v = isl_vec_alloc(ctx, 1 + tab->n_var);
5007 if (!v)
5008 return -1;
5010 for (; i >= 0; --i) {
5011 v = isl_vec_clr(v);
5012 isl_int_set_si(v->el[1 + i], -1);
5013 if (add_lexmin_eq(tab, v->el) < 0)
5014 goto error;
5017 isl_vec_free(v);
5018 return 0;
5019 error:
5020 isl_vec_free(v);
5021 return -1;
5024 struct isl_trivial {
5025 int update;
5026 int region;
5027 int side;
5028 struct isl_tab_undo *snap;
5031 /* Return the lexicographically smallest non-trivial solution of the
5032 * given ILP problem.
5034 * All variables are assumed to be non-negative.
5036 * n_op is the number of initial coordinates to optimize.
5037 * That is, once a solution has been found, we will only continue looking
5038 * for solution that result in significantly better values for those
5039 * initial coordinates. That is, we only continue looking for solutions
5040 * that increase the number of initial zeros in this sequence.
5042 * A solution is non-trivial, if it is non-trivial on each of the
5043 * specified regions. Each region represents a sequence of pairs
5044 * of variables. A solution is non-trivial on such a region if
5045 * at least one of these pairs consists of different values, i.e.,
5046 * such that the non-negative variable represented by the pair is non-zero.
5048 * Whenever a conflict is encountered, all constraints involved are
5049 * reported to the caller through a call to "conflict".
5051 * We perform a simple branch-and-bound backtracking search.
5052 * Each level in the search represents initially trivial region that is forced
5053 * to be non-trivial.
5054 * At each level we consider n cases, where n is the length of the region.
5055 * In terms of the n/2 variables of unrestricted signs being encoded by
5056 * the region, we consider the cases
5057 * x_0 >= 1
5058 * x_0 <= -1
5059 * x_0 = 0 and x_1 >= 1
5060 * x_0 = 0 and x_1 <= -1
5061 * x_0 = 0 and x_1 = 0 and x_2 >= 1
5062 * x_0 = 0 and x_1 = 0 and x_2 <= -1
5063 * ...
5064 * The cases are considered in this order, assuming that each pair
5065 * x_i_a x_i_b represents the value x_i_b - x_i_a.
5066 * That is, x_0 >= 1 is enforced by adding the constraint
5067 * x_0_b - x_0_a >= 1
5069 __isl_give isl_vec *isl_tab_basic_set_non_trivial_lexmin(
5070 __isl_take isl_basic_set *bset, int n_op, int n_region,
5071 struct isl_region *region,
5072 int (*conflict)(int con, void *user), void *user)
5074 int i, j;
5075 int r;
5076 isl_ctx *ctx;
5077 isl_vec *v = NULL;
5078 isl_vec *sol = NULL;
5079 struct isl_tab *tab;
5080 struct isl_trivial *triv = NULL;
5081 int level, init;
5083 if (!bset)
5084 return NULL;
5086 ctx = isl_basic_set_get_ctx(bset);
5087 sol = isl_vec_alloc(ctx, 0);
5089 tab = tab_for_lexmin(bset, NULL, 0, 0);
5090 if (!tab)
5091 goto error;
5092 tab->conflict = conflict;
5093 tab->conflict_user = user;
5095 v = isl_vec_alloc(ctx, 1 + tab->n_var);
5096 triv = isl_calloc_array(ctx, struct isl_trivial, n_region);
5097 if (!v || (n_region && !triv))
5098 goto error;
5100 level = 0;
5101 init = 1;
5103 while (level >= 0) {
5104 int side, base;
5106 if (init) {
5107 tab = cut_to_integer_lexmin(tab, CUT_ONE);
5108 if (!tab)
5109 goto error;
5110 if (tab->empty)
5111 goto backtrack;
5112 r = first_trivial_region(tab, n_region, region);
5113 if (r < 0) {
5114 for (i = 0; i < level; ++i)
5115 triv[i].update = 1;
5116 isl_vec_free(sol);
5117 sol = isl_tab_get_sample_value(tab);
5118 if (!sol)
5119 goto error;
5120 if (is_optimal(sol, n_op))
5121 break;
5122 goto backtrack;
5124 if (level >= n_region)
5125 isl_die(ctx, isl_error_internal,
5126 "nesting level too deep", goto error);
5127 if (isl_tab_extend_cons(tab,
5128 2 * region[r].len + 2 * n_op) < 0)
5129 goto error;
5130 triv[level].region = r;
5131 triv[level].side = 0;
5134 r = triv[level].region;
5135 side = triv[level].side;
5136 base = 2 * (side/2);
5138 if (side >= region[r].len) {
5139 backtrack:
5140 level--;
5141 init = 0;
5142 if (level >= 0)
5143 if (isl_tab_rollback(tab, triv[level].snap) < 0)
5144 goto error;
5145 continue;
5148 if (triv[level].update) {
5149 if (force_better_solution(tab, sol, n_op) < 0)
5150 goto error;
5151 triv[level].update = 0;
5154 if (side == base && base >= 2) {
5155 for (j = base - 2; j < base; ++j) {
5156 v = isl_vec_clr(v);
5157 isl_int_set_si(v->el[1 + region[r].pos + j], 1);
5158 if (add_lexmin_eq(tab, v->el) < 0)
5159 goto error;
5163 triv[level].snap = isl_tab_snap(tab);
5164 if (isl_tab_push_basis(tab) < 0)
5165 goto error;
5167 v = isl_vec_clr(v);
5168 isl_int_set_si(v->el[0], -1);
5169 isl_int_set_si(v->el[1 + region[r].pos + side], -1);
5170 isl_int_set_si(v->el[1 + region[r].pos + (side ^ 1)], 1);
5171 tab = add_lexmin_ineq(tab, v->el);
5173 triv[level].side++;
5174 level++;
5175 init = 1;
5178 free(triv);
5179 isl_vec_free(v);
5180 isl_tab_free(tab);
5181 isl_basic_set_free(bset);
5183 return sol;
5184 error:
5185 free(triv);
5186 isl_vec_free(v);
5187 isl_tab_free(tab);
5188 isl_basic_set_free(bset);
5189 isl_vec_free(sol);
5190 return NULL;
5193 /* Return the lexicographically smallest rational point in "bset",
5194 * assuming that all variables are non-negative.
5195 * If "bset" is empty, then return a zero-length vector.
5197 __isl_give isl_vec *isl_tab_basic_set_non_neg_lexmin(
5198 __isl_take isl_basic_set *bset)
5200 struct isl_tab *tab;
5201 isl_ctx *ctx = isl_basic_set_get_ctx(bset);
5202 isl_vec *sol;
5204 if (!bset)
5205 return NULL;
5207 tab = tab_for_lexmin(bset, NULL, 0, 0);
5208 if (!tab)
5209 goto error;
5210 if (tab->empty)
5211 sol = isl_vec_alloc(ctx, 0);
5212 else
5213 sol = isl_tab_get_sample_value(tab);
5214 isl_tab_free(tab);
5215 isl_basic_set_free(bset);
5216 return sol;
5217 error:
5218 isl_tab_free(tab);
5219 isl_basic_set_free(bset);
5220 return NULL;
5223 struct isl_sol_pma {
5224 struct isl_sol sol;
5225 isl_pw_multi_aff *pma;
5226 isl_set *empty;
5229 static void sol_pma_free(struct isl_sol_pma *sol_pma)
5231 if (!sol_pma)
5232 return;
5233 if (sol_pma->sol.context)
5234 sol_pma->sol.context->op->free(sol_pma->sol.context);
5235 isl_pw_multi_aff_free(sol_pma->pma);
5236 isl_set_free(sol_pma->empty);
5237 free(sol_pma);
5240 /* This function is called for parts of the context where there is
5241 * no solution, with "bset" corresponding to the context tableau.
5242 * Simply add the basic set to the set "empty".
5244 static void sol_pma_add_empty(struct isl_sol_pma *sol,
5245 __isl_take isl_basic_set *bset)
5247 if (!bset)
5248 goto error;
5249 isl_assert(bset->ctx, sol->empty, goto error);
5251 sol->empty = isl_set_grow(sol->empty, 1);
5252 bset = isl_basic_set_simplify(bset);
5253 bset = isl_basic_set_finalize(bset);
5254 sol->empty = isl_set_add_basic_set(sol->empty, bset);
5255 if (!sol->empty)
5256 sol->sol.error = 1;
5257 return;
5258 error:
5259 isl_basic_set_free(bset);
5260 sol->sol.error = 1;
5263 /* Given a basic map "dom" that represents the context and an affine
5264 * matrix "M" that maps the dimensions of the context to the
5265 * output variables, construct an isl_pw_multi_aff with a single
5266 * cell corresponding to "dom" and affine expressions copied from "M".
5268 static void sol_pma_add(struct isl_sol_pma *sol,
5269 __isl_take isl_basic_set *dom, __isl_take isl_mat *M)
5271 int i;
5272 isl_local_space *ls;
5273 isl_aff *aff;
5274 isl_multi_aff *maff;
5275 isl_pw_multi_aff *pma;
5277 maff = isl_multi_aff_alloc(isl_pw_multi_aff_get_space(sol->pma));
5278 ls = isl_basic_set_get_local_space(dom);
5279 for (i = 1; i < M->n_row; ++i) {
5280 aff = isl_aff_alloc(isl_local_space_copy(ls));
5281 if (aff) {
5282 isl_int_set(aff->v->el[0], M->row[0][0]);
5283 isl_seq_cpy(aff->v->el + 1, M->row[i], M->n_col);
5285 aff = isl_aff_normalize(aff);
5286 maff = isl_multi_aff_set_aff(maff, i - 1, aff);
5288 isl_local_space_free(ls);
5289 isl_mat_free(M);
5290 dom = isl_basic_set_simplify(dom);
5291 dom = isl_basic_set_finalize(dom);
5292 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(dom), maff);
5293 sol->pma = isl_pw_multi_aff_add_disjoint(sol->pma, pma);
5294 if (!sol->pma)
5295 sol->sol.error = 1;
5298 static void sol_pma_free_wrap(struct isl_sol *sol)
5300 sol_pma_free((struct isl_sol_pma *)sol);
5303 static void sol_pma_add_empty_wrap(struct isl_sol *sol,
5304 __isl_take isl_basic_set *bset)
5306 sol_pma_add_empty((struct isl_sol_pma *)sol, bset);
5309 static void sol_pma_add_wrap(struct isl_sol *sol,
5310 __isl_take isl_basic_set *dom, __isl_take isl_mat *M)
5312 sol_pma_add((struct isl_sol_pma *)sol, dom, M);
5315 /* Construct an isl_sol_pma structure for accumulating the solution.
5316 * If track_empty is set, then we also keep track of the parts
5317 * of the context where there is no solution.
5318 * If max is set, then we are solving a maximization, rather than
5319 * a minimization problem, which means that the variables in the
5320 * tableau have value "M - x" rather than "M + x".
5322 static struct isl_sol *sol_pma_init(__isl_keep isl_basic_map *bmap,
5323 __isl_take isl_basic_set *dom, int track_empty, int max)
5325 struct isl_sol_pma *sol_pma = NULL;
5327 if (!bmap)
5328 goto error;
5330 sol_pma = isl_calloc_type(bmap->ctx, struct isl_sol_pma);
5331 if (!sol_pma)
5332 goto error;
5334 sol_pma->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
5335 sol_pma->sol.dec_level.callback.run = &sol_dec_level_wrap;
5336 sol_pma->sol.dec_level.sol = &sol_pma->sol;
5337 sol_pma->sol.max = max;
5338 sol_pma->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
5339 sol_pma->sol.add = &sol_pma_add_wrap;
5340 sol_pma->sol.add_empty = track_empty ? &sol_pma_add_empty_wrap : NULL;
5341 sol_pma->sol.free = &sol_pma_free_wrap;
5342 sol_pma->pma = isl_pw_multi_aff_empty(isl_basic_map_get_space(bmap));
5343 if (!sol_pma->pma)
5344 goto error;
5346 sol_pma->sol.context = isl_context_alloc(dom);
5347 if (!sol_pma->sol.context)
5348 goto error;
5350 if (track_empty) {
5351 sol_pma->empty = isl_set_alloc_space(isl_basic_set_get_space(dom),
5352 1, ISL_SET_DISJOINT);
5353 if (!sol_pma->empty)
5354 goto error;
5357 isl_basic_set_free(dom);
5358 return &sol_pma->sol;
5359 error:
5360 isl_basic_set_free(dom);
5361 sol_pma_free(sol_pma);
5362 return NULL;
5365 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
5366 * some obvious symmetries.
5368 * We call basic_map_partial_lexopt_base and extract the results.
5370 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_base_pma(
5371 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5372 __isl_give isl_set **empty, int max)
5374 isl_pw_multi_aff *result = NULL;
5375 struct isl_sol *sol;
5376 struct isl_sol_pma *sol_pma;
5378 sol = basic_map_partial_lexopt_base(bmap, dom, empty, max,
5379 &sol_pma_init);
5380 if (!sol)
5381 return NULL;
5382 sol_pma = (struct isl_sol_pma *) sol;
5384 result = isl_pw_multi_aff_copy(sol_pma->pma);
5385 if (empty)
5386 *empty = isl_set_copy(sol_pma->empty);
5387 sol_free(&sol_pma->sol);
5388 return result;
5391 /* Given that the last input variable of "maff" represents the minimum
5392 * of some bounds, check whether we need to plug in the expression
5393 * of the minimum.
5395 * In particular, check if the last input variable appears in any
5396 * of the expressions in "maff".
5398 static int need_substitution(__isl_keep isl_multi_aff *maff)
5400 int i;
5401 unsigned pos;
5403 pos = isl_multi_aff_dim(maff, isl_dim_in) - 1;
5405 for (i = 0; i < maff->n; ++i)
5406 if (isl_aff_involves_dims(maff->p[i], isl_dim_in, pos, 1))
5407 return 1;
5409 return 0;
5412 /* Given a set of upper bounds on the last "input" variable m,
5413 * construct a piecewise affine expression that selects
5414 * the minimal upper bound to m, i.e.,
5415 * divide the space into cells where one
5416 * of the upper bounds is smaller than all the others and select
5417 * this upper bound on that cell.
5419 * In particular, if there are n bounds b_i, then the result
5420 * consists of n cell, each one of the form
5422 * b_i <= b_j for j > i
5423 * b_i < b_j for j < i
5425 * The affine expression on this cell is
5427 * b_i
5429 static __isl_give isl_pw_aff *set_minimum_pa(__isl_take isl_space *space,
5430 __isl_take isl_mat *var)
5432 int i;
5433 isl_aff *aff = NULL;
5434 isl_basic_set *bset = NULL;
5435 isl_ctx *ctx;
5436 isl_pw_aff *paff = NULL;
5437 isl_space *pw_space;
5438 isl_local_space *ls = NULL;
5440 if (!space || !var)
5441 goto error;
5443 ctx = isl_space_get_ctx(space);
5444 ls = isl_local_space_from_space(isl_space_copy(space));
5445 pw_space = isl_space_copy(space);
5446 pw_space = isl_space_from_domain(pw_space);
5447 pw_space = isl_space_add_dims(pw_space, isl_dim_out, 1);
5448 paff = isl_pw_aff_alloc_size(pw_space, var->n_row);
5450 for (i = 0; i < var->n_row; ++i) {
5451 isl_pw_aff *paff_i;
5453 aff = isl_aff_alloc(isl_local_space_copy(ls));
5454 bset = isl_basic_set_alloc_space(isl_space_copy(space), 0,
5455 0, var->n_row - 1);
5456 if (!aff || !bset)
5457 goto error;
5458 isl_int_set_si(aff->v->el[0], 1);
5459 isl_seq_cpy(aff->v->el + 1, var->row[i], var->n_col);
5460 isl_int_set_si(aff->v->el[1 + var->n_col], 0);
5461 bset = select_minimum(bset, var, i);
5462 paff_i = isl_pw_aff_alloc(isl_set_from_basic_set(bset), aff);
5463 paff = isl_pw_aff_add_disjoint(paff, paff_i);
5466 isl_local_space_free(ls);
5467 isl_space_free(space);
5468 isl_mat_free(var);
5469 return paff;
5470 error:
5471 isl_aff_free(aff);
5472 isl_basic_set_free(bset);
5473 isl_pw_aff_free(paff);
5474 isl_local_space_free(ls);
5475 isl_space_free(space);
5476 isl_mat_free(var);
5477 return NULL;
5480 /* Given a piecewise multi-affine expression of which the last input variable
5481 * is the minimum of the bounds in "cst", plug in the value of the minimum.
5482 * This minimum expression is given in "min_expr_pa".
5483 * The set "min_expr" contains the same information, but in the form of a set.
5484 * The variable is subsequently projected out.
5486 * The implementation is similar to those of "split" and "split_domain".
5487 * If the variable appears in a given expression, then minimum expression
5488 * is plugged in. Otherwise, if the variable appears in the constraints
5489 * and a split is required, then the domain is split. Otherwise, no split
5490 * is performed.
5492 static __isl_give isl_pw_multi_aff *split_domain_pma(
5493 __isl_take isl_pw_multi_aff *opt, __isl_take isl_pw_aff *min_expr_pa,
5494 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
5496 int n_in;
5497 int i;
5498 isl_space *space;
5499 isl_pw_multi_aff *res;
5501 if (!opt || !min_expr || !cst)
5502 goto error;
5504 n_in = isl_pw_multi_aff_dim(opt, isl_dim_in);
5505 space = isl_pw_multi_aff_get_space(opt);
5506 space = isl_space_drop_dims(space, isl_dim_in, n_in - 1, 1);
5507 res = isl_pw_multi_aff_empty(space);
5509 for (i = 0; i < opt->n; ++i) {
5510 isl_pw_multi_aff *pma;
5512 pma = isl_pw_multi_aff_alloc(isl_set_copy(opt->p[i].set),
5513 isl_multi_aff_copy(opt->p[i].maff));
5514 if (need_substitution(opt->p[i].maff))
5515 pma = isl_pw_multi_aff_substitute(pma,
5516 isl_dim_in, n_in - 1, min_expr_pa);
5517 else if (need_split_set(opt->p[i].set, cst))
5518 pma = isl_pw_multi_aff_intersect_domain(pma,
5519 isl_set_copy(min_expr));
5520 pma = isl_pw_multi_aff_project_out(pma,
5521 isl_dim_in, n_in - 1, 1);
5523 res = isl_pw_multi_aff_add_disjoint(res, pma);
5526 isl_pw_multi_aff_free(opt);
5527 isl_pw_aff_free(min_expr_pa);
5528 isl_set_free(min_expr);
5529 isl_mat_free(cst);
5530 return res;
5531 error:
5532 isl_pw_multi_aff_free(opt);
5533 isl_pw_aff_free(min_expr_pa);
5534 isl_set_free(min_expr);
5535 isl_mat_free(cst);
5536 return NULL;
5539 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_pma(
5540 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5541 __isl_give isl_set **empty, int max);
5543 /* This function is called from basic_map_partial_lexopt_symm.
5544 * The last variable of "bmap" and "dom" corresponds to the minimum
5545 * of the bounds in "cst". "map_space" is the space of the original
5546 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
5547 * is the space of the original domain.
5549 * We recursively call basic_map_partial_lexopt and then plug in
5550 * the definition of the minimum in the result.
5552 static __isl_give union isl_lex_res basic_map_partial_lexopt_symm_pma_core(
5553 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5554 __isl_give isl_set **empty, int max, __isl_take isl_mat *cst,
5555 __isl_take isl_space *map_space, __isl_take isl_space *set_space)
5557 isl_pw_multi_aff *opt;
5558 isl_pw_aff *min_expr_pa;
5559 isl_set *min_expr;
5560 union isl_lex_res res;
5562 min_expr = set_minimum(isl_basic_set_get_space(dom), isl_mat_copy(cst));
5563 min_expr_pa = set_minimum_pa(isl_basic_set_get_space(dom),
5564 isl_mat_copy(cst));
5566 opt = basic_map_partial_lexopt_pma(bmap, dom, empty, max);
5568 if (empty) {
5569 *empty = split(*empty,
5570 isl_set_copy(min_expr), isl_mat_copy(cst));
5571 *empty = isl_set_reset_space(*empty, set_space);
5574 opt = split_domain_pma(opt, min_expr_pa, min_expr, cst);
5575 opt = isl_pw_multi_aff_reset_space(opt, map_space);
5577 res.pma = opt;
5578 return res;
5581 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_symm_pma(
5582 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5583 __isl_give isl_set **empty, int max, int first, int second)
5585 return basic_map_partial_lexopt_symm(bmap, dom, empty, max,
5586 first, second, &basic_map_partial_lexopt_symm_pma_core).pma;
5589 /* Recursive part of isl_basic_map_partial_lexopt_pw_multi_aff, after detecting
5590 * equalities and removing redundant constraints.
5592 * We first check if there are any parallel constraints (left).
5593 * If not, we are in the base case.
5594 * If there are parallel constraints, we replace them by a single
5595 * constraint in basic_map_partial_lexopt_symm_pma and then call
5596 * this function recursively to look for more parallel constraints.
5598 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_pma(
5599 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5600 __isl_give isl_set **empty, int max)
5602 int par = 0;
5603 int first, second;
5605 if (!bmap)
5606 goto error;
5608 if (bmap->ctx->opt->pip_symmetry)
5609 par = parallel_constraints(bmap, &first, &second);
5610 if (par < 0)
5611 goto error;
5612 if (!par)
5613 return basic_map_partial_lexopt_base_pma(bmap, dom, empty, max);
5615 return basic_map_partial_lexopt_symm_pma(bmap, dom, empty, max,
5616 first, second);
5617 error:
5618 isl_basic_set_free(dom);
5619 isl_basic_map_free(bmap);
5620 return NULL;
5623 /* Compute the lexicographic minimum (or maximum if "max" is set)
5624 * of "bmap" over the domain "dom" and return the result as a piecewise
5625 * multi-affine expression.
5626 * If "empty" is not NULL, then *empty is assigned a set that
5627 * contains those parts of the domain where there is no solution.
5628 * If "bmap" is marked as rational (ISL_BASIC_MAP_RATIONAL),
5629 * then we compute the rational optimum. Otherwise, we compute
5630 * the integral optimum.
5632 * We perform some preprocessing. As the PILP solver does not
5633 * handle implicit equalities very well, we first make sure all
5634 * the equalities are explicitly available.
5636 * We also add context constraints to the basic map and remove
5637 * redundant constraints. This is only needed because of the
5638 * way we handle simple symmetries. In particular, we currently look
5639 * for symmetries on the constraints, before we set up the main tableau.
5640 * It is then no good to look for symmetries on possibly redundant constraints.
5642 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexopt_pw_multi_aff(
5643 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5644 __isl_give isl_set **empty, int max)
5646 if (empty)
5647 *empty = NULL;
5648 if (!bmap || !dom)
5649 goto error;
5651 isl_assert(bmap->ctx,
5652 isl_basic_map_compatible_domain(bmap, dom), goto error);
5654 if (isl_basic_set_dim(dom, isl_dim_all) == 0)
5655 return basic_map_partial_lexopt_pma(bmap, dom, empty, max);
5657 bmap = isl_basic_map_intersect_domain(bmap, isl_basic_set_copy(dom));
5658 bmap = isl_basic_map_detect_equalities(bmap);
5659 bmap = isl_basic_map_remove_redundancies(bmap);
5661 return basic_map_partial_lexopt_pma(bmap, dom, empty, max);
5662 error:
5663 isl_basic_set_free(dom);
5664 isl_basic_map_free(bmap);
5665 return NULL;