isl_ast.c: print_ast_node_isl: avoid use of default label
[isl.git] / isl_tab_pip.c
blob49ede65195b249c55efe12351b1bc1daae38d1c2
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 if (isl_tab_add_sample(tab, sample) < 0)
1804 goto error;
1807 if (!tab->empty && isl_tab_rollback(tab, snap) < 0)
1808 goto error;
1810 return tab;
1811 error:
1812 isl_tab_free(tab);
1813 return NULL;
1816 /* Check if any of the currently active sample values satisfies
1817 * the inequality "ineq" (an equality if eq is set).
1819 static int tab_has_valid_sample(struct isl_tab *tab, isl_int *ineq, int eq)
1821 int i;
1822 isl_int v;
1824 if (!tab)
1825 return -1;
1827 isl_assert(tab->mat->ctx, tab->bmap, return -1);
1828 isl_assert(tab->mat->ctx, tab->samples, return -1);
1829 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, return -1);
1831 isl_int_init(v);
1832 for (i = tab->n_outside; i < tab->n_sample; ++i) {
1833 int sgn;
1834 isl_seq_inner_product(ineq, tab->samples->row[i],
1835 1 + tab->n_var, &v);
1836 sgn = isl_int_sgn(v);
1837 if (eq ? (sgn == 0) : (sgn >= 0))
1838 break;
1840 isl_int_clear(v);
1842 return i < tab->n_sample;
1845 /* Add a div specified by "div" to the tableau "tab" and return
1846 * 1 if the div is obviously non-negative.
1848 static int context_tab_add_div(struct isl_tab *tab, struct isl_vec *div,
1849 int (*add_ineq)(void *user, isl_int *), void *user)
1851 int i;
1852 int r;
1853 struct isl_mat *samples;
1854 int nonneg;
1856 r = isl_tab_add_div(tab, div, add_ineq, user);
1857 if (r < 0)
1858 return -1;
1859 nonneg = tab->var[r].is_nonneg;
1860 tab->var[r].frozen = 1;
1862 samples = isl_mat_extend(tab->samples,
1863 tab->n_sample, 1 + tab->n_var);
1864 tab->samples = samples;
1865 if (!samples)
1866 return -1;
1867 for (i = tab->n_outside; i < samples->n_row; ++i) {
1868 isl_seq_inner_product(div->el + 1, samples->row[i],
1869 div->size - 1, &samples->row[i][samples->n_col - 1]);
1870 isl_int_fdiv_q(samples->row[i][samples->n_col - 1],
1871 samples->row[i][samples->n_col - 1], div->el[0]);
1874 return nonneg;
1877 /* Add a div specified by "div" to both the main tableau and
1878 * the context tableau. In case of the main tableau, we only
1879 * need to add an extra div. In the context tableau, we also
1880 * need to express the meaning of the div.
1881 * Return the index of the div or -1 if anything went wrong.
1883 static int add_div(struct isl_tab *tab, struct isl_context *context,
1884 struct isl_vec *div)
1886 int r;
1887 int nonneg;
1889 if ((nonneg = context->op->add_div(context, div)) < 0)
1890 goto error;
1892 if (!context->op->is_ok(context))
1893 goto error;
1895 if (isl_tab_extend_vars(tab, 1) < 0)
1896 goto error;
1897 r = isl_tab_allocate_var(tab);
1898 if (r < 0)
1899 goto error;
1900 if (nonneg)
1901 tab->var[r].is_nonneg = 1;
1902 tab->var[r].frozen = 1;
1903 tab->n_div++;
1905 return tab->n_div - 1;
1906 error:
1907 context->op->invalidate(context);
1908 return -1;
1911 static int find_div(struct isl_tab *tab, isl_int *div, isl_int denom)
1913 int i;
1914 unsigned total = isl_basic_map_total_dim(tab->bmap);
1916 for (i = 0; i < tab->bmap->n_div; ++i) {
1917 if (isl_int_ne(tab->bmap->div[i][0], denom))
1918 continue;
1919 if (!isl_seq_eq(tab->bmap->div[i] + 1, div, 1 + total))
1920 continue;
1921 return i;
1923 return -1;
1926 /* Return the index of a div that corresponds to "div".
1927 * We first check if we already have such a div and if not, we create one.
1929 static int get_div(struct isl_tab *tab, struct isl_context *context,
1930 struct isl_vec *div)
1932 int d;
1933 struct isl_tab *context_tab = context->op->peek_tab(context);
1935 if (!context_tab)
1936 return -1;
1938 d = find_div(context_tab, div->el + 1, div->el[0]);
1939 if (d != -1)
1940 return d;
1942 return add_div(tab, context, div);
1945 /* Add a parametric cut to cut away the non-integral sample value
1946 * of the give row.
1947 * Let a_i be the coefficients of the constant term and the parameters
1948 * and let b_i be the coefficients of the variables or constraints
1949 * in basis of the tableau.
1950 * Let q be the div q = floor(\sum_i {-a_i} y_i).
1952 * The cut is expressed as
1954 * c = \sum_i -{-a_i} y_i + \sum_i {b_i} x_i + q >= 0
1956 * If q did not already exist in the context tableau, then it is added first.
1957 * If q is in a column of the main tableau then the "+ q" can be accomplished
1958 * by setting the corresponding entry to the denominator of the constraint.
1959 * If q happens to be in a row of the main tableau, then the corresponding
1960 * row needs to be added instead (taking care of the denominators).
1961 * Note that this is very unlikely, but perhaps not entirely impossible.
1963 * The current value of the cut is known to be negative (or at least
1964 * non-positive), so row_sign is set accordingly.
1966 * Return the row of the cut or -1.
1968 static int add_parametric_cut(struct isl_tab *tab, int row,
1969 struct isl_context *context)
1971 struct isl_vec *div;
1972 int d;
1973 int i;
1974 int r;
1975 isl_int *r_row;
1976 int col;
1977 int n;
1978 unsigned off = 2 + tab->M;
1980 if (!context)
1981 return -1;
1983 div = get_row_parameter_div(tab, row);
1984 if (!div)
1985 return -1;
1987 n = tab->n_div;
1988 d = context->op->get_div(context, tab, div);
1989 isl_vec_free(div);
1990 if (d < 0)
1991 return -1;
1993 if (isl_tab_extend_cons(tab, 1) < 0)
1994 return -1;
1995 r = isl_tab_allocate_con(tab);
1996 if (r < 0)
1997 return -1;
1999 r_row = tab->mat->row[tab->con[r].index];
2000 isl_int_set(r_row[0], tab->mat->row[row][0]);
2001 isl_int_neg(r_row[1], tab->mat->row[row][1]);
2002 isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
2003 isl_int_neg(r_row[1], r_row[1]);
2004 if (tab->M)
2005 isl_int_set_si(r_row[2], 0);
2006 for (i = 0; i < tab->n_param; ++i) {
2007 if (tab->var[i].is_row)
2008 continue;
2009 col = tab->var[i].index;
2010 isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
2011 isl_int_fdiv_r(r_row[off + col], r_row[off + col],
2012 tab->mat->row[row][0]);
2013 isl_int_neg(r_row[off + col], r_row[off + col]);
2015 for (i = 0; i < tab->n_div; ++i) {
2016 if (tab->var[tab->n_var - tab->n_div + i].is_row)
2017 continue;
2018 col = tab->var[tab->n_var - tab->n_div + i].index;
2019 isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
2020 isl_int_fdiv_r(r_row[off + col], r_row[off + col],
2021 tab->mat->row[row][0]);
2022 isl_int_neg(r_row[off + col], r_row[off + col]);
2024 for (i = 0; i < tab->n_col; ++i) {
2025 if (tab->col_var[i] >= 0 &&
2026 (tab->col_var[i] < tab->n_param ||
2027 tab->col_var[i] >= tab->n_var - tab->n_div))
2028 continue;
2029 isl_int_fdiv_r(r_row[off + i],
2030 tab->mat->row[row][off + i], tab->mat->row[row][0]);
2032 if (tab->var[tab->n_var - tab->n_div + d].is_row) {
2033 isl_int gcd;
2034 int d_row = tab->var[tab->n_var - tab->n_div + d].index;
2035 isl_int_init(gcd);
2036 isl_int_gcd(gcd, tab->mat->row[d_row][0], r_row[0]);
2037 isl_int_divexact(r_row[0], r_row[0], gcd);
2038 isl_int_divexact(gcd, tab->mat->row[d_row][0], gcd);
2039 isl_seq_combine(r_row + 1, gcd, r_row + 1,
2040 r_row[0], tab->mat->row[d_row] + 1,
2041 off - 1 + tab->n_col);
2042 isl_int_mul(r_row[0], r_row[0], tab->mat->row[d_row][0]);
2043 isl_int_clear(gcd);
2044 } else {
2045 col = tab->var[tab->n_var - tab->n_div + d].index;
2046 isl_int_set(r_row[off + col], tab->mat->row[row][0]);
2049 tab->con[r].is_nonneg = 1;
2050 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
2051 return -1;
2052 if (tab->row_sign)
2053 tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
2055 row = tab->con[r].index;
2057 if (d >= n && context->op->detect_equalities(context, tab) < 0)
2058 return -1;
2060 return row;
2063 /* Construct a tableau for bmap that can be used for computing
2064 * the lexicographic minimum (or maximum) of bmap.
2065 * If not NULL, then dom is the domain where the minimum
2066 * should be computed. In this case, we set up a parametric
2067 * tableau with row signs (initialized to "unknown").
2068 * If M is set, then the tableau will use a big parameter.
2069 * If max is set, then a maximum should be computed instead of a minimum.
2070 * This means that for each variable x, the tableau will contain the variable
2071 * x' = M - x, rather than x' = M + x. This in turn means that the coefficient
2072 * of the variables in all constraints are negated prior to adding them
2073 * to the tableau.
2075 static struct isl_tab *tab_for_lexmin(struct isl_basic_map *bmap,
2076 struct isl_basic_set *dom, unsigned M, int max)
2078 int i;
2079 struct isl_tab *tab;
2080 unsigned n_var;
2081 unsigned o_var;
2083 tab = isl_tab_alloc(bmap->ctx, 2 * bmap->n_eq + bmap->n_ineq + 1,
2084 isl_basic_map_total_dim(bmap), M);
2085 if (!tab)
2086 return NULL;
2088 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
2089 if (dom) {
2090 tab->n_param = isl_basic_set_total_dim(dom) - dom->n_div;
2091 tab->n_div = dom->n_div;
2092 tab->row_sign = isl_calloc_array(bmap->ctx,
2093 enum isl_tab_row_sign, tab->mat->n_row);
2094 if (tab->mat->n_row && !tab->row_sign)
2095 goto error;
2097 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
2098 if (isl_tab_mark_empty(tab) < 0)
2099 goto error;
2100 return tab;
2103 for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
2104 tab->var[i].is_nonneg = 1;
2105 tab->var[i].frozen = 1;
2107 o_var = 1 + tab->n_param;
2108 n_var = tab->n_var - tab->n_param - tab->n_div;
2109 for (i = 0; i < bmap->n_eq; ++i) {
2110 if (max)
2111 isl_seq_neg(bmap->eq[i] + o_var,
2112 bmap->eq[i] + o_var, n_var);
2113 tab = add_lexmin_valid_eq(tab, bmap->eq[i]);
2114 if (max)
2115 isl_seq_neg(bmap->eq[i] + o_var,
2116 bmap->eq[i] + o_var, n_var);
2117 if (!tab || tab->empty)
2118 return tab;
2120 if (bmap->n_eq && restore_lexmin(tab) < 0)
2121 goto error;
2122 for (i = 0; i < bmap->n_ineq; ++i) {
2123 if (max)
2124 isl_seq_neg(bmap->ineq[i] + o_var,
2125 bmap->ineq[i] + o_var, n_var);
2126 tab = add_lexmin_ineq(tab, bmap->ineq[i]);
2127 if (max)
2128 isl_seq_neg(bmap->ineq[i] + o_var,
2129 bmap->ineq[i] + o_var, n_var);
2130 if (!tab || tab->empty)
2131 return tab;
2133 return tab;
2134 error:
2135 isl_tab_free(tab);
2136 return NULL;
2139 /* Given a main tableau where more than one row requires a split,
2140 * determine and return the "best" row to split on.
2142 * Given two rows in the main tableau, if the inequality corresponding
2143 * to the first row is redundant with respect to that of the second row
2144 * in the current tableau, then it is better to split on the second row,
2145 * since in the positive part, both row will be positive.
2146 * (In the negative part a pivot will have to be performed and just about
2147 * anything can happen to the sign of the other row.)
2149 * As a simple heuristic, we therefore select the row that makes the most
2150 * of the other rows redundant.
2152 * Perhaps it would also be useful to look at the number of constraints
2153 * that conflict with any given constraint.
2155 * best is the best row so far (-1 when we have not found any row yet).
2156 * best_r is the number of other rows made redundant by row best.
2157 * When best is still -1, bset_r is meaningless, but it is initialized
2158 * to some arbitrary value (0) anyway. Without this redundant initialization
2159 * valgrind may warn about uninitialized memory accesses when isl
2160 * is compiled with some versions of gcc.
2162 static int best_split(struct isl_tab *tab, struct isl_tab *context_tab)
2164 struct isl_tab_undo *snap;
2165 int split;
2166 int row;
2167 int best = -1;
2168 int best_r = 0;
2170 if (isl_tab_extend_cons(context_tab, 2) < 0)
2171 return -1;
2173 snap = isl_tab_snap(context_tab);
2175 for (split = tab->n_redundant; split < tab->n_row; ++split) {
2176 struct isl_tab_undo *snap2;
2177 struct isl_vec *ineq = NULL;
2178 int r = 0;
2179 int ok;
2181 if (!isl_tab_var_from_row(tab, split)->is_nonneg)
2182 continue;
2183 if (tab->row_sign[split] != isl_tab_row_any)
2184 continue;
2186 ineq = get_row_parameter_ineq(tab, split);
2187 if (!ineq)
2188 return -1;
2189 ok = isl_tab_add_ineq(context_tab, ineq->el) >= 0;
2190 isl_vec_free(ineq);
2191 if (!ok)
2192 return -1;
2194 snap2 = isl_tab_snap(context_tab);
2196 for (row = tab->n_redundant; row < tab->n_row; ++row) {
2197 struct isl_tab_var *var;
2199 if (row == split)
2200 continue;
2201 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
2202 continue;
2203 if (tab->row_sign[row] != isl_tab_row_any)
2204 continue;
2206 ineq = get_row_parameter_ineq(tab, row);
2207 if (!ineq)
2208 return -1;
2209 ok = isl_tab_add_ineq(context_tab, ineq->el) >= 0;
2210 isl_vec_free(ineq);
2211 if (!ok)
2212 return -1;
2213 var = &context_tab->con[context_tab->n_con - 1];
2214 if (!context_tab->empty &&
2215 !isl_tab_min_at_most_neg_one(context_tab, var))
2216 r++;
2217 if (isl_tab_rollback(context_tab, snap2) < 0)
2218 return -1;
2220 if (best == -1 || r > best_r) {
2221 best = split;
2222 best_r = r;
2224 if (isl_tab_rollback(context_tab, snap) < 0)
2225 return -1;
2228 return best;
2231 static struct isl_basic_set *context_lex_peek_basic_set(
2232 struct isl_context *context)
2234 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2235 if (!clex->tab)
2236 return NULL;
2237 return isl_tab_peek_bset(clex->tab);
2240 static struct isl_tab *context_lex_peek_tab(struct isl_context *context)
2242 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2243 return clex->tab;
2246 static void context_lex_add_eq(struct isl_context *context, isl_int *eq,
2247 int check, int update)
2249 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2250 if (isl_tab_extend_cons(clex->tab, 2) < 0)
2251 goto error;
2252 if (add_lexmin_eq(clex->tab, eq) < 0)
2253 goto error;
2254 if (check) {
2255 int v = tab_has_valid_sample(clex->tab, eq, 1);
2256 if (v < 0)
2257 goto error;
2258 if (!v)
2259 clex->tab = check_integer_feasible(clex->tab);
2261 if (update)
2262 clex->tab = check_samples(clex->tab, eq, 1);
2263 return;
2264 error:
2265 isl_tab_free(clex->tab);
2266 clex->tab = NULL;
2269 static void context_lex_add_ineq(struct isl_context *context, isl_int *ineq,
2270 int check, int update)
2272 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2273 if (isl_tab_extend_cons(clex->tab, 1) < 0)
2274 goto error;
2275 clex->tab = add_lexmin_ineq(clex->tab, ineq);
2276 if (check) {
2277 int v = tab_has_valid_sample(clex->tab, ineq, 0);
2278 if (v < 0)
2279 goto error;
2280 if (!v)
2281 clex->tab = check_integer_feasible(clex->tab);
2283 if (update)
2284 clex->tab = check_samples(clex->tab, ineq, 0);
2285 return;
2286 error:
2287 isl_tab_free(clex->tab);
2288 clex->tab = NULL;
2291 static int context_lex_add_ineq_wrap(void *user, isl_int *ineq)
2293 struct isl_context *context = (struct isl_context *)user;
2294 context_lex_add_ineq(context, ineq, 0, 0);
2295 return context->op->is_ok(context) ? 0 : -1;
2298 /* Check which signs can be obtained by "ineq" on all the currently
2299 * active sample values. See row_sign for more information.
2301 static enum isl_tab_row_sign tab_ineq_sign(struct isl_tab *tab, isl_int *ineq,
2302 int strict)
2304 int i;
2305 int sgn;
2306 isl_int tmp;
2307 enum isl_tab_row_sign res = isl_tab_row_unknown;
2309 isl_assert(tab->mat->ctx, tab->samples, return isl_tab_row_unknown);
2310 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var,
2311 return isl_tab_row_unknown);
2313 isl_int_init(tmp);
2314 for (i = tab->n_outside; i < tab->n_sample; ++i) {
2315 isl_seq_inner_product(tab->samples->row[i], ineq,
2316 1 + tab->n_var, &tmp);
2317 sgn = isl_int_sgn(tmp);
2318 if (sgn > 0 || (sgn == 0 && strict)) {
2319 if (res == isl_tab_row_unknown)
2320 res = isl_tab_row_pos;
2321 if (res == isl_tab_row_neg)
2322 res = isl_tab_row_any;
2324 if (sgn < 0) {
2325 if (res == isl_tab_row_unknown)
2326 res = isl_tab_row_neg;
2327 if (res == isl_tab_row_pos)
2328 res = isl_tab_row_any;
2330 if (res == isl_tab_row_any)
2331 break;
2333 isl_int_clear(tmp);
2335 return res;
2338 static enum isl_tab_row_sign context_lex_ineq_sign(struct isl_context *context,
2339 isl_int *ineq, int strict)
2341 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2342 return tab_ineq_sign(clex->tab, ineq, strict);
2345 /* Check whether "ineq" can be added to the tableau without rendering
2346 * it infeasible.
2348 static int context_lex_test_ineq(struct isl_context *context, isl_int *ineq)
2350 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2351 struct isl_tab_undo *snap;
2352 int feasible;
2354 if (!clex->tab)
2355 return -1;
2357 if (isl_tab_extend_cons(clex->tab, 1) < 0)
2358 return -1;
2360 snap = isl_tab_snap(clex->tab);
2361 if (isl_tab_push_basis(clex->tab) < 0)
2362 return -1;
2363 clex->tab = add_lexmin_ineq(clex->tab, ineq);
2364 clex->tab = check_integer_feasible(clex->tab);
2365 if (!clex->tab)
2366 return -1;
2367 feasible = !clex->tab->empty;
2368 if (isl_tab_rollback(clex->tab, snap) < 0)
2369 return -1;
2371 return feasible;
2374 static int context_lex_get_div(struct isl_context *context, struct isl_tab *tab,
2375 struct isl_vec *div)
2377 return get_div(tab, context, div);
2380 /* Add a div specified by "div" to the context tableau and return
2381 * 1 if the div is obviously non-negative.
2382 * context_tab_add_div will always return 1, because all variables
2383 * in a isl_context_lex tableau are non-negative.
2384 * However, if we are using a big parameter in the context, then this only
2385 * reflects the non-negativity of the variable used to _encode_ the
2386 * div, i.e., div' = M + div, so we can't draw any conclusions.
2388 static int context_lex_add_div(struct isl_context *context, struct isl_vec *div)
2390 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2391 int nonneg;
2392 nonneg = context_tab_add_div(clex->tab, div,
2393 context_lex_add_ineq_wrap, context);
2394 if (nonneg < 0)
2395 return -1;
2396 if (clex->tab->M)
2397 return 0;
2398 return nonneg;
2401 static int context_lex_detect_equalities(struct isl_context *context,
2402 struct isl_tab *tab)
2404 return 0;
2407 static int context_lex_best_split(struct isl_context *context,
2408 struct isl_tab *tab)
2410 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2411 struct isl_tab_undo *snap;
2412 int r;
2414 snap = isl_tab_snap(clex->tab);
2415 if (isl_tab_push_basis(clex->tab) < 0)
2416 return -1;
2417 r = best_split(tab, clex->tab);
2419 if (r >= 0 && isl_tab_rollback(clex->tab, snap) < 0)
2420 return -1;
2422 return r;
2425 static int context_lex_is_empty(struct isl_context *context)
2427 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2428 if (!clex->tab)
2429 return -1;
2430 return clex->tab->empty;
2433 static void *context_lex_save(struct isl_context *context)
2435 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2436 struct isl_tab_undo *snap;
2438 snap = isl_tab_snap(clex->tab);
2439 if (isl_tab_push_basis(clex->tab) < 0)
2440 return NULL;
2441 if (isl_tab_save_samples(clex->tab) < 0)
2442 return NULL;
2444 return snap;
2447 static void context_lex_restore(struct isl_context *context, void *save)
2449 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2450 if (isl_tab_rollback(clex->tab, (struct isl_tab_undo *)save) < 0) {
2451 isl_tab_free(clex->tab);
2452 clex->tab = NULL;
2456 static void context_lex_discard(void *save)
2460 static int context_lex_is_ok(struct isl_context *context)
2462 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2463 return !!clex->tab;
2466 /* For each variable in the context tableau, check if the variable can
2467 * only attain non-negative values. If so, mark the parameter as non-negative
2468 * in the main tableau. This allows for a more direct identification of some
2469 * cases of violated constraints.
2471 static struct isl_tab *tab_detect_nonnegative_parameters(struct isl_tab *tab,
2472 struct isl_tab *context_tab)
2474 int i;
2475 struct isl_tab_undo *snap;
2476 struct isl_vec *ineq = NULL;
2477 struct isl_tab_var *var;
2478 int n;
2480 if (context_tab->n_var == 0)
2481 return tab;
2483 ineq = isl_vec_alloc(tab->mat->ctx, 1 + context_tab->n_var);
2484 if (!ineq)
2485 goto error;
2487 if (isl_tab_extend_cons(context_tab, 1) < 0)
2488 goto error;
2490 snap = isl_tab_snap(context_tab);
2492 n = 0;
2493 isl_seq_clr(ineq->el, ineq->size);
2494 for (i = 0; i < context_tab->n_var; ++i) {
2495 isl_int_set_si(ineq->el[1 + i], 1);
2496 if (isl_tab_add_ineq(context_tab, ineq->el) < 0)
2497 goto error;
2498 var = &context_tab->con[context_tab->n_con - 1];
2499 if (!context_tab->empty &&
2500 !isl_tab_min_at_most_neg_one(context_tab, var)) {
2501 int j = i;
2502 if (i >= tab->n_param)
2503 j = i - tab->n_param + tab->n_var - tab->n_div;
2504 tab->var[j].is_nonneg = 1;
2505 n++;
2507 isl_int_set_si(ineq->el[1 + i], 0);
2508 if (isl_tab_rollback(context_tab, snap) < 0)
2509 goto error;
2512 if (context_tab->M && n == context_tab->n_var) {
2513 context_tab->mat = isl_mat_drop_cols(context_tab->mat, 2, 1);
2514 context_tab->M = 0;
2517 isl_vec_free(ineq);
2518 return tab;
2519 error:
2520 isl_vec_free(ineq);
2521 isl_tab_free(tab);
2522 return NULL;
2525 static struct isl_tab *context_lex_detect_nonnegative_parameters(
2526 struct isl_context *context, struct isl_tab *tab)
2528 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2529 struct isl_tab_undo *snap;
2531 if (!tab)
2532 return NULL;
2534 snap = isl_tab_snap(clex->tab);
2535 if (isl_tab_push_basis(clex->tab) < 0)
2536 goto error;
2538 tab = tab_detect_nonnegative_parameters(tab, clex->tab);
2540 if (isl_tab_rollback(clex->tab, snap) < 0)
2541 goto error;
2543 return tab;
2544 error:
2545 isl_tab_free(tab);
2546 return NULL;
2549 static void context_lex_invalidate(struct isl_context *context)
2551 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2552 isl_tab_free(clex->tab);
2553 clex->tab = NULL;
2556 static void context_lex_free(struct isl_context *context)
2558 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2559 isl_tab_free(clex->tab);
2560 free(clex);
2563 struct isl_context_op isl_context_lex_op = {
2564 context_lex_detect_nonnegative_parameters,
2565 context_lex_peek_basic_set,
2566 context_lex_peek_tab,
2567 context_lex_add_eq,
2568 context_lex_add_ineq,
2569 context_lex_ineq_sign,
2570 context_lex_test_ineq,
2571 context_lex_get_div,
2572 context_lex_add_div,
2573 context_lex_detect_equalities,
2574 context_lex_best_split,
2575 context_lex_is_empty,
2576 context_lex_is_ok,
2577 context_lex_save,
2578 context_lex_restore,
2579 context_lex_discard,
2580 context_lex_invalidate,
2581 context_lex_free,
2584 static struct isl_tab *context_tab_for_lexmin(struct isl_basic_set *bset)
2586 struct isl_tab *tab;
2588 if (!bset)
2589 return NULL;
2590 tab = tab_for_lexmin((struct isl_basic_map *)bset, NULL, 1, 0);
2591 if (!tab)
2592 goto error;
2593 if (isl_tab_track_bset(tab, bset) < 0)
2594 goto error;
2595 tab = isl_tab_init_samples(tab);
2596 return tab;
2597 error:
2598 isl_basic_set_free(bset);
2599 return NULL;
2602 static struct isl_context *isl_context_lex_alloc(struct isl_basic_set *dom)
2604 struct isl_context_lex *clex;
2606 if (!dom)
2607 return NULL;
2609 clex = isl_alloc_type(dom->ctx, struct isl_context_lex);
2610 if (!clex)
2611 return NULL;
2613 clex->context.op = &isl_context_lex_op;
2615 clex->tab = context_tab_for_lexmin(isl_basic_set_copy(dom));
2616 if (restore_lexmin(clex->tab) < 0)
2617 goto error;
2618 clex->tab = check_integer_feasible(clex->tab);
2619 if (!clex->tab)
2620 goto error;
2622 return &clex->context;
2623 error:
2624 clex->context.op->free(&clex->context);
2625 return NULL;
2628 /* Representation of the context when using generalized basis reduction.
2630 * "shifted" contains the offsets of the unit hypercubes that lie inside the
2631 * context. Any rational point in "shifted" can therefore be rounded
2632 * up to an integer point in the context.
2633 * If the context is constrained by any equality, then "shifted" is not used
2634 * as it would be empty.
2636 struct isl_context_gbr {
2637 struct isl_context context;
2638 struct isl_tab *tab;
2639 struct isl_tab *shifted;
2640 struct isl_tab *cone;
2643 static struct isl_tab *context_gbr_detect_nonnegative_parameters(
2644 struct isl_context *context, struct isl_tab *tab)
2646 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2647 if (!tab)
2648 return NULL;
2649 return tab_detect_nonnegative_parameters(tab, cgbr->tab);
2652 static struct isl_basic_set *context_gbr_peek_basic_set(
2653 struct isl_context *context)
2655 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2656 if (!cgbr->tab)
2657 return NULL;
2658 return isl_tab_peek_bset(cgbr->tab);
2661 static struct isl_tab *context_gbr_peek_tab(struct isl_context *context)
2663 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2664 return cgbr->tab;
2667 /* Initialize the "shifted" tableau of the context, which
2668 * contains the constraints of the original tableau shifted
2669 * by the sum of all negative coefficients. This ensures
2670 * that any rational point in the shifted tableau can
2671 * be rounded up to yield an integer point in the original tableau.
2673 static void gbr_init_shifted(struct isl_context_gbr *cgbr)
2675 int i, j;
2676 struct isl_vec *cst;
2677 struct isl_basic_set *bset = isl_tab_peek_bset(cgbr->tab);
2678 unsigned dim = isl_basic_set_total_dim(bset);
2680 cst = isl_vec_alloc(cgbr->tab->mat->ctx, bset->n_ineq);
2681 if (!cst)
2682 return;
2684 for (i = 0; i < bset->n_ineq; ++i) {
2685 isl_int_set(cst->el[i], bset->ineq[i][0]);
2686 for (j = 0; j < dim; ++j) {
2687 if (!isl_int_is_neg(bset->ineq[i][1 + j]))
2688 continue;
2689 isl_int_add(bset->ineq[i][0], bset->ineq[i][0],
2690 bset->ineq[i][1 + j]);
2694 cgbr->shifted = isl_tab_from_basic_set(bset, 0);
2696 for (i = 0; i < bset->n_ineq; ++i)
2697 isl_int_set(bset->ineq[i][0], cst->el[i]);
2699 isl_vec_free(cst);
2702 /* Check if the shifted tableau is non-empty, and if so
2703 * use the sample point to construct an integer point
2704 * of the context tableau.
2706 static struct isl_vec *gbr_get_shifted_sample(struct isl_context_gbr *cgbr)
2708 struct isl_vec *sample;
2710 if (!cgbr->shifted)
2711 gbr_init_shifted(cgbr);
2712 if (!cgbr->shifted)
2713 return NULL;
2714 if (cgbr->shifted->empty)
2715 return isl_vec_alloc(cgbr->tab->mat->ctx, 0);
2717 sample = isl_tab_get_sample_value(cgbr->shifted);
2718 sample = isl_vec_ceil(sample);
2720 return sample;
2723 static struct isl_basic_set *drop_constant_terms(struct isl_basic_set *bset)
2725 int i;
2727 if (!bset)
2728 return NULL;
2730 for (i = 0; i < bset->n_eq; ++i)
2731 isl_int_set_si(bset->eq[i][0], 0);
2733 for (i = 0; i < bset->n_ineq; ++i)
2734 isl_int_set_si(bset->ineq[i][0], 0);
2736 return bset;
2739 static int use_shifted(struct isl_context_gbr *cgbr)
2741 if (!cgbr->tab)
2742 return 0;
2743 return cgbr->tab->bmap->n_eq == 0 && cgbr->tab->bmap->n_div == 0;
2746 static struct isl_vec *gbr_get_sample(struct isl_context_gbr *cgbr)
2748 struct isl_basic_set *bset;
2749 struct isl_basic_set *cone;
2751 if (isl_tab_sample_is_integer(cgbr->tab))
2752 return isl_tab_get_sample_value(cgbr->tab);
2754 if (use_shifted(cgbr)) {
2755 struct isl_vec *sample;
2757 sample = gbr_get_shifted_sample(cgbr);
2758 if (!sample || sample->size > 0)
2759 return sample;
2761 isl_vec_free(sample);
2764 if (!cgbr->cone) {
2765 bset = isl_tab_peek_bset(cgbr->tab);
2766 cgbr->cone = isl_tab_from_recession_cone(bset, 0);
2767 if (!cgbr->cone)
2768 return NULL;
2769 if (isl_tab_track_bset(cgbr->cone,
2770 isl_basic_set_copy(bset)) < 0)
2771 return NULL;
2773 if (isl_tab_detect_implicit_equalities(cgbr->cone) < 0)
2774 return NULL;
2776 if (cgbr->cone->n_dead == cgbr->cone->n_col) {
2777 struct isl_vec *sample;
2778 struct isl_tab_undo *snap;
2780 if (cgbr->tab->basis) {
2781 if (cgbr->tab->basis->n_col != 1 + cgbr->tab->n_var) {
2782 isl_mat_free(cgbr->tab->basis);
2783 cgbr->tab->basis = NULL;
2785 cgbr->tab->n_zero = 0;
2786 cgbr->tab->n_unbounded = 0;
2789 snap = isl_tab_snap(cgbr->tab);
2791 sample = isl_tab_sample(cgbr->tab);
2793 if (isl_tab_rollback(cgbr->tab, snap) < 0) {
2794 isl_vec_free(sample);
2795 return NULL;
2798 return sample;
2801 cone = isl_basic_set_dup(isl_tab_peek_bset(cgbr->cone));
2802 cone = drop_constant_terms(cone);
2803 cone = isl_basic_set_update_from_tab(cone, cgbr->cone);
2804 cone = isl_basic_set_underlying_set(cone);
2805 cone = isl_basic_set_gauss(cone, NULL);
2807 bset = isl_basic_set_dup(isl_tab_peek_bset(cgbr->tab));
2808 bset = isl_basic_set_update_from_tab(bset, cgbr->tab);
2809 bset = isl_basic_set_underlying_set(bset);
2810 bset = isl_basic_set_gauss(bset, NULL);
2812 return isl_basic_set_sample_with_cone(bset, cone);
2815 static void check_gbr_integer_feasible(struct isl_context_gbr *cgbr)
2817 struct isl_vec *sample;
2819 if (!cgbr->tab)
2820 return;
2822 if (cgbr->tab->empty)
2823 return;
2825 sample = gbr_get_sample(cgbr);
2826 if (!sample)
2827 goto error;
2829 if (sample->size == 0) {
2830 isl_vec_free(sample);
2831 if (isl_tab_mark_empty(cgbr->tab) < 0)
2832 goto error;
2833 return;
2836 if (isl_tab_add_sample(cgbr->tab, sample) < 0)
2837 goto error;
2839 return;
2840 error:
2841 isl_tab_free(cgbr->tab);
2842 cgbr->tab = NULL;
2845 static struct isl_tab *add_gbr_eq(struct isl_tab *tab, isl_int *eq)
2847 if (!tab)
2848 return NULL;
2850 if (isl_tab_extend_cons(tab, 2) < 0)
2851 goto error;
2853 if (isl_tab_add_eq(tab, eq) < 0)
2854 goto error;
2856 return tab;
2857 error:
2858 isl_tab_free(tab);
2859 return NULL;
2862 /* Add the equality described by "eq" to the context.
2863 * If "check" is set, then we check if the context is empty after
2864 * adding the equality.
2865 * If "update" is set, then we check if the samples are still valid.
2867 * We do not explicitly add shifted copies of the equality to
2868 * cgbr->shifted since they would conflict with each other.
2869 * Instead, we directly mark cgbr->shifted empty.
2871 static void context_gbr_add_eq(struct isl_context *context, isl_int *eq,
2872 int check, int update)
2874 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2876 cgbr->tab = add_gbr_eq(cgbr->tab, eq);
2878 if (cgbr->shifted && !cgbr->shifted->empty && use_shifted(cgbr)) {
2879 if (isl_tab_mark_empty(cgbr->shifted) < 0)
2880 goto error;
2883 if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
2884 if (isl_tab_extend_cons(cgbr->cone, 2) < 0)
2885 goto error;
2886 if (isl_tab_add_eq(cgbr->cone, eq) < 0)
2887 goto error;
2890 if (check) {
2891 int v = tab_has_valid_sample(cgbr->tab, eq, 1);
2892 if (v < 0)
2893 goto error;
2894 if (!v)
2895 check_gbr_integer_feasible(cgbr);
2897 if (update)
2898 cgbr->tab = check_samples(cgbr->tab, eq, 1);
2899 return;
2900 error:
2901 isl_tab_free(cgbr->tab);
2902 cgbr->tab = NULL;
2905 static void add_gbr_ineq(struct isl_context_gbr *cgbr, isl_int *ineq)
2907 if (!cgbr->tab)
2908 return;
2910 if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
2911 goto error;
2913 if (isl_tab_add_ineq(cgbr->tab, ineq) < 0)
2914 goto error;
2916 if (cgbr->shifted && !cgbr->shifted->empty && use_shifted(cgbr)) {
2917 int i;
2918 unsigned dim;
2919 dim = isl_basic_map_total_dim(cgbr->tab->bmap);
2921 if (isl_tab_extend_cons(cgbr->shifted, 1) < 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_add(ineq[0], ineq[0], ineq[1 + i]);
2930 if (isl_tab_add_ineq(cgbr->shifted, ineq) < 0)
2931 goto error;
2933 for (i = 0; i < dim; ++i) {
2934 if (!isl_int_is_neg(ineq[1 + i]))
2935 continue;
2936 isl_int_sub(ineq[0], ineq[0], ineq[1 + i]);
2940 if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
2941 if (isl_tab_extend_cons(cgbr->cone, 1) < 0)
2942 goto error;
2943 if (isl_tab_add_ineq(cgbr->cone, ineq) < 0)
2944 goto error;
2947 return;
2948 error:
2949 isl_tab_free(cgbr->tab);
2950 cgbr->tab = NULL;
2953 static void context_gbr_add_ineq(struct isl_context *context, isl_int *ineq,
2954 int check, int update)
2956 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2958 add_gbr_ineq(cgbr, ineq);
2959 if (!cgbr->tab)
2960 return;
2962 if (check) {
2963 int v = tab_has_valid_sample(cgbr->tab, ineq, 0);
2964 if (v < 0)
2965 goto error;
2966 if (!v)
2967 check_gbr_integer_feasible(cgbr);
2969 if (update)
2970 cgbr->tab = check_samples(cgbr->tab, ineq, 0);
2971 return;
2972 error:
2973 isl_tab_free(cgbr->tab);
2974 cgbr->tab = NULL;
2977 static int context_gbr_add_ineq_wrap(void *user, isl_int *ineq)
2979 struct isl_context *context = (struct isl_context *)user;
2980 context_gbr_add_ineq(context, ineq, 0, 0);
2981 return context->op->is_ok(context) ? 0 : -1;
2984 static enum isl_tab_row_sign context_gbr_ineq_sign(struct isl_context *context,
2985 isl_int *ineq, int strict)
2987 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2988 return tab_ineq_sign(cgbr->tab, ineq, strict);
2991 /* Check whether "ineq" can be added to the tableau without rendering
2992 * it infeasible.
2994 static int context_gbr_test_ineq(struct isl_context *context, isl_int *ineq)
2996 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2997 struct isl_tab_undo *snap;
2998 struct isl_tab_undo *shifted_snap = NULL;
2999 struct isl_tab_undo *cone_snap = NULL;
3000 int feasible;
3002 if (!cgbr->tab)
3003 return -1;
3005 if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
3006 return -1;
3008 snap = isl_tab_snap(cgbr->tab);
3009 if (cgbr->shifted)
3010 shifted_snap = isl_tab_snap(cgbr->shifted);
3011 if (cgbr->cone)
3012 cone_snap = isl_tab_snap(cgbr->cone);
3013 add_gbr_ineq(cgbr, ineq);
3014 check_gbr_integer_feasible(cgbr);
3015 if (!cgbr->tab)
3016 return -1;
3017 feasible = !cgbr->tab->empty;
3018 if (isl_tab_rollback(cgbr->tab, snap) < 0)
3019 return -1;
3020 if (shifted_snap) {
3021 if (isl_tab_rollback(cgbr->shifted, shifted_snap))
3022 return -1;
3023 } else if (cgbr->shifted) {
3024 isl_tab_free(cgbr->shifted);
3025 cgbr->shifted = NULL;
3027 if (cone_snap) {
3028 if (isl_tab_rollback(cgbr->cone, cone_snap))
3029 return -1;
3030 } else if (cgbr->cone) {
3031 isl_tab_free(cgbr->cone);
3032 cgbr->cone = NULL;
3035 return feasible;
3038 /* Return the column of the last of the variables associated to
3039 * a column that has a non-zero coefficient.
3040 * This function is called in a context where only coefficients
3041 * of parameters or divs can be non-zero.
3043 static int last_non_zero_var_col(struct isl_tab *tab, isl_int *p)
3045 int i;
3046 int col;
3048 if (tab->n_var == 0)
3049 return -1;
3051 for (i = tab->n_var - 1; i >= 0; --i) {
3052 if (i >= tab->n_param && i < tab->n_var - tab->n_div)
3053 continue;
3054 if (tab->var[i].is_row)
3055 continue;
3056 col = tab->var[i].index;
3057 if (!isl_int_is_zero(p[col]))
3058 return col;
3061 return -1;
3064 /* Look through all the recently added equalities in the context
3065 * to see if we can propagate any of them to the main tableau.
3067 * The newly added equalities in the context are encoded as pairs
3068 * of inequalities starting at inequality "first".
3070 * We tentatively add each of these equalities to the main tableau
3071 * and if this happens to result in a row with a final coefficient
3072 * that is one or negative one, we use it to kill a column
3073 * in the main tableau. Otherwise, we discard the tentatively
3074 * added row.
3076 * Return 0 on success and -1 on failure.
3078 static int propagate_equalities(struct isl_context_gbr *cgbr,
3079 struct isl_tab *tab, unsigned first)
3081 int i;
3082 struct isl_vec *eq = NULL;
3084 eq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
3085 if (!eq)
3086 goto error;
3088 if (isl_tab_extend_cons(tab, (cgbr->tab->bmap->n_ineq - first)/2) < 0)
3089 goto error;
3091 isl_seq_clr(eq->el + 1 + tab->n_param,
3092 tab->n_var - tab->n_param - tab->n_div);
3093 for (i = first; i < cgbr->tab->bmap->n_ineq; i += 2) {
3094 int j;
3095 int r;
3096 struct isl_tab_undo *snap;
3097 snap = isl_tab_snap(tab);
3099 isl_seq_cpy(eq->el, cgbr->tab->bmap->ineq[i], 1 + tab->n_param);
3100 isl_seq_cpy(eq->el + 1 + tab->n_var - tab->n_div,
3101 cgbr->tab->bmap->ineq[i] + 1 + tab->n_param,
3102 tab->n_div);
3104 r = isl_tab_add_row(tab, eq->el);
3105 if (r < 0)
3106 goto error;
3107 r = tab->con[r].index;
3108 j = last_non_zero_var_col(tab, tab->mat->row[r] + 2 + tab->M);
3109 if (j < 0 || j < tab->n_dead ||
3110 !isl_int_is_one(tab->mat->row[r][0]) ||
3111 (!isl_int_is_one(tab->mat->row[r][2 + tab->M + j]) &&
3112 !isl_int_is_negone(tab->mat->row[r][2 + tab->M + j]))) {
3113 if (isl_tab_rollback(tab, snap) < 0)
3114 goto error;
3115 continue;
3117 if (isl_tab_pivot(tab, r, j) < 0)
3118 goto error;
3119 if (isl_tab_kill_col(tab, j) < 0)
3120 goto error;
3122 if (restore_lexmin(tab) < 0)
3123 goto error;
3126 isl_vec_free(eq);
3128 return 0;
3129 error:
3130 isl_vec_free(eq);
3131 isl_tab_free(cgbr->tab);
3132 cgbr->tab = NULL;
3133 return -1;
3136 static int context_gbr_detect_equalities(struct isl_context *context,
3137 struct isl_tab *tab)
3139 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3140 unsigned n_ineq;
3142 if (!cgbr->cone) {
3143 struct isl_basic_set *bset = isl_tab_peek_bset(cgbr->tab);
3144 cgbr->cone = isl_tab_from_recession_cone(bset, 0);
3145 if (!cgbr->cone)
3146 goto error;
3147 if (isl_tab_track_bset(cgbr->cone,
3148 isl_basic_set_copy(bset)) < 0)
3149 goto error;
3151 if (isl_tab_detect_implicit_equalities(cgbr->cone) < 0)
3152 goto error;
3154 n_ineq = cgbr->tab->bmap->n_ineq;
3155 cgbr->tab = isl_tab_detect_equalities(cgbr->tab, cgbr->cone);
3156 if (!cgbr->tab)
3157 return -1;
3158 if (cgbr->tab->bmap->n_ineq > n_ineq &&
3159 propagate_equalities(cgbr, tab, n_ineq) < 0)
3160 return -1;
3162 return 0;
3163 error:
3164 isl_tab_free(cgbr->tab);
3165 cgbr->tab = NULL;
3166 return -1;
3169 static int context_gbr_get_div(struct isl_context *context, struct isl_tab *tab,
3170 struct isl_vec *div)
3172 return get_div(tab, context, div);
3175 static int context_gbr_add_div(struct isl_context *context, struct isl_vec *div)
3177 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3178 if (cgbr->cone) {
3179 int k;
3181 if (isl_tab_extend_cons(cgbr->cone, 3) < 0)
3182 return -1;
3183 if (isl_tab_extend_vars(cgbr->cone, 1) < 0)
3184 return -1;
3185 if (isl_tab_allocate_var(cgbr->cone) <0)
3186 return -1;
3188 cgbr->cone->bmap = isl_basic_map_extend_space(cgbr->cone->bmap,
3189 isl_basic_map_get_space(cgbr->cone->bmap), 1, 0, 2);
3190 k = isl_basic_map_alloc_div(cgbr->cone->bmap);
3191 if (k < 0)
3192 return -1;
3193 isl_seq_cpy(cgbr->cone->bmap->div[k], div->el, div->size);
3194 if (isl_tab_push(cgbr->cone, isl_tab_undo_bmap_div) < 0)
3195 return -1;
3197 return context_tab_add_div(cgbr->tab, div,
3198 context_gbr_add_ineq_wrap, context);
3201 static int context_gbr_best_split(struct isl_context *context,
3202 struct isl_tab *tab)
3204 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3205 struct isl_tab_undo *snap;
3206 int r;
3208 snap = isl_tab_snap(cgbr->tab);
3209 r = best_split(tab, cgbr->tab);
3211 if (r >= 0 && isl_tab_rollback(cgbr->tab, snap) < 0)
3212 return -1;
3214 return r;
3217 static int context_gbr_is_empty(struct isl_context *context)
3219 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3220 if (!cgbr->tab)
3221 return -1;
3222 return cgbr->tab->empty;
3225 struct isl_gbr_tab_undo {
3226 struct isl_tab_undo *tab_snap;
3227 struct isl_tab_undo *shifted_snap;
3228 struct isl_tab_undo *cone_snap;
3231 static void *context_gbr_save(struct isl_context *context)
3233 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3234 struct isl_gbr_tab_undo *snap;
3236 if (!cgbr->tab)
3237 return NULL;
3239 snap = isl_alloc_type(cgbr->tab->mat->ctx, struct isl_gbr_tab_undo);
3240 if (!snap)
3241 return NULL;
3243 snap->tab_snap = isl_tab_snap(cgbr->tab);
3244 if (isl_tab_save_samples(cgbr->tab) < 0)
3245 goto error;
3247 if (cgbr->shifted)
3248 snap->shifted_snap = isl_tab_snap(cgbr->shifted);
3249 else
3250 snap->shifted_snap = NULL;
3252 if (cgbr->cone)
3253 snap->cone_snap = isl_tab_snap(cgbr->cone);
3254 else
3255 snap->cone_snap = NULL;
3257 return snap;
3258 error:
3259 free(snap);
3260 return NULL;
3263 static void context_gbr_restore(struct isl_context *context, void *save)
3265 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3266 struct isl_gbr_tab_undo *snap = (struct isl_gbr_tab_undo *)save;
3267 if (!snap)
3268 goto error;
3269 if (isl_tab_rollback(cgbr->tab, snap->tab_snap) < 0) {
3270 isl_tab_free(cgbr->tab);
3271 cgbr->tab = NULL;
3274 if (snap->shifted_snap) {
3275 if (isl_tab_rollback(cgbr->shifted, snap->shifted_snap) < 0)
3276 goto error;
3277 } else if (cgbr->shifted) {
3278 isl_tab_free(cgbr->shifted);
3279 cgbr->shifted = NULL;
3282 if (snap->cone_snap) {
3283 if (isl_tab_rollback(cgbr->cone, snap->cone_snap) < 0)
3284 goto error;
3285 } else if (cgbr->cone) {
3286 isl_tab_free(cgbr->cone);
3287 cgbr->cone = NULL;
3290 free(snap);
3292 return;
3293 error:
3294 free(snap);
3295 isl_tab_free(cgbr->tab);
3296 cgbr->tab = NULL;
3299 static void context_gbr_discard(void *save)
3301 struct isl_gbr_tab_undo *snap = (struct isl_gbr_tab_undo *)save;
3302 free(snap);
3305 static int context_gbr_is_ok(struct isl_context *context)
3307 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3308 return !!cgbr->tab;
3311 static void context_gbr_invalidate(struct isl_context *context)
3313 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3314 isl_tab_free(cgbr->tab);
3315 cgbr->tab = NULL;
3318 static void context_gbr_free(struct isl_context *context)
3320 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3321 isl_tab_free(cgbr->tab);
3322 isl_tab_free(cgbr->shifted);
3323 isl_tab_free(cgbr->cone);
3324 free(cgbr);
3327 struct isl_context_op isl_context_gbr_op = {
3328 context_gbr_detect_nonnegative_parameters,
3329 context_gbr_peek_basic_set,
3330 context_gbr_peek_tab,
3331 context_gbr_add_eq,
3332 context_gbr_add_ineq,
3333 context_gbr_ineq_sign,
3334 context_gbr_test_ineq,
3335 context_gbr_get_div,
3336 context_gbr_add_div,
3337 context_gbr_detect_equalities,
3338 context_gbr_best_split,
3339 context_gbr_is_empty,
3340 context_gbr_is_ok,
3341 context_gbr_save,
3342 context_gbr_restore,
3343 context_gbr_discard,
3344 context_gbr_invalidate,
3345 context_gbr_free,
3348 static struct isl_context *isl_context_gbr_alloc(struct isl_basic_set *dom)
3350 struct isl_context_gbr *cgbr;
3352 if (!dom)
3353 return NULL;
3355 cgbr = isl_calloc_type(dom->ctx, struct isl_context_gbr);
3356 if (!cgbr)
3357 return NULL;
3359 cgbr->context.op = &isl_context_gbr_op;
3361 cgbr->shifted = NULL;
3362 cgbr->cone = NULL;
3363 cgbr->tab = isl_tab_from_basic_set(dom, 1);
3364 cgbr->tab = isl_tab_init_samples(cgbr->tab);
3365 if (!cgbr->tab)
3366 goto error;
3367 check_gbr_integer_feasible(cgbr);
3369 return &cgbr->context;
3370 error:
3371 cgbr->context.op->free(&cgbr->context);
3372 return NULL;
3375 static struct isl_context *isl_context_alloc(struct isl_basic_set *dom)
3377 if (!dom)
3378 return NULL;
3380 if (dom->ctx->opt->context == ISL_CONTEXT_LEXMIN)
3381 return isl_context_lex_alloc(dom);
3382 else
3383 return isl_context_gbr_alloc(dom);
3386 /* Construct an isl_sol_map structure for accumulating the solution.
3387 * If track_empty is set, then we also keep track of the parts
3388 * of the context where there is no solution.
3389 * If max is set, then we are solving a maximization, rather than
3390 * a minimization problem, which means that the variables in the
3391 * tableau have value "M - x" rather than "M + x".
3393 static struct isl_sol *sol_map_init(struct isl_basic_map *bmap,
3394 struct isl_basic_set *dom, int track_empty, int max)
3396 struct isl_sol_map *sol_map = NULL;
3398 if (!bmap)
3399 goto error;
3401 sol_map = isl_calloc_type(bmap->ctx, struct isl_sol_map);
3402 if (!sol_map)
3403 goto error;
3405 sol_map->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
3406 sol_map->sol.dec_level.callback.run = &sol_dec_level_wrap;
3407 sol_map->sol.dec_level.sol = &sol_map->sol;
3408 sol_map->sol.max = max;
3409 sol_map->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
3410 sol_map->sol.add = &sol_map_add_wrap;
3411 sol_map->sol.add_empty = track_empty ? &sol_map_add_empty_wrap : NULL;
3412 sol_map->sol.free = &sol_map_free_wrap;
3413 sol_map->map = isl_map_alloc_space(isl_basic_map_get_space(bmap), 1,
3414 ISL_MAP_DISJOINT);
3415 if (!sol_map->map)
3416 goto error;
3418 sol_map->sol.context = isl_context_alloc(dom);
3419 if (!sol_map->sol.context)
3420 goto error;
3422 if (track_empty) {
3423 sol_map->empty = isl_set_alloc_space(isl_basic_set_get_space(dom),
3424 1, ISL_SET_DISJOINT);
3425 if (!sol_map->empty)
3426 goto error;
3429 isl_basic_set_free(dom);
3430 return &sol_map->sol;
3431 error:
3432 isl_basic_set_free(dom);
3433 sol_map_free(sol_map);
3434 return NULL;
3437 /* Check whether all coefficients of (non-parameter) variables
3438 * are non-positive, meaning that no pivots can be performed on the row.
3440 static int is_critical(struct isl_tab *tab, int row)
3442 int j;
3443 unsigned off = 2 + tab->M;
3445 for (j = tab->n_dead; j < tab->n_col; ++j) {
3446 if (tab->col_var[j] >= 0 &&
3447 (tab->col_var[j] < tab->n_param ||
3448 tab->col_var[j] >= tab->n_var - tab->n_div))
3449 continue;
3451 if (isl_int_is_pos(tab->mat->row[row][off + j]))
3452 return 0;
3455 return 1;
3458 /* Check whether the inequality represented by vec is strict over the integers,
3459 * i.e., there are no integer values satisfying the constraint with
3460 * equality. This happens if the gcd of the coefficients is not a divisor
3461 * of the constant term. If so, scale the constraint down by the gcd
3462 * of the coefficients.
3464 static int is_strict(struct isl_vec *vec)
3466 isl_int gcd;
3467 int strict = 0;
3469 isl_int_init(gcd);
3470 isl_seq_gcd(vec->el + 1, vec->size - 1, &gcd);
3471 if (!isl_int_is_one(gcd)) {
3472 strict = !isl_int_is_divisible_by(vec->el[0], gcd);
3473 isl_int_fdiv_q(vec->el[0], vec->el[0], gcd);
3474 isl_seq_scale_down(vec->el + 1, vec->el + 1, gcd, vec->size-1);
3476 isl_int_clear(gcd);
3478 return strict;
3481 /* Determine the sign of the given row of the main tableau.
3482 * The result is one of
3483 * isl_tab_row_pos: always non-negative; no pivot needed
3484 * isl_tab_row_neg: always non-positive; pivot
3485 * isl_tab_row_any: can be both positive and negative; split
3487 * We first handle some simple cases
3488 * - the row sign may be known already
3489 * - the row may be obviously non-negative
3490 * - the parametric constant may be equal to that of another row
3491 * for which we know the sign. This sign will be either "pos" or
3492 * "any". If it had been "neg" then we would have pivoted before.
3494 * If none of these cases hold, we check the value of the row for each
3495 * of the currently active samples. Based on the signs of these values
3496 * we make an initial determination of the sign of the row.
3498 * all zero -> unk(nown)
3499 * all non-negative -> pos
3500 * all non-positive -> neg
3501 * both negative and positive -> all
3503 * If we end up with "all", we are done.
3504 * Otherwise, we perform a check for positive and/or negative
3505 * values as follows.
3507 * samples neg unk pos
3508 * <0 ? Y N Y N
3509 * pos any pos
3510 * >0 ? Y N Y N
3511 * any neg any neg
3513 * There is no special sign for "zero", because we can usually treat zero
3514 * as either non-negative or non-positive, whatever works out best.
3515 * However, if the row is "critical", meaning that pivoting is impossible
3516 * then we don't want to limp zero with the non-positive case, because
3517 * then we we would lose the solution for those values of the parameters
3518 * where the value of the row is zero. Instead, we treat 0 as non-negative
3519 * ensuring a split if the row can attain both zero and negative values.
3520 * The same happens when the original constraint was one that could not
3521 * be satisfied with equality by any integer values of the parameters.
3522 * In this case, we normalize the constraint, but then a value of zero
3523 * for the normalized constraint is actually a positive value for the
3524 * original constraint, so again we need to treat zero as non-negative.
3525 * In both these cases, we have the following decision tree instead:
3527 * all non-negative -> pos
3528 * all negative -> neg
3529 * both negative and non-negative -> all
3531 * samples neg pos
3532 * <0 ? Y N
3533 * any pos
3534 * >=0 ? Y N
3535 * any neg
3537 static enum isl_tab_row_sign row_sign(struct isl_tab *tab,
3538 struct isl_sol *sol, int row)
3540 struct isl_vec *ineq = NULL;
3541 enum isl_tab_row_sign res = isl_tab_row_unknown;
3542 int critical;
3543 int strict;
3544 int row2;
3546 if (tab->row_sign[row] != isl_tab_row_unknown)
3547 return tab->row_sign[row];
3548 if (is_obviously_nonneg(tab, row))
3549 return isl_tab_row_pos;
3550 for (row2 = tab->n_redundant; row2 < tab->n_row; ++row2) {
3551 if (tab->row_sign[row2] == isl_tab_row_unknown)
3552 continue;
3553 if (identical_parameter_line(tab, row, row2))
3554 return tab->row_sign[row2];
3557 critical = is_critical(tab, row);
3559 ineq = get_row_parameter_ineq(tab, row);
3560 if (!ineq)
3561 goto error;
3563 strict = is_strict(ineq);
3565 res = sol->context->op->ineq_sign(sol->context, ineq->el,
3566 critical || strict);
3568 if (res == isl_tab_row_unknown || res == isl_tab_row_pos) {
3569 /* test for negative values */
3570 int feasible;
3571 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3572 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3574 feasible = sol->context->op->test_ineq(sol->context, ineq->el);
3575 if (feasible < 0)
3576 goto error;
3577 if (!feasible)
3578 res = isl_tab_row_pos;
3579 else
3580 res = (res == isl_tab_row_unknown) ? isl_tab_row_neg
3581 : isl_tab_row_any;
3582 if (res == isl_tab_row_neg) {
3583 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3584 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3588 if (res == isl_tab_row_neg) {
3589 /* test for positive values */
3590 int feasible;
3591 if (!critical && !strict)
3592 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3594 feasible = sol->context->op->test_ineq(sol->context, ineq->el);
3595 if (feasible < 0)
3596 goto error;
3597 if (feasible)
3598 res = isl_tab_row_any;
3601 isl_vec_free(ineq);
3602 return res;
3603 error:
3604 isl_vec_free(ineq);
3605 return isl_tab_row_unknown;
3608 static void find_solutions(struct isl_sol *sol, struct isl_tab *tab);
3610 /* Find solutions for values of the parameters that satisfy the given
3611 * inequality.
3613 * We currently take a snapshot of the context tableau that is reset
3614 * when we return from this function, while we make a copy of the main
3615 * tableau, leaving the original main tableau untouched.
3616 * These are fairly arbitrary choices. Making a copy also of the context
3617 * tableau would obviate the need to undo any changes made to it later,
3618 * while taking a snapshot of the main tableau could reduce memory usage.
3619 * If we were to switch to taking a snapshot of the main tableau,
3620 * we would have to keep in mind that we need to save the row signs
3621 * and that we need to do this before saving the current basis
3622 * such that the basis has been restore before we restore the row signs.
3624 static void find_in_pos(struct isl_sol *sol, struct isl_tab *tab, isl_int *ineq)
3626 void *saved;
3628 if (!sol->context)
3629 goto error;
3630 saved = sol->context->op->save(sol->context);
3632 tab = isl_tab_dup(tab);
3633 if (!tab)
3634 goto error;
3636 sol->context->op->add_ineq(sol->context, ineq, 0, 1);
3638 find_solutions(sol, tab);
3640 if (!sol->error)
3641 sol->context->op->restore(sol->context, saved);
3642 else
3643 sol->context->op->discard(saved);
3644 return;
3645 error:
3646 sol->error = 1;
3649 /* Record the absence of solutions for those values of the parameters
3650 * that do not satisfy the given inequality with equality.
3652 static void no_sol_in_strict(struct isl_sol *sol,
3653 struct isl_tab *tab, struct isl_vec *ineq)
3655 int empty;
3656 void *saved;
3658 if (!sol->context || sol->error)
3659 goto error;
3660 saved = sol->context->op->save(sol->context);
3662 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3664 sol->context->op->add_ineq(sol->context, ineq->el, 1, 0);
3665 if (!sol->context)
3666 goto error;
3668 empty = tab->empty;
3669 tab->empty = 1;
3670 sol_add(sol, tab);
3671 tab->empty = empty;
3673 isl_int_add_ui(ineq->el[0], ineq->el[0], 1);
3675 sol->context->op->restore(sol->context, saved);
3676 return;
3677 error:
3678 sol->error = 1;
3681 /* Compute the lexicographic minimum of the set represented by the main
3682 * tableau "tab" within the context "sol->context_tab".
3683 * On entry the sample value of the main tableau is lexicographically
3684 * less than or equal to this lexicographic minimum.
3685 * Pivots are performed until a feasible point is found, which is then
3686 * necessarily equal to the minimum, or until the tableau is found to
3687 * be infeasible. Some pivots may need to be performed for only some
3688 * feasible values of the context tableau. If so, the context tableau
3689 * is split into a part where the pivot is needed and a part where it is not.
3691 * Whenever we enter the main loop, the main tableau is such that no
3692 * "obvious" pivots need to be performed on it, where "obvious" means
3693 * that the given row can be seen to be negative without looking at
3694 * the context tableau. In particular, for non-parametric problems,
3695 * no pivots need to be performed on the main tableau.
3696 * The caller of find_solutions is responsible for making this property
3697 * hold prior to the first iteration of the loop, while restore_lexmin
3698 * is called before every other iteration.
3700 * Inside the main loop, we first examine the signs of the rows of
3701 * the main tableau within the context of the context tableau.
3702 * If we find a row that is always non-positive for all values of
3703 * the parameters satisfying the context tableau and negative for at
3704 * least one value of the parameters, we perform the appropriate pivot
3705 * and start over. An exception is the case where no pivot can be
3706 * performed on the row. In this case, we require that the sign of
3707 * the row is negative for all values of the parameters (rather than just
3708 * non-positive). This special case is handled inside row_sign, which
3709 * will say that the row can have any sign if it determines that it can
3710 * attain both negative and zero values.
3712 * If we can't find a row that always requires a pivot, but we can find
3713 * one or more rows that require a pivot for some values of the parameters
3714 * (i.e., the row can attain both positive and negative signs), then we split
3715 * the context tableau into two parts, one where we force the sign to be
3716 * non-negative and one where we force is to be negative.
3717 * The non-negative part is handled by a recursive call (through find_in_pos).
3718 * Upon returning from this call, we continue with the negative part and
3719 * perform the required pivot.
3721 * If no such rows can be found, all rows are non-negative and we have
3722 * found a (rational) feasible point. If we only wanted a rational point
3723 * then we are done.
3724 * Otherwise, we check if all values of the sample point of the tableau
3725 * are integral for the variables. If so, we have found the minimal
3726 * integral point and we are done.
3727 * If the sample point is not integral, then we need to make a distinction
3728 * based on whether the constant term is non-integral or the coefficients
3729 * of the parameters. Furthermore, in order to decide how to handle
3730 * the non-integrality, we also need to know whether the coefficients
3731 * of the other columns in the tableau are integral. This leads
3732 * to the following table. The first two rows do not correspond
3733 * to a non-integral sample point and are only mentioned for completeness.
3735 * constant parameters other
3737 * int int int |
3738 * int int rat | -> no problem
3740 * rat int int -> fail
3742 * rat int rat -> cut
3744 * int rat rat |
3745 * rat rat rat | -> parametric cut
3747 * int rat int |
3748 * rat rat int | -> split context
3750 * If the parametric constant is completely integral, then there is nothing
3751 * to be done. If the constant term is non-integral, but all the other
3752 * coefficient are integral, then there is nothing that can be done
3753 * and the tableau has no integral solution.
3754 * If, on the other hand, one or more of the other columns have rational
3755 * coefficients, but the parameter coefficients are all integral, then
3756 * we can perform a regular (non-parametric) cut.
3757 * Finally, if there is any parameter coefficient that is non-integral,
3758 * then we need to involve the context tableau. There are two cases here.
3759 * If at least one other column has a rational coefficient, then we
3760 * can perform a parametric cut in the main tableau by adding a new
3761 * integer division in the context tableau.
3762 * If all other columns have integral coefficients, then we need to
3763 * enforce that the rational combination of parameters (c + \sum a_i y_i)/m
3764 * is always integral. We do this by introducing an integer division
3765 * q = floor((c + \sum a_i y_i)/m) and stipulating that its argument should
3766 * always be integral in the context tableau, i.e., m q = c + \sum a_i y_i.
3767 * Since q is expressed in the tableau as
3768 * c + \sum a_i y_i - m q >= 0
3769 * -c - \sum a_i y_i + m q + m - 1 >= 0
3770 * it is sufficient to add the inequality
3771 * -c - \sum a_i y_i + m q >= 0
3772 * In the part of the context where this inequality does not hold, the
3773 * main tableau is marked as being empty.
3775 static void find_solutions(struct isl_sol *sol, struct isl_tab *tab)
3777 struct isl_context *context;
3778 int r;
3780 if (!tab || sol->error)
3781 goto error;
3783 context = sol->context;
3785 if (tab->empty)
3786 goto done;
3787 if (context->op->is_empty(context))
3788 goto done;
3790 for (r = 0; r >= 0 && tab && !tab->empty; r = restore_lexmin(tab)) {
3791 int flags;
3792 int row;
3793 enum isl_tab_row_sign sgn;
3794 int split = -1;
3795 int n_split = 0;
3797 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3798 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
3799 continue;
3800 sgn = row_sign(tab, sol, row);
3801 if (!sgn)
3802 goto error;
3803 tab->row_sign[row] = sgn;
3804 if (sgn == isl_tab_row_any)
3805 n_split++;
3806 if (sgn == isl_tab_row_any && split == -1)
3807 split = row;
3808 if (sgn == isl_tab_row_neg)
3809 break;
3811 if (row < tab->n_row)
3812 continue;
3813 if (split != -1) {
3814 struct isl_vec *ineq;
3815 if (n_split != 1)
3816 split = context->op->best_split(context, tab);
3817 if (split < 0)
3818 goto error;
3819 ineq = get_row_parameter_ineq(tab, split);
3820 if (!ineq)
3821 goto error;
3822 is_strict(ineq);
3823 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3824 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
3825 continue;
3826 if (tab->row_sign[row] == isl_tab_row_any)
3827 tab->row_sign[row] = isl_tab_row_unknown;
3829 tab->row_sign[split] = isl_tab_row_pos;
3830 sol_inc_level(sol);
3831 find_in_pos(sol, tab, ineq->el);
3832 tab->row_sign[split] = isl_tab_row_neg;
3833 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3834 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3835 if (!sol->error)
3836 context->op->add_ineq(context, ineq->el, 0, 1);
3837 isl_vec_free(ineq);
3838 if (sol->error)
3839 goto error;
3840 continue;
3842 if (tab->rational)
3843 break;
3844 row = first_non_integer_row(tab, &flags);
3845 if (row < 0)
3846 break;
3847 if (ISL_FL_ISSET(flags, I_PAR)) {
3848 if (ISL_FL_ISSET(flags, I_VAR)) {
3849 if (isl_tab_mark_empty(tab) < 0)
3850 goto error;
3851 break;
3853 row = add_cut(tab, row);
3854 } else if (ISL_FL_ISSET(flags, I_VAR)) {
3855 struct isl_vec *div;
3856 struct isl_vec *ineq;
3857 int d;
3858 div = get_row_split_div(tab, row);
3859 if (!div)
3860 goto error;
3861 d = context->op->get_div(context, tab, div);
3862 isl_vec_free(div);
3863 if (d < 0)
3864 goto error;
3865 ineq = ineq_for_div(context->op->peek_basic_set(context), d);
3866 if (!ineq)
3867 goto error;
3868 sol_inc_level(sol);
3869 no_sol_in_strict(sol, tab, ineq);
3870 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3871 context->op->add_ineq(context, ineq->el, 1, 1);
3872 isl_vec_free(ineq);
3873 if (sol->error || !context->op->is_ok(context))
3874 goto error;
3875 tab = set_row_cst_to_div(tab, row, d);
3876 if (context->op->is_empty(context))
3877 break;
3878 } else
3879 row = add_parametric_cut(tab, row, context);
3880 if (row < 0)
3881 goto error;
3883 if (r < 0)
3884 goto error;
3885 done:
3886 sol_add(sol, tab);
3887 isl_tab_free(tab);
3888 return;
3889 error:
3890 isl_tab_free(tab);
3891 sol->error = 1;
3894 /* Does "sol" contain a pair of partial solutions that could potentially
3895 * be merged?
3897 * We currently only check that "sol" is not in an error state
3898 * and that there are at least two partial solutions of which the final two
3899 * are defined at the same level.
3901 static int sol_has_mergeable_solutions(struct isl_sol *sol)
3903 if (sol->error)
3904 return 0;
3905 if (!sol->partial)
3906 return 0;
3907 if (!sol->partial->next)
3908 return 0;
3909 return sol->partial->level == sol->partial->next->level;
3912 /* Compute the lexicographic minimum of the set represented by the main
3913 * tableau "tab" within the context "sol->context_tab".
3915 * As a preprocessing step, we first transfer all the purely parametric
3916 * equalities from the main tableau to the context tableau, i.e.,
3917 * parameters that have been pivoted to a row.
3918 * These equalities are ignored by the main algorithm, because the
3919 * corresponding rows may not be marked as being non-negative.
3920 * In parts of the context where the added equality does not hold,
3921 * the main tableau is marked as being empty.
3923 * Before we embark on the actual computation, we save a copy
3924 * of the context. When we return, we check if there are any
3925 * partial solutions that can potentially be merged. If so,
3926 * we perform a rollback to the initial state of the context.
3927 * The merging of partial solutions happens inside calls to
3928 * sol_dec_level that are pushed onto the undo stack of the context.
3929 * If there are no partial solutions that can potentially be merged
3930 * then the rollback is skipped as it would just be wasted effort.
3932 static void find_solutions_main(struct isl_sol *sol, struct isl_tab *tab)
3934 int row;
3935 void *saved;
3937 if (!tab)
3938 goto error;
3940 sol->level = 0;
3942 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3943 int p;
3944 struct isl_vec *eq;
3946 if (tab->row_var[row] < 0)
3947 continue;
3948 if (tab->row_var[row] >= tab->n_param &&
3949 tab->row_var[row] < tab->n_var - tab->n_div)
3950 continue;
3951 if (tab->row_var[row] < tab->n_param)
3952 p = tab->row_var[row];
3953 else
3954 p = tab->row_var[row]
3955 + tab->n_param - (tab->n_var - tab->n_div);
3957 eq = isl_vec_alloc(tab->mat->ctx, 1+tab->n_param+tab->n_div);
3958 if (!eq)
3959 goto error;
3960 get_row_parameter_line(tab, row, eq->el);
3961 isl_int_neg(eq->el[1 + p], tab->mat->row[row][0]);
3962 eq = isl_vec_normalize(eq);
3964 sol_inc_level(sol);
3965 no_sol_in_strict(sol, tab, eq);
3967 isl_seq_neg(eq->el, eq->el, eq->size);
3968 sol_inc_level(sol);
3969 no_sol_in_strict(sol, tab, eq);
3970 isl_seq_neg(eq->el, eq->el, eq->size);
3972 sol->context->op->add_eq(sol->context, eq->el, 1, 1);
3974 isl_vec_free(eq);
3976 if (isl_tab_mark_redundant(tab, row) < 0)
3977 goto error;
3979 if (sol->context->op->is_empty(sol->context))
3980 break;
3982 row = tab->n_redundant - 1;
3985 saved = sol->context->op->save(sol->context);
3987 find_solutions(sol, tab);
3989 if (sol_has_mergeable_solutions(sol))
3990 sol->context->op->restore(sol->context, saved);
3991 else
3992 sol->context->op->discard(saved);
3994 sol->level = 0;
3995 sol_pop(sol);
3997 return;
3998 error:
3999 isl_tab_free(tab);
4000 sol->error = 1;
4003 /* Check if integer division "div" of "dom" also occurs in "bmap".
4004 * If so, return its position within the divs.
4005 * If not, return -1.
4007 static int find_context_div(struct isl_basic_map *bmap,
4008 struct isl_basic_set *dom, unsigned div)
4010 int i;
4011 unsigned b_dim = isl_space_dim(bmap->dim, isl_dim_all);
4012 unsigned d_dim = isl_space_dim(dom->dim, isl_dim_all);
4014 if (isl_int_is_zero(dom->div[div][0]))
4015 return -1;
4016 if (isl_seq_first_non_zero(dom->div[div] + 2 + d_dim, dom->n_div) != -1)
4017 return -1;
4019 for (i = 0; i < bmap->n_div; ++i) {
4020 if (isl_int_is_zero(bmap->div[i][0]))
4021 continue;
4022 if (isl_seq_first_non_zero(bmap->div[i] + 2 + d_dim,
4023 (b_dim - d_dim) + bmap->n_div) != -1)
4024 continue;
4025 if (isl_seq_eq(bmap->div[i], dom->div[div], 2 + d_dim))
4026 return i;
4028 return -1;
4031 /* The correspondence between the variables in the main tableau,
4032 * the context tableau, and the input map and domain is as follows.
4033 * The first n_param and the last n_div variables of the main tableau
4034 * form the variables of the context tableau.
4035 * In the basic map, these n_param variables correspond to the
4036 * parameters and the input dimensions. In the domain, they correspond
4037 * to the parameters and the set dimensions.
4038 * The n_div variables correspond to the integer divisions in the domain.
4039 * To ensure that everything lines up, we may need to copy some of the
4040 * integer divisions of the domain to the map. These have to be placed
4041 * in the same order as those in the context and they have to be placed
4042 * after any other integer divisions that the map may have.
4043 * This function performs the required reordering.
4045 static struct isl_basic_map *align_context_divs(struct isl_basic_map *bmap,
4046 struct isl_basic_set *dom)
4048 int i;
4049 int common = 0;
4050 int other;
4052 for (i = 0; i < dom->n_div; ++i)
4053 if (find_context_div(bmap, dom, i) != -1)
4054 common++;
4055 other = bmap->n_div - common;
4056 if (dom->n_div - common > 0) {
4057 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
4058 dom->n_div - common, 0, 0);
4059 if (!bmap)
4060 return NULL;
4062 for (i = 0; i < dom->n_div; ++i) {
4063 int pos = find_context_div(bmap, dom, i);
4064 if (pos < 0) {
4065 pos = isl_basic_map_alloc_div(bmap);
4066 if (pos < 0)
4067 goto error;
4068 isl_int_set_si(bmap->div[pos][0], 0);
4070 if (pos != other + i)
4071 isl_basic_map_swap_div(bmap, pos, other + i);
4073 return bmap;
4074 error:
4075 isl_basic_map_free(bmap);
4076 return NULL;
4079 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4080 * some obvious symmetries.
4082 * We make sure the divs in the domain are properly ordered,
4083 * because they will be added one by one in the given order
4084 * during the construction of the solution map.
4086 static struct isl_sol *basic_map_partial_lexopt_base(
4087 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4088 __isl_give isl_set **empty, int max,
4089 struct isl_sol *(*init)(__isl_keep isl_basic_map *bmap,
4090 __isl_take isl_basic_set *dom, int track_empty, int max))
4092 struct isl_tab *tab;
4093 struct isl_sol *sol = NULL;
4094 struct isl_context *context;
4096 if (dom->n_div) {
4097 dom = isl_basic_set_order_divs(dom);
4098 bmap = align_context_divs(bmap, dom);
4100 sol = init(bmap, dom, !!empty, max);
4101 if (!sol)
4102 goto error;
4104 context = sol->context;
4105 if (isl_basic_set_plain_is_empty(context->op->peek_basic_set(context)))
4106 /* nothing */;
4107 else if (isl_basic_map_plain_is_empty(bmap)) {
4108 if (sol->add_empty)
4109 sol->add_empty(sol,
4110 isl_basic_set_copy(context->op->peek_basic_set(context)));
4111 } else {
4112 tab = tab_for_lexmin(bmap,
4113 context->op->peek_basic_set(context), 1, max);
4114 tab = context->op->detect_nonnegative_parameters(context, tab);
4115 find_solutions_main(sol, tab);
4117 if (sol->error)
4118 goto error;
4120 isl_basic_map_free(bmap);
4121 return sol;
4122 error:
4123 sol_free(sol);
4124 isl_basic_map_free(bmap);
4125 return NULL;
4128 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4129 * some obvious symmetries.
4131 * We call basic_map_partial_lexopt_base and extract the results.
4133 static __isl_give isl_map *basic_map_partial_lexopt_base_map(
4134 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4135 __isl_give isl_set **empty, int max)
4137 isl_map *result = NULL;
4138 struct isl_sol *sol;
4139 struct isl_sol_map *sol_map;
4141 sol = basic_map_partial_lexopt_base(bmap, dom, empty, max,
4142 &sol_map_init);
4143 if (!sol)
4144 return NULL;
4145 sol_map = (struct isl_sol_map *) sol;
4147 result = isl_map_copy(sol_map->map);
4148 if (empty)
4149 *empty = isl_set_copy(sol_map->empty);
4150 sol_free(&sol_map->sol);
4151 return result;
4154 /* Structure used during detection of parallel constraints.
4155 * n_in: number of "input" variables: isl_dim_param + isl_dim_in
4156 * n_out: number of "output" variables: isl_dim_out + isl_dim_div
4157 * val: the coefficients of the output variables
4159 struct isl_constraint_equal_info {
4160 isl_basic_map *bmap;
4161 unsigned n_in;
4162 unsigned n_out;
4163 isl_int *val;
4166 /* Check whether the coefficients of the output variables
4167 * of the constraint in "entry" are equal to info->val.
4169 static int constraint_equal(const void *entry, const void *val)
4171 isl_int **row = (isl_int **)entry;
4172 const struct isl_constraint_equal_info *info = val;
4174 return isl_seq_eq((*row) + 1 + info->n_in, info->val, info->n_out);
4177 /* Check whether "bmap" has a pair of constraints that have
4178 * the same coefficients for the output variables.
4179 * Note that the coefficients of the existentially quantified
4180 * variables need to be zero since the existentially quantified
4181 * of the result are usually not the same as those of the input.
4182 * the isl_dim_out and isl_dim_div dimensions.
4183 * If so, return 1 and return the row indices of the two constraints
4184 * in *first and *second.
4186 static int parallel_constraints(__isl_keep isl_basic_map *bmap,
4187 int *first, int *second)
4189 int i;
4190 isl_ctx *ctx;
4191 struct isl_hash_table *table = NULL;
4192 struct isl_hash_table_entry *entry;
4193 struct isl_constraint_equal_info info;
4194 unsigned n_out;
4195 unsigned n_div;
4197 ctx = isl_basic_map_get_ctx(bmap);
4198 table = isl_hash_table_alloc(ctx, bmap->n_ineq);
4199 if (!table)
4200 goto error;
4202 info.n_in = isl_basic_map_dim(bmap, isl_dim_param) +
4203 isl_basic_map_dim(bmap, isl_dim_in);
4204 info.bmap = bmap;
4205 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4206 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4207 info.n_out = n_out + n_div;
4208 for (i = 0; i < bmap->n_ineq; ++i) {
4209 uint32_t hash;
4211 info.val = bmap->ineq[i] + 1 + info.n_in;
4212 if (isl_seq_first_non_zero(info.val, n_out) < 0)
4213 continue;
4214 if (isl_seq_first_non_zero(info.val + n_out, n_div) >= 0)
4215 continue;
4216 hash = isl_seq_get_hash(info.val, info.n_out);
4217 entry = isl_hash_table_find(ctx, table, hash,
4218 constraint_equal, &info, 1);
4219 if (!entry)
4220 goto error;
4221 if (entry->data)
4222 break;
4223 entry->data = &bmap->ineq[i];
4226 if (i < bmap->n_ineq) {
4227 *first = ((isl_int **)entry->data) - bmap->ineq;
4228 *second = i;
4231 isl_hash_table_free(ctx, table);
4233 return i < bmap->n_ineq;
4234 error:
4235 isl_hash_table_free(ctx, table);
4236 return -1;
4239 /* Given a set of upper bounds in "var", add constraints to "bset"
4240 * that make the i-th bound smallest.
4242 * In particular, if there are n bounds b_i, then add the constraints
4244 * b_i <= b_j for j > i
4245 * b_i < b_j for j < i
4247 static __isl_give isl_basic_set *select_minimum(__isl_take isl_basic_set *bset,
4248 __isl_keep isl_mat *var, int i)
4250 isl_ctx *ctx;
4251 int j, k;
4253 ctx = isl_mat_get_ctx(var);
4255 for (j = 0; j < var->n_row; ++j) {
4256 if (j == i)
4257 continue;
4258 k = isl_basic_set_alloc_inequality(bset);
4259 if (k < 0)
4260 goto error;
4261 isl_seq_combine(bset->ineq[k], ctx->one, var->row[j],
4262 ctx->negone, var->row[i], var->n_col);
4263 isl_int_set_si(bset->ineq[k][var->n_col], 0);
4264 if (j < i)
4265 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
4268 bset = isl_basic_set_finalize(bset);
4270 return bset;
4271 error:
4272 isl_basic_set_free(bset);
4273 return NULL;
4276 /* Given a set of upper bounds on the last "input" variable m,
4277 * construct a set that assigns the minimal upper bound to m, i.e.,
4278 * construct a set that divides the space into cells where one
4279 * of the upper bounds is smaller than all the others and assign
4280 * this upper bound to m.
4282 * In particular, if there are n bounds b_i, then the result
4283 * consists of n basic sets, each one of the form
4285 * m = b_i
4286 * b_i <= b_j for j > i
4287 * b_i < b_j for j < i
4289 static __isl_give isl_set *set_minimum(__isl_take isl_space *dim,
4290 __isl_take isl_mat *var)
4292 int i, k;
4293 isl_basic_set *bset = NULL;
4294 isl_set *set = NULL;
4296 if (!dim || !var)
4297 goto error;
4299 set = isl_set_alloc_space(isl_space_copy(dim),
4300 var->n_row, ISL_SET_DISJOINT);
4302 for (i = 0; i < var->n_row; ++i) {
4303 bset = isl_basic_set_alloc_space(isl_space_copy(dim), 0,
4304 1, var->n_row - 1);
4305 k = isl_basic_set_alloc_equality(bset);
4306 if (k < 0)
4307 goto error;
4308 isl_seq_cpy(bset->eq[k], var->row[i], var->n_col);
4309 isl_int_set_si(bset->eq[k][var->n_col], -1);
4310 bset = select_minimum(bset, var, i);
4311 set = isl_set_add_basic_set(set, bset);
4314 isl_space_free(dim);
4315 isl_mat_free(var);
4316 return set;
4317 error:
4318 isl_basic_set_free(bset);
4319 isl_set_free(set);
4320 isl_space_free(dim);
4321 isl_mat_free(var);
4322 return NULL;
4325 /* Given that the last input variable of "bmap" represents the minimum
4326 * of the bounds in "cst", check whether we need to split the domain
4327 * based on which bound attains the minimum.
4329 * A split is needed when the minimum appears in an integer division
4330 * or in an equality. Otherwise, it is only needed if it appears in
4331 * an upper bound that is different from the upper bounds on which it
4332 * is defined.
4334 static int need_split_basic_map(__isl_keep isl_basic_map *bmap,
4335 __isl_keep isl_mat *cst)
4337 int i, j;
4338 unsigned total;
4339 unsigned pos;
4341 pos = cst->n_col - 1;
4342 total = isl_basic_map_dim(bmap, isl_dim_all);
4344 for (i = 0; i < bmap->n_div; ++i)
4345 if (!isl_int_is_zero(bmap->div[i][2 + pos]))
4346 return 1;
4348 for (i = 0; i < bmap->n_eq; ++i)
4349 if (!isl_int_is_zero(bmap->eq[i][1 + pos]))
4350 return 1;
4352 for (i = 0; i < bmap->n_ineq; ++i) {
4353 if (isl_int_is_nonneg(bmap->ineq[i][1 + pos]))
4354 continue;
4355 if (!isl_int_is_negone(bmap->ineq[i][1 + pos]))
4356 return 1;
4357 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + pos + 1,
4358 total - pos - 1) >= 0)
4359 return 1;
4361 for (j = 0; j < cst->n_row; ++j)
4362 if (isl_seq_eq(bmap->ineq[i], cst->row[j], cst->n_col))
4363 break;
4364 if (j >= cst->n_row)
4365 return 1;
4368 return 0;
4371 /* Given that the last set variable of "bset" represents the minimum
4372 * of the bounds in "cst", check whether we need to split the domain
4373 * based on which bound attains the minimum.
4375 * We simply call need_split_basic_map here. This is safe because
4376 * the position of the minimum is computed from "cst" and not
4377 * from "bmap".
4379 static int need_split_basic_set(__isl_keep isl_basic_set *bset,
4380 __isl_keep isl_mat *cst)
4382 return need_split_basic_map((isl_basic_map *)bset, cst);
4385 /* Given that the last set variable of "set" represents the minimum
4386 * of the bounds in "cst", check whether we need to split the domain
4387 * based on which bound attains the minimum.
4389 static int need_split_set(__isl_keep isl_set *set, __isl_keep isl_mat *cst)
4391 int i;
4393 for (i = 0; i < set->n; ++i)
4394 if (need_split_basic_set(set->p[i], cst))
4395 return 1;
4397 return 0;
4400 /* Given a set of which the last set variable is the minimum
4401 * of the bounds in "cst", split each basic set in the set
4402 * in pieces where one of the bounds is (strictly) smaller than the others.
4403 * This subdivision is given in "min_expr".
4404 * The variable is subsequently projected out.
4406 * We only do the split when it is needed.
4407 * For example if the last input variable m = min(a,b) and the only
4408 * constraints in the given basic set are lower bounds on m,
4409 * i.e., l <= m = min(a,b), then we can simply project out m
4410 * to obtain l <= a and l <= b, without having to split on whether
4411 * m is equal to a or b.
4413 static __isl_give isl_set *split(__isl_take isl_set *empty,
4414 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
4416 int n_in;
4417 int i;
4418 isl_space *dim;
4419 isl_set *res;
4421 if (!empty || !min_expr || !cst)
4422 goto error;
4424 n_in = isl_set_dim(empty, isl_dim_set);
4425 dim = isl_set_get_space(empty);
4426 dim = isl_space_drop_dims(dim, isl_dim_set, n_in - 1, 1);
4427 res = isl_set_empty(dim);
4429 for (i = 0; i < empty->n; ++i) {
4430 isl_set *set;
4432 set = isl_set_from_basic_set(isl_basic_set_copy(empty->p[i]));
4433 if (need_split_basic_set(empty->p[i], cst))
4434 set = isl_set_intersect(set, isl_set_copy(min_expr));
4435 set = isl_set_remove_dims(set, isl_dim_set, n_in - 1, 1);
4437 res = isl_set_union_disjoint(res, set);
4440 isl_set_free(empty);
4441 isl_set_free(min_expr);
4442 isl_mat_free(cst);
4443 return res;
4444 error:
4445 isl_set_free(empty);
4446 isl_set_free(min_expr);
4447 isl_mat_free(cst);
4448 return NULL;
4451 /* Given a map of which the last input variable is the minimum
4452 * of the bounds in "cst", split each basic set in the set
4453 * in pieces where one of the bounds is (strictly) smaller than the others.
4454 * This subdivision is given in "min_expr".
4455 * The variable is subsequently projected out.
4457 * The implementation is essentially the same as that of "split".
4459 static __isl_give isl_map *split_domain(__isl_take isl_map *opt,
4460 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
4462 int n_in;
4463 int i;
4464 isl_space *dim;
4465 isl_map *res;
4467 if (!opt || !min_expr || !cst)
4468 goto error;
4470 n_in = isl_map_dim(opt, isl_dim_in);
4471 dim = isl_map_get_space(opt);
4472 dim = isl_space_drop_dims(dim, isl_dim_in, n_in - 1, 1);
4473 res = isl_map_empty(dim);
4475 for (i = 0; i < opt->n; ++i) {
4476 isl_map *map;
4478 map = isl_map_from_basic_map(isl_basic_map_copy(opt->p[i]));
4479 if (need_split_basic_map(opt->p[i], cst))
4480 map = isl_map_intersect_domain(map,
4481 isl_set_copy(min_expr));
4482 map = isl_map_remove_dims(map, isl_dim_in, n_in - 1, 1);
4484 res = isl_map_union_disjoint(res, map);
4487 isl_map_free(opt);
4488 isl_set_free(min_expr);
4489 isl_mat_free(cst);
4490 return res;
4491 error:
4492 isl_map_free(opt);
4493 isl_set_free(min_expr);
4494 isl_mat_free(cst);
4495 return NULL;
4498 static __isl_give isl_map *basic_map_partial_lexopt(
4499 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4500 __isl_give isl_set **empty, int max);
4502 union isl_lex_res {
4503 void *p;
4504 isl_map *map;
4505 isl_pw_multi_aff *pma;
4508 /* This function is called from basic_map_partial_lexopt_symm.
4509 * The last variable of "bmap" and "dom" corresponds to the minimum
4510 * of the bounds in "cst". "map_space" is the space of the original
4511 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
4512 * is the space of the original domain.
4514 * We recursively call basic_map_partial_lexopt and then plug in
4515 * the definition of the minimum in the result.
4517 static __isl_give union isl_lex_res basic_map_partial_lexopt_symm_map_core(
4518 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4519 __isl_give isl_set **empty, int max, __isl_take isl_mat *cst,
4520 __isl_take isl_space *map_space, __isl_take isl_space *set_space)
4522 isl_map *opt;
4523 isl_set *min_expr;
4524 union isl_lex_res res;
4526 min_expr = set_minimum(isl_basic_set_get_space(dom), isl_mat_copy(cst));
4528 opt = basic_map_partial_lexopt(bmap, dom, empty, max);
4530 if (empty) {
4531 *empty = split(*empty,
4532 isl_set_copy(min_expr), isl_mat_copy(cst));
4533 *empty = isl_set_reset_space(*empty, set_space);
4536 opt = split_domain(opt, min_expr, cst);
4537 opt = isl_map_reset_space(opt, map_space);
4539 res.map = opt;
4540 return res;
4543 /* Given a basic map with at least two parallel constraints (as found
4544 * by the function parallel_constraints), first look for more constraints
4545 * parallel to the two constraint and replace the found list of parallel
4546 * constraints by a single constraint with as "input" part the minimum
4547 * of the input parts of the list of constraints. Then, recursively call
4548 * basic_map_partial_lexopt (possibly finding more parallel constraints)
4549 * and plug in the definition of the minimum in the result.
4551 * More specifically, given a set of constraints
4553 * a x + b_i(p) >= 0
4555 * Replace this set by a single constraint
4557 * a x + u >= 0
4559 * with u a new parameter with constraints
4561 * u <= b_i(p)
4563 * Any solution to the new system is also a solution for the original system
4564 * since
4566 * a x >= -u >= -b_i(p)
4568 * Moreover, m = min_i(b_i(p)) satisfies the constraints on u and can
4569 * therefore be plugged into the solution.
4571 static union isl_lex_res basic_map_partial_lexopt_symm(
4572 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4573 __isl_give isl_set **empty, int max, int first, int second,
4574 __isl_give union isl_lex_res (*core)(__isl_take isl_basic_map *bmap,
4575 __isl_take isl_basic_set *dom,
4576 __isl_give isl_set **empty,
4577 int max, __isl_take isl_mat *cst,
4578 __isl_take isl_space *map_space,
4579 __isl_take isl_space *set_space))
4581 int i, n, k;
4582 int *list = NULL;
4583 unsigned n_in, n_out, n_div;
4584 isl_ctx *ctx;
4585 isl_vec *var = NULL;
4586 isl_mat *cst = NULL;
4587 isl_space *map_space, *set_space;
4588 union isl_lex_res res;
4590 map_space = isl_basic_map_get_space(bmap);
4591 set_space = empty ? isl_basic_set_get_space(dom) : NULL;
4593 n_in = isl_basic_map_dim(bmap, isl_dim_param) +
4594 isl_basic_map_dim(bmap, isl_dim_in);
4595 n_out = isl_basic_map_dim(bmap, isl_dim_all) - n_in;
4597 ctx = isl_basic_map_get_ctx(bmap);
4598 list = isl_alloc_array(ctx, int, bmap->n_ineq);
4599 var = isl_vec_alloc(ctx, n_out);
4600 if ((bmap->n_ineq && !list) || (n_out && !var))
4601 goto error;
4603 list[0] = first;
4604 list[1] = second;
4605 isl_seq_cpy(var->el, bmap->ineq[first] + 1 + n_in, n_out);
4606 for (i = second + 1, n = 2; i < bmap->n_ineq; ++i) {
4607 if (isl_seq_eq(var->el, bmap->ineq[i] + 1 + n_in, n_out))
4608 list[n++] = i;
4611 cst = isl_mat_alloc(ctx, n, 1 + n_in);
4612 if (!cst)
4613 goto error;
4615 for (i = 0; i < n; ++i)
4616 isl_seq_cpy(cst->row[i], bmap->ineq[list[i]], 1 + n_in);
4618 bmap = isl_basic_map_cow(bmap);
4619 if (!bmap)
4620 goto error;
4621 for (i = n - 1; i >= 0; --i)
4622 if (isl_basic_map_drop_inequality(bmap, list[i]) < 0)
4623 goto error;
4625 bmap = isl_basic_map_add(bmap, isl_dim_in, 1);
4626 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
4627 k = isl_basic_map_alloc_inequality(bmap);
4628 if (k < 0)
4629 goto error;
4630 isl_seq_clr(bmap->ineq[k], 1 + n_in);
4631 isl_int_set_si(bmap->ineq[k][1 + n_in], 1);
4632 isl_seq_cpy(bmap->ineq[k] + 1 + n_in + 1, var->el, n_out);
4633 bmap = isl_basic_map_finalize(bmap);
4635 n_div = isl_basic_set_dim(dom, isl_dim_div);
4636 dom = isl_basic_set_add_dims(dom, isl_dim_set, 1);
4637 dom = isl_basic_set_extend_constraints(dom, 0, n);
4638 for (i = 0; i < n; ++i) {
4639 k = isl_basic_set_alloc_inequality(dom);
4640 if (k < 0)
4641 goto error;
4642 isl_seq_cpy(dom->ineq[k], cst->row[i], 1 + n_in);
4643 isl_int_set_si(dom->ineq[k][1 + n_in], -1);
4644 isl_seq_clr(dom->ineq[k] + 1 + n_in + 1, n_div);
4647 isl_vec_free(var);
4648 free(list);
4650 return core(bmap, dom, empty, max, cst, map_space, set_space);
4651 error:
4652 isl_space_free(map_space);
4653 isl_space_free(set_space);
4654 isl_mat_free(cst);
4655 isl_vec_free(var);
4656 free(list);
4657 isl_basic_set_free(dom);
4658 isl_basic_map_free(bmap);
4659 res.p = NULL;
4660 return res;
4663 static __isl_give isl_map *basic_map_partial_lexopt_symm_map(
4664 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4665 __isl_give isl_set **empty, int max, int first, int second)
4667 return basic_map_partial_lexopt_symm(bmap, dom, empty, max,
4668 first, second, &basic_map_partial_lexopt_symm_map_core).map;
4671 /* Recursive part of isl_tab_basic_map_partial_lexopt, after detecting
4672 * equalities and removing redundant constraints.
4674 * We first check if there are any parallel constraints (left).
4675 * If not, we are in the base case.
4676 * If there are parallel constraints, we replace them by a single
4677 * constraint in basic_map_partial_lexopt_symm and then call
4678 * this function recursively to look for more parallel constraints.
4680 static __isl_give isl_map *basic_map_partial_lexopt(
4681 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4682 __isl_give isl_set **empty, int max)
4684 int par = 0;
4685 int first, second;
4687 if (!bmap)
4688 goto error;
4690 if (bmap->ctx->opt->pip_symmetry)
4691 par = parallel_constraints(bmap, &first, &second);
4692 if (par < 0)
4693 goto error;
4694 if (!par)
4695 return basic_map_partial_lexopt_base_map(bmap, dom, empty, max);
4697 return basic_map_partial_lexopt_symm_map(bmap, dom, empty, max,
4698 first, second);
4699 error:
4700 isl_basic_set_free(dom);
4701 isl_basic_map_free(bmap);
4702 return NULL;
4705 /* Compute the lexicographic minimum (or maximum if "max" is set)
4706 * of "bmap" over the domain "dom" and return the result as a map.
4707 * If "empty" is not NULL, then *empty is assigned a set that
4708 * contains those parts of the domain where there is no solution.
4709 * If "bmap" is marked as rational (ISL_BASIC_MAP_RATIONAL),
4710 * then we compute the rational optimum. Otherwise, we compute
4711 * the integral optimum.
4713 * We perform some preprocessing. As the PILP solver does not
4714 * handle implicit equalities very well, we first make sure all
4715 * the equalities are explicitly available.
4717 * We also add context constraints to the basic map and remove
4718 * redundant constraints. This is only needed because of the
4719 * way we handle simple symmetries. In particular, we currently look
4720 * for symmetries on the constraints, before we set up the main tableau.
4721 * It is then no good to look for symmetries on possibly redundant constraints.
4723 struct isl_map *isl_tab_basic_map_partial_lexopt(
4724 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4725 struct isl_set **empty, int max)
4727 if (empty)
4728 *empty = NULL;
4729 if (!bmap || !dom)
4730 goto error;
4732 isl_assert(bmap->ctx,
4733 isl_basic_map_compatible_domain(bmap, dom), goto error);
4735 if (isl_basic_set_dim(dom, isl_dim_all) == 0)
4736 return basic_map_partial_lexopt(bmap, dom, empty, max);
4738 bmap = isl_basic_map_intersect_domain(bmap, isl_basic_set_copy(dom));
4739 bmap = isl_basic_map_detect_equalities(bmap);
4740 bmap = isl_basic_map_remove_redundancies(bmap);
4742 return basic_map_partial_lexopt(bmap, dom, empty, max);
4743 error:
4744 isl_basic_set_free(dom);
4745 isl_basic_map_free(bmap);
4746 return NULL;
4749 struct isl_sol_for {
4750 struct isl_sol sol;
4751 int (*fn)(__isl_take isl_basic_set *dom,
4752 __isl_take isl_aff_list *list, void *user);
4753 void *user;
4756 static void sol_for_free(struct isl_sol_for *sol_for)
4758 if (!sol_for)
4759 return;
4760 if (sol_for->sol.context)
4761 sol_for->sol.context->op->free(sol_for->sol.context);
4762 free(sol_for);
4765 static void sol_for_free_wrap(struct isl_sol *sol)
4767 sol_for_free((struct isl_sol_for *)sol);
4770 /* Add the solution identified by the tableau and the context tableau.
4772 * See documentation of sol_add for more details.
4774 * Instead of constructing a basic map, this function calls a user
4775 * defined function with the current context as a basic set and
4776 * a list of affine expressions representing the relation between
4777 * the input and output. The space over which the affine expressions
4778 * are defined is the same as that of the domain. The number of
4779 * affine expressions in the list is equal to the number of output variables.
4781 static void sol_for_add(struct isl_sol_for *sol,
4782 struct isl_basic_set *dom, struct isl_mat *M)
4784 int i;
4785 isl_ctx *ctx;
4786 isl_local_space *ls;
4787 isl_aff *aff;
4788 isl_aff_list *list;
4790 if (sol->sol.error || !dom || !M)
4791 goto error;
4793 ctx = isl_basic_set_get_ctx(dom);
4794 ls = isl_basic_set_get_local_space(dom);
4795 list = isl_aff_list_alloc(ctx, M->n_row - 1);
4796 for (i = 1; i < M->n_row; ++i) {
4797 aff = isl_aff_alloc(isl_local_space_copy(ls));
4798 if (aff) {
4799 isl_int_set(aff->v->el[0], M->row[0][0]);
4800 isl_seq_cpy(aff->v->el + 1, M->row[i], M->n_col);
4802 aff = isl_aff_normalize(aff);
4803 list = isl_aff_list_add(list, aff);
4805 isl_local_space_free(ls);
4807 dom = isl_basic_set_finalize(dom);
4809 if (sol->fn(isl_basic_set_copy(dom), list, sol->user) < 0)
4810 goto error;
4812 isl_basic_set_free(dom);
4813 isl_mat_free(M);
4814 return;
4815 error:
4816 isl_basic_set_free(dom);
4817 isl_mat_free(M);
4818 sol->sol.error = 1;
4821 static void sol_for_add_wrap(struct isl_sol *sol,
4822 struct isl_basic_set *dom, struct isl_mat *M)
4824 sol_for_add((struct isl_sol_for *)sol, dom, M);
4827 static struct isl_sol_for *sol_for_init(struct isl_basic_map *bmap, int max,
4828 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4829 void *user),
4830 void *user)
4832 struct isl_sol_for *sol_for = NULL;
4833 isl_space *dom_dim;
4834 struct isl_basic_set *dom = NULL;
4836 sol_for = isl_calloc_type(bmap->ctx, struct isl_sol_for);
4837 if (!sol_for)
4838 goto error;
4840 dom_dim = isl_space_domain(isl_space_copy(bmap->dim));
4841 dom = isl_basic_set_universe(dom_dim);
4843 sol_for->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
4844 sol_for->sol.dec_level.callback.run = &sol_dec_level_wrap;
4845 sol_for->sol.dec_level.sol = &sol_for->sol;
4846 sol_for->fn = fn;
4847 sol_for->user = user;
4848 sol_for->sol.max = max;
4849 sol_for->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
4850 sol_for->sol.add = &sol_for_add_wrap;
4851 sol_for->sol.add_empty = NULL;
4852 sol_for->sol.free = &sol_for_free_wrap;
4854 sol_for->sol.context = isl_context_alloc(dom);
4855 if (!sol_for->sol.context)
4856 goto error;
4858 isl_basic_set_free(dom);
4859 return sol_for;
4860 error:
4861 isl_basic_set_free(dom);
4862 sol_for_free(sol_for);
4863 return NULL;
4866 static void sol_for_find_solutions(struct isl_sol_for *sol_for,
4867 struct isl_tab *tab)
4869 find_solutions_main(&sol_for->sol, tab);
4872 int isl_basic_map_foreach_lexopt(__isl_keep isl_basic_map *bmap, int max,
4873 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4874 void *user),
4875 void *user)
4877 struct isl_sol_for *sol_for = NULL;
4879 bmap = isl_basic_map_copy(bmap);
4880 bmap = isl_basic_map_detect_equalities(bmap);
4881 if (!bmap)
4882 return -1;
4884 sol_for = sol_for_init(bmap, max, fn, user);
4885 if (!sol_for)
4886 goto error;
4888 if (isl_basic_map_plain_is_empty(bmap))
4889 /* nothing */;
4890 else {
4891 struct isl_tab *tab;
4892 struct isl_context *context = sol_for->sol.context;
4893 tab = tab_for_lexmin(bmap,
4894 context->op->peek_basic_set(context), 1, max);
4895 tab = context->op->detect_nonnegative_parameters(context, tab);
4896 sol_for_find_solutions(sol_for, tab);
4897 if (sol_for->sol.error)
4898 goto error;
4901 sol_free(&sol_for->sol);
4902 isl_basic_map_free(bmap);
4903 return 0;
4904 error:
4905 sol_free(&sol_for->sol);
4906 isl_basic_map_free(bmap);
4907 return -1;
4910 int isl_basic_set_foreach_lexopt(__isl_keep isl_basic_set *bset, int max,
4911 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4912 void *user),
4913 void *user)
4915 return isl_basic_map_foreach_lexopt(bset, max, fn, user);
4918 /* Check if the given sequence of len variables starting at pos
4919 * represents a trivial (i.e., zero) solution.
4920 * The variables are assumed to be non-negative and to come in pairs,
4921 * with each pair representing a variable of unrestricted sign.
4922 * The solution is trivial if each such pair in the sequence consists
4923 * of two identical values, meaning that the variable being represented
4924 * has value zero.
4926 static int region_is_trivial(struct isl_tab *tab, int pos, int len)
4928 int i;
4930 if (len == 0)
4931 return 0;
4933 for (i = 0; i < len; i += 2) {
4934 int neg_row;
4935 int pos_row;
4937 neg_row = tab->var[pos + i].is_row ?
4938 tab->var[pos + i].index : -1;
4939 pos_row = tab->var[pos + i + 1].is_row ?
4940 tab->var[pos + i + 1].index : -1;
4942 if ((neg_row < 0 ||
4943 isl_int_is_zero(tab->mat->row[neg_row][1])) &&
4944 (pos_row < 0 ||
4945 isl_int_is_zero(tab->mat->row[pos_row][1])))
4946 continue;
4948 if (neg_row < 0 || pos_row < 0)
4949 return 0;
4950 if (isl_int_ne(tab->mat->row[neg_row][1],
4951 tab->mat->row[pos_row][1]))
4952 return 0;
4955 return 1;
4958 /* Return the index of the first trivial region or -1 if all regions
4959 * are non-trivial.
4961 static int first_trivial_region(struct isl_tab *tab,
4962 int n_region, struct isl_region *region)
4964 int i;
4966 for (i = 0; i < n_region; ++i) {
4967 if (region_is_trivial(tab, region[i].pos, region[i].len))
4968 return i;
4971 return -1;
4974 /* Check if the solution is optimal, i.e., whether the first
4975 * n_op entries are zero.
4977 static int is_optimal(__isl_keep isl_vec *sol, int n_op)
4979 int i;
4981 for (i = 0; i < n_op; ++i)
4982 if (!isl_int_is_zero(sol->el[1 + i]))
4983 return 0;
4984 return 1;
4987 /* Add constraints to "tab" that ensure that any solution is significantly
4988 * better that that represented by "sol". That is, find the first
4989 * relevant (within first n_op) non-zero coefficient and force it (along
4990 * with all previous coefficients) to be zero.
4991 * If the solution is already optimal (all relevant coefficients are zero),
4992 * then just mark the table as empty.
4994 static int force_better_solution(struct isl_tab *tab,
4995 __isl_keep isl_vec *sol, int n_op)
4997 int i;
4998 isl_ctx *ctx;
4999 isl_vec *v = NULL;
5001 if (!sol)
5002 return -1;
5004 for (i = 0; i < n_op; ++i)
5005 if (!isl_int_is_zero(sol->el[1 + i]))
5006 break;
5008 if (i == n_op) {
5009 if (isl_tab_mark_empty(tab) < 0)
5010 return -1;
5011 return 0;
5014 ctx = isl_vec_get_ctx(sol);
5015 v = isl_vec_alloc(ctx, 1 + tab->n_var);
5016 if (!v)
5017 return -1;
5019 for (; i >= 0; --i) {
5020 v = isl_vec_clr(v);
5021 isl_int_set_si(v->el[1 + i], -1);
5022 if (add_lexmin_eq(tab, v->el) < 0)
5023 goto error;
5026 isl_vec_free(v);
5027 return 0;
5028 error:
5029 isl_vec_free(v);
5030 return -1;
5033 struct isl_trivial {
5034 int update;
5035 int region;
5036 int side;
5037 struct isl_tab_undo *snap;
5040 /* Return the lexicographically smallest non-trivial solution of the
5041 * given ILP problem.
5043 * All variables are assumed to be non-negative.
5045 * n_op is the number of initial coordinates to optimize.
5046 * That is, once a solution has been found, we will only continue looking
5047 * for solution that result in significantly better values for those
5048 * initial coordinates. That is, we only continue looking for solutions
5049 * that increase the number of initial zeros in this sequence.
5051 * A solution is non-trivial, if it is non-trivial on each of the
5052 * specified regions. Each region represents a sequence of pairs
5053 * of variables. A solution is non-trivial on such a region if
5054 * at least one of these pairs consists of different values, i.e.,
5055 * such that the non-negative variable represented by the pair is non-zero.
5057 * Whenever a conflict is encountered, all constraints involved are
5058 * reported to the caller through a call to "conflict".
5060 * We perform a simple branch-and-bound backtracking search.
5061 * Each level in the search represents initially trivial region that is forced
5062 * to be non-trivial.
5063 * At each level we consider n cases, where n is the length of the region.
5064 * In terms of the n/2 variables of unrestricted signs being encoded by
5065 * the region, we consider the cases
5066 * x_0 >= 1
5067 * x_0 <= -1
5068 * x_0 = 0 and x_1 >= 1
5069 * x_0 = 0 and x_1 <= -1
5070 * x_0 = 0 and x_1 = 0 and x_2 >= 1
5071 * x_0 = 0 and x_1 = 0 and x_2 <= -1
5072 * ...
5073 * The cases are considered in this order, assuming that each pair
5074 * x_i_a x_i_b represents the value x_i_b - x_i_a.
5075 * That is, x_0 >= 1 is enforced by adding the constraint
5076 * x_0_b - x_0_a >= 1
5078 __isl_give isl_vec *isl_tab_basic_set_non_trivial_lexmin(
5079 __isl_take isl_basic_set *bset, int n_op, int n_region,
5080 struct isl_region *region,
5081 int (*conflict)(int con, void *user), void *user)
5083 int i, j;
5084 int r;
5085 isl_ctx *ctx;
5086 isl_vec *v = NULL;
5087 isl_vec *sol = NULL;
5088 struct isl_tab *tab;
5089 struct isl_trivial *triv = NULL;
5090 int level, init;
5092 if (!bset)
5093 return NULL;
5095 ctx = isl_basic_set_get_ctx(bset);
5096 sol = isl_vec_alloc(ctx, 0);
5098 tab = tab_for_lexmin(bset, NULL, 0, 0);
5099 if (!tab)
5100 goto error;
5101 tab->conflict = conflict;
5102 tab->conflict_user = user;
5104 v = isl_vec_alloc(ctx, 1 + tab->n_var);
5105 triv = isl_calloc_array(ctx, struct isl_trivial, n_region);
5106 if (!v || (n_region && !triv))
5107 goto error;
5109 level = 0;
5110 init = 1;
5112 while (level >= 0) {
5113 int side, base;
5115 if (init) {
5116 tab = cut_to_integer_lexmin(tab, CUT_ONE);
5117 if (!tab)
5118 goto error;
5119 if (tab->empty)
5120 goto backtrack;
5121 r = first_trivial_region(tab, n_region, region);
5122 if (r < 0) {
5123 for (i = 0; i < level; ++i)
5124 triv[i].update = 1;
5125 isl_vec_free(sol);
5126 sol = isl_tab_get_sample_value(tab);
5127 if (!sol)
5128 goto error;
5129 if (is_optimal(sol, n_op))
5130 break;
5131 goto backtrack;
5133 if (level >= n_region)
5134 isl_die(ctx, isl_error_internal,
5135 "nesting level too deep", goto error);
5136 if (isl_tab_extend_cons(tab,
5137 2 * region[r].len + 2 * n_op) < 0)
5138 goto error;
5139 triv[level].region = r;
5140 triv[level].side = 0;
5143 r = triv[level].region;
5144 side = triv[level].side;
5145 base = 2 * (side/2);
5147 if (side >= region[r].len) {
5148 backtrack:
5149 level--;
5150 init = 0;
5151 if (level >= 0)
5152 if (isl_tab_rollback(tab, triv[level].snap) < 0)
5153 goto error;
5154 continue;
5157 if (triv[level].update) {
5158 if (force_better_solution(tab, sol, n_op) < 0)
5159 goto error;
5160 triv[level].update = 0;
5163 if (side == base && base >= 2) {
5164 for (j = base - 2; j < base; ++j) {
5165 v = isl_vec_clr(v);
5166 isl_int_set_si(v->el[1 + region[r].pos + j], 1);
5167 if (add_lexmin_eq(tab, v->el) < 0)
5168 goto error;
5172 triv[level].snap = isl_tab_snap(tab);
5173 if (isl_tab_push_basis(tab) < 0)
5174 goto error;
5176 v = isl_vec_clr(v);
5177 isl_int_set_si(v->el[0], -1);
5178 isl_int_set_si(v->el[1 + region[r].pos + side], -1);
5179 isl_int_set_si(v->el[1 + region[r].pos + (side ^ 1)], 1);
5180 tab = add_lexmin_ineq(tab, v->el);
5182 triv[level].side++;
5183 level++;
5184 init = 1;
5187 free(triv);
5188 isl_vec_free(v);
5189 isl_tab_free(tab);
5190 isl_basic_set_free(bset);
5192 return sol;
5193 error:
5194 free(triv);
5195 isl_vec_free(v);
5196 isl_tab_free(tab);
5197 isl_basic_set_free(bset);
5198 isl_vec_free(sol);
5199 return NULL;
5202 /* Return the lexicographically smallest rational point in "bset",
5203 * assuming that all variables are non-negative.
5204 * If "bset" is empty, then return a zero-length vector.
5206 __isl_give isl_vec *isl_tab_basic_set_non_neg_lexmin(
5207 __isl_take isl_basic_set *bset)
5209 struct isl_tab *tab;
5210 isl_ctx *ctx = isl_basic_set_get_ctx(bset);
5211 isl_vec *sol;
5213 if (!bset)
5214 return NULL;
5216 tab = tab_for_lexmin(bset, NULL, 0, 0);
5217 if (!tab)
5218 goto error;
5219 if (tab->empty)
5220 sol = isl_vec_alloc(ctx, 0);
5221 else
5222 sol = isl_tab_get_sample_value(tab);
5223 isl_tab_free(tab);
5224 isl_basic_set_free(bset);
5225 return sol;
5226 error:
5227 isl_tab_free(tab);
5228 isl_basic_set_free(bset);
5229 return NULL;
5232 struct isl_sol_pma {
5233 struct isl_sol sol;
5234 isl_pw_multi_aff *pma;
5235 isl_set *empty;
5238 static void sol_pma_free(struct isl_sol_pma *sol_pma)
5240 if (!sol_pma)
5241 return;
5242 if (sol_pma->sol.context)
5243 sol_pma->sol.context->op->free(sol_pma->sol.context);
5244 isl_pw_multi_aff_free(sol_pma->pma);
5245 isl_set_free(sol_pma->empty);
5246 free(sol_pma);
5249 /* This function is called for parts of the context where there is
5250 * no solution, with "bset" corresponding to the context tableau.
5251 * Simply add the basic set to the set "empty".
5253 static void sol_pma_add_empty(struct isl_sol_pma *sol,
5254 __isl_take isl_basic_set *bset)
5256 if (!bset)
5257 goto error;
5258 isl_assert(bset->ctx, sol->empty, goto error);
5260 sol->empty = isl_set_grow(sol->empty, 1);
5261 bset = isl_basic_set_simplify(bset);
5262 bset = isl_basic_set_finalize(bset);
5263 sol->empty = isl_set_add_basic_set(sol->empty, bset);
5264 if (!sol->empty)
5265 sol->sol.error = 1;
5266 return;
5267 error:
5268 isl_basic_set_free(bset);
5269 sol->sol.error = 1;
5272 /* Given a basic map "dom" that represents the context and an affine
5273 * matrix "M" that maps the dimensions of the context to the
5274 * output variables, construct an isl_pw_multi_aff with a single
5275 * cell corresponding to "dom" and affine expressions copied from "M".
5277 static void sol_pma_add(struct isl_sol_pma *sol,
5278 __isl_take isl_basic_set *dom, __isl_take isl_mat *M)
5280 int i;
5281 isl_local_space *ls;
5282 isl_aff *aff;
5283 isl_multi_aff *maff;
5284 isl_pw_multi_aff *pma;
5286 maff = isl_multi_aff_alloc(isl_pw_multi_aff_get_space(sol->pma));
5287 ls = isl_basic_set_get_local_space(dom);
5288 for (i = 1; i < M->n_row; ++i) {
5289 aff = isl_aff_alloc(isl_local_space_copy(ls));
5290 if (aff) {
5291 isl_int_set(aff->v->el[0], M->row[0][0]);
5292 isl_seq_cpy(aff->v->el + 1, M->row[i], M->n_col);
5294 aff = isl_aff_normalize(aff);
5295 maff = isl_multi_aff_set_aff(maff, i - 1, aff);
5297 isl_local_space_free(ls);
5298 isl_mat_free(M);
5299 dom = isl_basic_set_simplify(dom);
5300 dom = isl_basic_set_finalize(dom);
5301 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(dom), maff);
5302 sol->pma = isl_pw_multi_aff_add_disjoint(sol->pma, pma);
5303 if (!sol->pma)
5304 sol->sol.error = 1;
5307 static void sol_pma_free_wrap(struct isl_sol *sol)
5309 sol_pma_free((struct isl_sol_pma *)sol);
5312 static void sol_pma_add_empty_wrap(struct isl_sol *sol,
5313 __isl_take isl_basic_set *bset)
5315 sol_pma_add_empty((struct isl_sol_pma *)sol, bset);
5318 static void sol_pma_add_wrap(struct isl_sol *sol,
5319 __isl_take isl_basic_set *dom, __isl_take isl_mat *M)
5321 sol_pma_add((struct isl_sol_pma *)sol, dom, M);
5324 /* Construct an isl_sol_pma structure for accumulating the solution.
5325 * If track_empty is set, then we also keep track of the parts
5326 * of the context where there is no solution.
5327 * If max is set, then we are solving a maximization, rather than
5328 * a minimization problem, which means that the variables in the
5329 * tableau have value "M - x" rather than "M + x".
5331 static struct isl_sol *sol_pma_init(__isl_keep isl_basic_map *bmap,
5332 __isl_take isl_basic_set *dom, int track_empty, int max)
5334 struct isl_sol_pma *sol_pma = NULL;
5336 if (!bmap)
5337 goto error;
5339 sol_pma = isl_calloc_type(bmap->ctx, struct isl_sol_pma);
5340 if (!sol_pma)
5341 goto error;
5343 sol_pma->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
5344 sol_pma->sol.dec_level.callback.run = &sol_dec_level_wrap;
5345 sol_pma->sol.dec_level.sol = &sol_pma->sol;
5346 sol_pma->sol.max = max;
5347 sol_pma->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
5348 sol_pma->sol.add = &sol_pma_add_wrap;
5349 sol_pma->sol.add_empty = track_empty ? &sol_pma_add_empty_wrap : NULL;
5350 sol_pma->sol.free = &sol_pma_free_wrap;
5351 sol_pma->pma = isl_pw_multi_aff_empty(isl_basic_map_get_space(bmap));
5352 if (!sol_pma->pma)
5353 goto error;
5355 sol_pma->sol.context = isl_context_alloc(dom);
5356 if (!sol_pma->sol.context)
5357 goto error;
5359 if (track_empty) {
5360 sol_pma->empty = isl_set_alloc_space(isl_basic_set_get_space(dom),
5361 1, ISL_SET_DISJOINT);
5362 if (!sol_pma->empty)
5363 goto error;
5366 isl_basic_set_free(dom);
5367 return &sol_pma->sol;
5368 error:
5369 isl_basic_set_free(dom);
5370 sol_pma_free(sol_pma);
5371 return NULL;
5374 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
5375 * some obvious symmetries.
5377 * We call basic_map_partial_lexopt_base and extract the results.
5379 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_base_pma(
5380 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5381 __isl_give isl_set **empty, int max)
5383 isl_pw_multi_aff *result = NULL;
5384 struct isl_sol *sol;
5385 struct isl_sol_pma *sol_pma;
5387 sol = basic_map_partial_lexopt_base(bmap, dom, empty, max,
5388 &sol_pma_init);
5389 if (!sol)
5390 return NULL;
5391 sol_pma = (struct isl_sol_pma *) sol;
5393 result = isl_pw_multi_aff_copy(sol_pma->pma);
5394 if (empty)
5395 *empty = isl_set_copy(sol_pma->empty);
5396 sol_free(&sol_pma->sol);
5397 return result;
5400 /* Given that the last input variable of "maff" represents the minimum
5401 * of some bounds, check whether we need to plug in the expression
5402 * of the minimum.
5404 * In particular, check if the last input variable appears in any
5405 * of the expressions in "maff".
5407 static int need_substitution(__isl_keep isl_multi_aff *maff)
5409 int i;
5410 unsigned pos;
5412 pos = isl_multi_aff_dim(maff, isl_dim_in) - 1;
5414 for (i = 0; i < maff->n; ++i)
5415 if (isl_aff_involves_dims(maff->p[i], isl_dim_in, pos, 1))
5416 return 1;
5418 return 0;
5421 /* Given a set of upper bounds on the last "input" variable m,
5422 * construct a piecewise affine expression that selects
5423 * the minimal upper bound to m, i.e.,
5424 * divide the space into cells where one
5425 * of the upper bounds is smaller than all the others and select
5426 * this upper bound on that cell.
5428 * In particular, if there are n bounds b_i, then the result
5429 * consists of n cell, each one of the form
5431 * b_i <= b_j for j > i
5432 * b_i < b_j for j < i
5434 * The affine expression on this cell is
5436 * b_i
5438 static __isl_give isl_pw_aff *set_minimum_pa(__isl_take isl_space *space,
5439 __isl_take isl_mat *var)
5441 int i;
5442 isl_aff *aff = NULL;
5443 isl_basic_set *bset = NULL;
5444 isl_pw_aff *paff = NULL;
5445 isl_space *pw_space;
5446 isl_local_space *ls = NULL;
5448 if (!space || !var)
5449 goto error;
5451 ls = isl_local_space_from_space(isl_space_copy(space));
5452 pw_space = isl_space_copy(space);
5453 pw_space = isl_space_from_domain(pw_space);
5454 pw_space = isl_space_add_dims(pw_space, isl_dim_out, 1);
5455 paff = isl_pw_aff_alloc_size(pw_space, var->n_row);
5457 for (i = 0; i < var->n_row; ++i) {
5458 isl_pw_aff *paff_i;
5460 aff = isl_aff_alloc(isl_local_space_copy(ls));
5461 bset = isl_basic_set_alloc_space(isl_space_copy(space), 0,
5462 0, var->n_row - 1);
5463 if (!aff || !bset)
5464 goto error;
5465 isl_int_set_si(aff->v->el[0], 1);
5466 isl_seq_cpy(aff->v->el + 1, var->row[i], var->n_col);
5467 isl_int_set_si(aff->v->el[1 + var->n_col], 0);
5468 bset = select_minimum(bset, var, i);
5469 paff_i = isl_pw_aff_alloc(isl_set_from_basic_set(bset), aff);
5470 paff = isl_pw_aff_add_disjoint(paff, paff_i);
5473 isl_local_space_free(ls);
5474 isl_space_free(space);
5475 isl_mat_free(var);
5476 return paff;
5477 error:
5478 isl_aff_free(aff);
5479 isl_basic_set_free(bset);
5480 isl_pw_aff_free(paff);
5481 isl_local_space_free(ls);
5482 isl_space_free(space);
5483 isl_mat_free(var);
5484 return NULL;
5487 /* Given a piecewise multi-affine expression of which the last input variable
5488 * is the minimum of the bounds in "cst", plug in the value of the minimum.
5489 * This minimum expression is given in "min_expr_pa".
5490 * The set "min_expr" contains the same information, but in the form of a set.
5491 * The variable is subsequently projected out.
5493 * The implementation is similar to those of "split" and "split_domain".
5494 * If the variable appears in a given expression, then minimum expression
5495 * is plugged in. Otherwise, if the variable appears in the constraints
5496 * and a split is required, then the domain is split. Otherwise, no split
5497 * is performed.
5499 static __isl_give isl_pw_multi_aff *split_domain_pma(
5500 __isl_take isl_pw_multi_aff *opt, __isl_take isl_pw_aff *min_expr_pa,
5501 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
5503 int n_in;
5504 int i;
5505 isl_space *space;
5506 isl_pw_multi_aff *res;
5508 if (!opt || !min_expr || !cst)
5509 goto error;
5511 n_in = isl_pw_multi_aff_dim(opt, isl_dim_in);
5512 space = isl_pw_multi_aff_get_space(opt);
5513 space = isl_space_drop_dims(space, isl_dim_in, n_in - 1, 1);
5514 res = isl_pw_multi_aff_empty(space);
5516 for (i = 0; i < opt->n; ++i) {
5517 isl_pw_multi_aff *pma;
5519 pma = isl_pw_multi_aff_alloc(isl_set_copy(opt->p[i].set),
5520 isl_multi_aff_copy(opt->p[i].maff));
5521 if (need_substitution(opt->p[i].maff))
5522 pma = isl_pw_multi_aff_substitute(pma,
5523 isl_dim_in, n_in - 1, min_expr_pa);
5524 else if (need_split_set(opt->p[i].set, cst))
5525 pma = isl_pw_multi_aff_intersect_domain(pma,
5526 isl_set_copy(min_expr));
5527 pma = isl_pw_multi_aff_project_out(pma,
5528 isl_dim_in, n_in - 1, 1);
5530 res = isl_pw_multi_aff_add_disjoint(res, pma);
5533 isl_pw_multi_aff_free(opt);
5534 isl_pw_aff_free(min_expr_pa);
5535 isl_set_free(min_expr);
5536 isl_mat_free(cst);
5537 return res;
5538 error:
5539 isl_pw_multi_aff_free(opt);
5540 isl_pw_aff_free(min_expr_pa);
5541 isl_set_free(min_expr);
5542 isl_mat_free(cst);
5543 return NULL;
5546 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_pma(
5547 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5548 __isl_give isl_set **empty, int max);
5550 /* This function is called from basic_map_partial_lexopt_symm.
5551 * The last variable of "bmap" and "dom" corresponds to the minimum
5552 * of the bounds in "cst". "map_space" is the space of the original
5553 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
5554 * is the space of the original domain.
5556 * We recursively call basic_map_partial_lexopt and then plug in
5557 * the definition of the minimum in the result.
5559 static __isl_give union isl_lex_res basic_map_partial_lexopt_symm_pma_core(
5560 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5561 __isl_give isl_set **empty, int max, __isl_take isl_mat *cst,
5562 __isl_take isl_space *map_space, __isl_take isl_space *set_space)
5564 isl_pw_multi_aff *opt;
5565 isl_pw_aff *min_expr_pa;
5566 isl_set *min_expr;
5567 union isl_lex_res res;
5569 min_expr = set_minimum(isl_basic_set_get_space(dom), isl_mat_copy(cst));
5570 min_expr_pa = set_minimum_pa(isl_basic_set_get_space(dom),
5571 isl_mat_copy(cst));
5573 opt = basic_map_partial_lexopt_pma(bmap, dom, empty, max);
5575 if (empty) {
5576 *empty = split(*empty,
5577 isl_set_copy(min_expr), isl_mat_copy(cst));
5578 *empty = isl_set_reset_space(*empty, set_space);
5581 opt = split_domain_pma(opt, min_expr_pa, min_expr, cst);
5582 opt = isl_pw_multi_aff_reset_space(opt, map_space);
5584 res.pma = opt;
5585 return res;
5588 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_symm_pma(
5589 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5590 __isl_give isl_set **empty, int max, int first, int second)
5592 return basic_map_partial_lexopt_symm(bmap, dom, empty, max,
5593 first, second, &basic_map_partial_lexopt_symm_pma_core).pma;
5596 /* Recursive part of isl_basic_map_partial_lexopt_pw_multi_aff, after detecting
5597 * equalities and removing redundant constraints.
5599 * We first check if there are any parallel constraints (left).
5600 * If not, we are in the base case.
5601 * If there are parallel constraints, we replace them by a single
5602 * constraint in basic_map_partial_lexopt_symm_pma and then call
5603 * this function recursively to look for more parallel constraints.
5605 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_pma(
5606 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5607 __isl_give isl_set **empty, int max)
5609 int par = 0;
5610 int first, second;
5612 if (!bmap)
5613 goto error;
5615 if (bmap->ctx->opt->pip_symmetry)
5616 par = parallel_constraints(bmap, &first, &second);
5617 if (par < 0)
5618 goto error;
5619 if (!par)
5620 return basic_map_partial_lexopt_base_pma(bmap, dom, empty, max);
5622 return basic_map_partial_lexopt_symm_pma(bmap, dom, empty, max,
5623 first, second);
5624 error:
5625 isl_basic_set_free(dom);
5626 isl_basic_map_free(bmap);
5627 return NULL;
5630 /* Compute the lexicographic minimum (or maximum if "max" is set)
5631 * of "bmap" over the domain "dom" and return the result as a piecewise
5632 * multi-affine expression.
5633 * If "empty" is not NULL, then *empty is assigned a set that
5634 * contains those parts of the domain where there is no solution.
5635 * If "bmap" is marked as rational (ISL_BASIC_MAP_RATIONAL),
5636 * then we compute the rational optimum. Otherwise, we compute
5637 * the integral optimum.
5639 * We perform some preprocessing. As the PILP solver does not
5640 * handle implicit equalities very well, we first make sure all
5641 * the equalities are explicitly available.
5643 * We also add context constraints to the basic map and remove
5644 * redundant constraints. This is only needed because of the
5645 * way we handle simple symmetries. In particular, we currently look
5646 * for symmetries on the constraints, before we set up the main tableau.
5647 * It is then no good to look for symmetries on possibly redundant constraints.
5649 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexopt_pw_multi_aff(
5650 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5651 __isl_give isl_set **empty, int max)
5653 if (empty)
5654 *empty = NULL;
5655 if (!bmap || !dom)
5656 goto error;
5658 isl_assert(bmap->ctx,
5659 isl_basic_map_compatible_domain(bmap, dom), goto error);
5661 if (isl_basic_set_dim(dom, isl_dim_all) == 0)
5662 return basic_map_partial_lexopt_pma(bmap, dom, empty, max);
5664 bmap = isl_basic_map_intersect_domain(bmap, isl_basic_set_copy(dom));
5665 bmap = isl_basic_map_detect_equalities(bmap);
5666 bmap = isl_basic_map_remove_redundancies(bmap);
5668 return basic_map_partial_lexopt_pma(bmap, dom, empty, max);
5669 error:
5670 isl_basic_set_free(dom);
5671 isl_basic_map_free(bmap);
5672 return NULL;