isl_tab_pip.c: isl_context_op.add_div: return isl_bool
[isl.git] / isl_tab_pip.c
blob20efe196ac7296127abb30e20866eaa641968f74
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2016 Sven Verdoolaege
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
14 #include <isl_ctx_private.h>
15 #include "isl_map_private.h"
16 #include <isl_seq.h>
17 #include "isl_tab.h"
18 #include "isl_sample.h"
19 #include <isl_mat_private.h>
20 #include <isl_vec_private.h>
21 #include <isl_aff_private.h>
22 #include <isl_constraint_private.h>
23 #include <isl_options_private.h>
24 #include <isl_config.h>
27 * The implementation of parametric integer linear programming in this file
28 * was inspired by the paper "Parametric Integer Programming" and the
29 * report "Solving systems of affine (in)equalities" by Paul Feautrier
30 * (and others).
32 * The strategy used for obtaining a feasible solution is different
33 * from the one used in isl_tab.c. In particular, in isl_tab.c,
34 * upon finding a constraint that is not yet satisfied, we pivot
35 * in a row that increases the constant term of the row holding the
36 * constraint, making sure the sample solution remains feasible
37 * for all the constraints it already satisfied.
38 * Here, we always pivot in the row holding the constraint,
39 * choosing a column that induces the lexicographically smallest
40 * increment to the sample solution.
42 * By starting out from a sample value that is lexicographically
43 * smaller than any integer point in the problem space, the first
44 * feasible integer sample point we find will also be the lexicographically
45 * smallest. If all variables can be assumed to be non-negative,
46 * then the initial sample value may be chosen equal to zero.
47 * However, we will not make this assumption. Instead, we apply
48 * the "big parameter" trick. Any variable x is then not directly
49 * used in the tableau, but instead it is represented by another
50 * variable x' = M + x, where M is an arbitrarily large (positive)
51 * value. x' is therefore always non-negative, whatever the value of x.
52 * Taking as initial sample value x' = 0 corresponds to x = -M,
53 * which is always smaller than any possible value of x.
55 * The big parameter trick is used in the main tableau and
56 * also in the context tableau if isl_context_lex is used.
57 * In this case, each tableaus has its own big parameter.
58 * Before doing any real work, we check if all the parameters
59 * happen to be non-negative. If so, we drop the column corresponding
60 * to M from the initial context tableau.
61 * If isl_context_gbr is used, then the big parameter trick is only
62 * used in the main tableau.
65 struct isl_context;
66 struct isl_context_op {
67 /* detect nonnegative parameters in context and mark them in tab */
68 struct isl_tab *(*detect_nonnegative_parameters)(
69 struct isl_context *context, struct isl_tab *tab);
70 /* return temporary reference to basic set representation of context */
71 struct isl_basic_set *(*peek_basic_set)(struct isl_context *context);
72 /* return temporary reference to tableau representation of context */
73 struct isl_tab *(*peek_tab)(struct isl_context *context);
74 /* add equality; check is 1 if eq may not be valid;
75 * update is 1 if we may want to call ineq_sign on context later.
77 void (*add_eq)(struct isl_context *context, isl_int *eq,
78 int check, int update);
79 /* add inequality; check is 1 if ineq may not be valid;
80 * update is 1 if we may want to call ineq_sign on context later.
82 void (*add_ineq)(struct isl_context *context, isl_int *ineq,
83 int check, int update);
84 /* check sign of ineq based on previous information.
85 * strict is 1 if saturation should be treated as a positive sign.
87 enum isl_tab_row_sign (*ineq_sign)(struct isl_context *context,
88 isl_int *ineq, int strict);
89 /* check if inequality maintains feasibility */
90 int (*test_ineq)(struct isl_context *context, isl_int *ineq);
91 /* return index of a div that corresponds to "div" */
92 int (*get_div)(struct isl_context *context, struct isl_tab *tab,
93 struct isl_vec *div);
94 /* add div "div" to context and return non-negativity */
95 isl_bool (*add_div)(struct isl_context *context,
96 __isl_keep isl_vec *div);
97 int (*detect_equalities)(struct isl_context *context,
98 struct isl_tab *tab);
99 /* return row index of "best" split */
100 int (*best_split)(struct isl_context *context, struct isl_tab *tab);
101 /* check if context has already been determined to be empty */
102 int (*is_empty)(struct isl_context *context);
103 /* check if context is still usable */
104 int (*is_ok)(struct isl_context *context);
105 /* save a copy/snapshot of context */
106 void *(*save)(struct isl_context *context);
107 /* restore saved context */
108 void (*restore)(struct isl_context *context, void *);
109 /* discard saved context */
110 void (*discard)(void *);
111 /* invalidate context */
112 void (*invalidate)(struct isl_context *context);
113 /* free context */
114 void (*free)(struct isl_context *context);
117 struct isl_context {
118 struct isl_context_op *op;
121 struct isl_context_lex {
122 struct isl_context context;
123 struct isl_tab *tab;
126 /* A stack (linked list) of solutions of subtrees of the search space.
128 * "M" describes the solution in terms of the dimensions of "dom".
129 * The number of columns of "M" is one more than the total number
130 * of dimensions of "dom".
132 * If "M" is NULL, then there is no solution on "dom".
134 struct isl_partial_sol {
135 int level;
136 struct isl_basic_set *dom;
137 struct isl_mat *M;
139 struct isl_partial_sol *next;
142 struct isl_sol;
143 struct isl_sol_callback {
144 struct isl_tab_callback callback;
145 struct isl_sol *sol;
148 /* isl_sol is an interface for constructing a solution to
149 * a parametric integer linear programming problem.
150 * Every time the algorithm reaches a state where a solution
151 * can be read off from the tableau (including cases where the tableau
152 * is empty), the function "add" is called on the isl_sol passed
153 * to find_solutions_main.
155 * The context tableau is owned by isl_sol and is updated incrementally.
157 * There are currently two implementations of this interface,
158 * isl_sol_map, which simply collects the solutions in an isl_map
159 * and (optionally) the parts of the context where there is no solution
160 * in an isl_set, and
161 * isl_sol_for, which calls a user-defined function for each part of
162 * the solution.
164 struct isl_sol {
165 int error;
166 int rational;
167 int level;
168 int max;
169 int n_out;
170 struct isl_context *context;
171 struct isl_partial_sol *partial;
172 void (*add)(struct isl_sol *sol,
173 struct isl_basic_set *dom, struct isl_mat *M);
174 void (*add_empty)(struct isl_sol *sol, struct isl_basic_set *bset);
175 void (*free)(struct isl_sol *sol);
176 struct isl_sol_callback dec_level;
179 static void sol_free(struct isl_sol *sol)
181 struct isl_partial_sol *partial, *next;
182 if (!sol)
183 return;
184 for (partial = sol->partial; partial; partial = next) {
185 next = partial->next;
186 isl_basic_set_free(partial->dom);
187 isl_mat_free(partial->M);
188 free(partial);
190 sol->free(sol);
193 /* Push a partial solution represented by a domain and mapping M
194 * onto the stack of partial solutions.
196 static void sol_push_sol(struct isl_sol *sol,
197 struct isl_basic_set *dom, struct isl_mat *M)
199 struct isl_partial_sol *partial;
201 if (sol->error || !dom)
202 goto error;
204 partial = isl_alloc_type(dom->ctx, struct isl_partial_sol);
205 if (!partial)
206 goto error;
208 partial->level = sol->level;
209 partial->dom = dom;
210 partial->M = M;
211 partial->next = sol->partial;
213 sol->partial = partial;
215 return;
216 error:
217 isl_basic_set_free(dom);
218 isl_mat_free(M);
219 sol->error = 1;
222 /* Pop one partial solution from the partial solution stack and
223 * pass it on to sol->add or sol->add_empty.
225 static void sol_pop_one(struct isl_sol *sol)
227 struct isl_partial_sol *partial;
229 partial = sol->partial;
230 sol->partial = partial->next;
232 if (partial->M)
233 sol->add(sol, partial->dom, partial->M);
234 else
235 sol->add_empty(sol, partial->dom);
236 free(partial);
239 /* Return a fresh copy of the domain represented by the context tableau.
241 static struct isl_basic_set *sol_domain(struct isl_sol *sol)
243 struct isl_basic_set *bset;
245 if (sol->error)
246 return NULL;
248 bset = isl_basic_set_dup(sol->context->op->peek_basic_set(sol->context));
249 bset = isl_basic_set_update_from_tab(bset,
250 sol->context->op->peek_tab(sol->context));
252 return bset;
255 /* Check whether two partial solutions have the same mapping, where n_div
256 * is the number of divs that the two partial solutions have in common.
258 static int same_solution(struct isl_partial_sol *s1, struct isl_partial_sol *s2,
259 unsigned n_div)
261 int i;
262 unsigned dim;
264 if (!s1->M != !s2->M)
265 return 0;
266 if (!s1->M)
267 return 1;
269 dim = isl_basic_set_total_dim(s1->dom) - s1->dom->n_div;
271 for (i = 0; i < s1->M->n_row; ++i) {
272 if (isl_seq_first_non_zero(s1->M->row[i]+1+dim+n_div,
273 s1->M->n_col-1-dim-n_div) != -1)
274 return 0;
275 if (isl_seq_first_non_zero(s2->M->row[i]+1+dim+n_div,
276 s2->M->n_col-1-dim-n_div) != -1)
277 return 0;
278 if (!isl_seq_eq(s1->M->row[i], s2->M->row[i], 1+dim+n_div))
279 return 0;
281 return 1;
284 /* Pop all solutions from the partial solution stack that were pushed onto
285 * the stack at levels that are deeper than the current level.
286 * If the two topmost elements on the stack have the same level
287 * and represent the same solution, then their domains are combined.
288 * This combined domain is the same as the current context domain
289 * as sol_pop is called each time we move back to a higher level.
290 * If the outer level (0) has been reached, then all partial solutions
291 * at the current level are also popped off.
293 static void sol_pop(struct isl_sol *sol)
295 struct isl_partial_sol *partial;
296 unsigned n_div;
298 if (sol->error)
299 return;
301 partial = sol->partial;
302 if (!partial)
303 return;
305 if (partial->level == 0 && sol->level == 0) {
306 for (partial = sol->partial; partial; partial = sol->partial)
307 sol_pop_one(sol);
308 return;
311 if (partial->level <= sol->level)
312 return;
314 if (partial->next && partial->next->level == partial->level) {
315 n_div = isl_basic_set_dim(
316 sol->context->op->peek_basic_set(sol->context),
317 isl_dim_div);
319 if (!same_solution(partial, partial->next, n_div)) {
320 sol_pop_one(sol);
321 sol_pop_one(sol);
322 } else {
323 struct isl_basic_set *bset;
324 isl_mat *M;
325 unsigned n;
327 n = isl_basic_set_dim(partial->next->dom, isl_dim_div);
328 n -= n_div;
329 bset = sol_domain(sol);
330 isl_basic_set_free(partial->next->dom);
331 partial->next->dom = bset;
332 M = partial->next->M;
333 if (M) {
334 M = isl_mat_drop_cols(M, M->n_col - n, n);
335 partial->next->M = M;
336 if (!M)
337 goto error;
339 partial->next->level = sol->level;
341 if (!bset)
342 goto error;
344 sol->partial = partial->next;
345 isl_basic_set_free(partial->dom);
346 isl_mat_free(partial->M);
347 free(partial);
349 } else
350 sol_pop_one(sol);
352 if (sol->level == 0) {
353 for (partial = sol->partial; partial; partial = sol->partial)
354 sol_pop_one(sol);
355 return;
358 if (0)
359 error: sol->error = 1;
362 static void sol_dec_level(struct isl_sol *sol)
364 if (sol->error)
365 return;
367 sol->level--;
369 sol_pop(sol);
372 static int sol_dec_level_wrap(struct isl_tab_callback *cb)
374 struct isl_sol_callback *callback = (struct isl_sol_callback *)cb;
376 sol_dec_level(callback->sol);
378 return callback->sol->error ? -1 : 0;
381 /* Move down to next level and push callback onto context tableau
382 * to decrease the level again when it gets rolled back across
383 * the current state. That is, dec_level will be called with
384 * the context tableau in the same state as it is when inc_level
385 * is called.
387 static void sol_inc_level(struct isl_sol *sol)
389 struct isl_tab *tab;
391 if (sol->error)
392 return;
394 sol->level++;
395 tab = sol->context->op->peek_tab(sol->context);
396 if (isl_tab_push_callback(tab, &sol->dec_level.callback) < 0)
397 sol->error = 1;
400 static void scale_rows(struct isl_mat *mat, isl_int m, int n_row)
402 int i;
404 if (isl_int_is_one(m))
405 return;
407 for (i = 0; i < n_row; ++i)
408 isl_seq_scale(mat->row[i], mat->row[i], m, mat->n_col);
411 /* Add the solution identified by the tableau and the context tableau.
413 * The layout of the variables is as follows.
414 * tab->n_var is equal to the total number of variables in the input
415 * map (including divs that were copied from the context)
416 * + the number of extra divs constructed
417 * Of these, the first tab->n_param and the last tab->n_div variables
418 * correspond to the variables in the context, i.e.,
419 * tab->n_param + tab->n_div = context_tab->n_var
420 * tab->n_param is equal to the number of parameters and input
421 * dimensions in the input map
422 * tab->n_div is equal to the number of divs in the context
424 * If there is no solution, then call add_empty with a basic set
425 * that corresponds to the context tableau. (If add_empty is NULL,
426 * then do nothing).
428 * If there is a solution, then first construct a matrix that maps
429 * all dimensions of the context to the output variables, i.e.,
430 * the output dimensions in the input map.
431 * The divs in the input map (if any) that do not correspond to any
432 * div in the context do not appear in the solution.
433 * The algorithm will make sure that they have an integer value,
434 * but these values themselves are of no interest.
435 * We have to be careful not to drop or rearrange any divs in the
436 * context because that would change the meaning of the matrix.
438 * To extract the value of the output variables, it should be noted
439 * that we always use a big parameter M in the main tableau and so
440 * the variable stored in this tableau is not an output variable x itself, but
441 * x' = M + x (in case of minimization)
442 * or
443 * x' = M - x (in case of maximization)
444 * If x' appears in a column, then its optimal value is zero,
445 * which means that the optimal value of x is an unbounded number
446 * (-M for minimization and M for maximization).
447 * We currently assume that the output dimensions in the original map
448 * are bounded, so this cannot occur.
449 * Similarly, when x' appears in a row, then the coefficient of M in that
450 * row is necessarily 1.
451 * If the row in the tableau represents
452 * d x' = c + d M + e(y)
453 * then, in case of minimization, the corresponding row in the matrix
454 * will be
455 * a c + a e(y)
456 * with a d = m, the (updated) common denominator of the matrix.
457 * In case of maximization, the row will be
458 * -a c - a e(y)
460 static void sol_add(struct isl_sol *sol, struct isl_tab *tab)
462 struct isl_basic_set *bset = NULL;
463 struct isl_mat *mat = NULL;
464 unsigned off;
465 int row;
466 isl_int m;
468 if (sol->error || !tab)
469 goto error;
471 if (tab->empty && !sol->add_empty)
472 return;
473 if (sol->context->op->is_empty(sol->context))
474 return;
476 bset = sol_domain(sol);
478 if (tab->empty) {
479 sol_push_sol(sol, bset, NULL);
480 return;
483 off = 2 + tab->M;
485 mat = isl_mat_alloc(tab->mat->ctx, 1 + sol->n_out,
486 1 + tab->n_param + tab->n_div);
487 if (!mat)
488 goto error;
490 isl_int_init(m);
492 isl_seq_clr(mat->row[0] + 1, mat->n_col - 1);
493 isl_int_set_si(mat->row[0][0], 1);
494 for (row = 0; row < sol->n_out; ++row) {
495 int i = tab->n_param + row;
496 int r, j;
498 isl_seq_clr(mat->row[1 + row], mat->n_col);
499 if (!tab->var[i].is_row) {
500 if (tab->M)
501 isl_die(mat->ctx, isl_error_invalid,
502 "unbounded optimum", goto error2);
503 continue;
506 r = tab->var[i].index;
507 if (tab->M &&
508 isl_int_ne(tab->mat->row[r][2], tab->mat->row[r][0]))
509 isl_die(mat->ctx, isl_error_invalid,
510 "unbounded optimum", goto error2);
511 isl_int_gcd(m, mat->row[0][0], tab->mat->row[r][0]);
512 isl_int_divexact(m, tab->mat->row[r][0], m);
513 scale_rows(mat, m, 1 + row);
514 isl_int_divexact(m, mat->row[0][0], tab->mat->row[r][0]);
515 isl_int_mul(mat->row[1 + row][0], m, tab->mat->row[r][1]);
516 for (j = 0; j < tab->n_param; ++j) {
517 int col;
518 if (tab->var[j].is_row)
519 continue;
520 col = tab->var[j].index;
521 isl_int_mul(mat->row[1 + row][1 + j], m,
522 tab->mat->row[r][off + col]);
524 for (j = 0; j < tab->n_div; ++j) {
525 int col;
526 if (tab->var[tab->n_var - tab->n_div+j].is_row)
527 continue;
528 col = tab->var[tab->n_var - tab->n_div+j].index;
529 isl_int_mul(mat->row[1 + row][1 + tab->n_param + j], m,
530 tab->mat->row[r][off + col]);
532 if (sol->max)
533 isl_seq_neg(mat->row[1 + row], mat->row[1 + row],
534 mat->n_col);
537 isl_int_clear(m);
539 sol_push_sol(sol, bset, mat);
540 return;
541 error2:
542 isl_int_clear(m);
543 error:
544 isl_basic_set_free(bset);
545 isl_mat_free(mat);
546 sol->error = 1;
549 struct isl_sol_map {
550 struct isl_sol sol;
551 struct isl_map *map;
552 struct isl_set *empty;
555 static void sol_map_free(struct isl_sol_map *sol_map)
557 if (!sol_map)
558 return;
559 if (sol_map->sol.context)
560 sol_map->sol.context->op->free(sol_map->sol.context);
561 isl_map_free(sol_map->map);
562 isl_set_free(sol_map->empty);
563 free(sol_map);
566 static void sol_map_free_wrap(struct isl_sol *sol)
568 sol_map_free((struct isl_sol_map *)sol);
571 /* This function is called for parts of the context where there is
572 * no solution, with "bset" corresponding to the context tableau.
573 * Simply add the basic set to the set "empty".
575 static void sol_map_add_empty(struct isl_sol_map *sol,
576 struct isl_basic_set *bset)
578 if (!bset || !sol->empty)
579 goto error;
581 sol->empty = isl_set_grow(sol->empty, 1);
582 bset = isl_basic_set_simplify(bset);
583 bset = isl_basic_set_finalize(bset);
584 sol->empty = isl_set_add_basic_set(sol->empty, isl_basic_set_copy(bset));
585 if (!sol->empty)
586 goto error;
587 isl_basic_set_free(bset);
588 return;
589 error:
590 isl_basic_set_free(bset);
591 sol->sol.error = 1;
594 static void sol_map_add_empty_wrap(struct isl_sol *sol,
595 struct isl_basic_set *bset)
597 sol_map_add_empty((struct isl_sol_map *)sol, bset);
600 /* Given a basic map "dom" that represents the context and an affine
601 * matrix "M" that maps the dimensions of the context to the
602 * output variables, construct a basic map with the same parameters
603 * and divs as the context, the dimensions of the context as input
604 * dimensions and a number of output dimensions that is equal to
605 * the number of output dimensions in the input map.
607 * The constraints and divs of the context are simply copied
608 * from "dom". For each row
609 * x = c + e(y)
610 * an equality
611 * c + e(y) - d x = 0
612 * is added, with d the common denominator of M.
614 static void sol_map_add(struct isl_sol_map *sol,
615 struct isl_basic_set *dom, struct isl_mat *M)
617 int i;
618 struct isl_basic_map *bmap = NULL;
619 unsigned n_eq;
620 unsigned n_ineq;
621 unsigned nparam;
622 unsigned total;
623 unsigned n_div;
624 unsigned n_out;
626 if (sol->sol.error || !dom || !M)
627 goto error;
629 n_out = sol->sol.n_out;
630 n_eq = dom->n_eq + n_out;
631 n_ineq = dom->n_ineq;
632 n_div = dom->n_div;
633 nparam = isl_basic_set_total_dim(dom) - n_div;
634 total = isl_map_dim(sol->map, isl_dim_all);
635 bmap = isl_basic_map_alloc_space(isl_map_get_space(sol->map),
636 n_div, n_eq, 2 * n_div + n_ineq);
637 if (!bmap)
638 goto error;
639 if (sol->sol.rational)
640 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
641 for (i = 0; i < dom->n_div; ++i) {
642 int k = isl_basic_map_alloc_div(bmap);
643 if (k < 0)
644 goto error;
645 isl_seq_cpy(bmap->div[k], dom->div[i], 1 + 1 + nparam);
646 isl_seq_clr(bmap->div[k] + 1 + 1 + nparam, total - nparam);
647 isl_seq_cpy(bmap->div[k] + 1 + 1 + total,
648 dom->div[i] + 1 + 1 + nparam, i);
650 for (i = 0; i < dom->n_eq; ++i) {
651 int k = isl_basic_map_alloc_equality(bmap);
652 if (k < 0)
653 goto error;
654 isl_seq_cpy(bmap->eq[k], dom->eq[i], 1 + nparam);
655 isl_seq_clr(bmap->eq[k] + 1 + nparam, total - nparam);
656 isl_seq_cpy(bmap->eq[k] + 1 + total,
657 dom->eq[i] + 1 + nparam, n_div);
659 for (i = 0; i < dom->n_ineq; ++i) {
660 int k = isl_basic_map_alloc_inequality(bmap);
661 if (k < 0)
662 goto error;
663 isl_seq_cpy(bmap->ineq[k], dom->ineq[i], 1 + nparam);
664 isl_seq_clr(bmap->ineq[k] + 1 + nparam, total - nparam);
665 isl_seq_cpy(bmap->ineq[k] + 1 + total,
666 dom->ineq[i] + 1 + nparam, n_div);
668 for (i = 0; i < M->n_row - 1; ++i) {
669 int k = isl_basic_map_alloc_equality(bmap);
670 if (k < 0)
671 goto error;
672 isl_seq_cpy(bmap->eq[k], M->row[1 + i], 1 + nparam);
673 isl_seq_clr(bmap->eq[k] + 1 + nparam, n_out);
674 isl_int_neg(bmap->eq[k][1 + nparam + i], M->row[0][0]);
675 isl_seq_cpy(bmap->eq[k] + 1 + nparam + n_out,
676 M->row[1 + i] + 1 + nparam, n_div);
678 bmap = isl_basic_map_simplify(bmap);
679 bmap = isl_basic_map_finalize(bmap);
680 sol->map = isl_map_grow(sol->map, 1);
681 sol->map = isl_map_add_basic_map(sol->map, bmap);
682 isl_basic_set_free(dom);
683 isl_mat_free(M);
684 if (!sol->map)
685 sol->sol.error = 1;
686 return;
687 error:
688 isl_basic_set_free(dom);
689 isl_mat_free(M);
690 isl_basic_map_free(bmap);
691 sol->sol.error = 1;
694 static void sol_map_add_wrap(struct isl_sol *sol,
695 struct isl_basic_set *dom, struct isl_mat *M)
697 sol_map_add((struct isl_sol_map *)sol, dom, M);
701 /* Store the "parametric constant" of row "row" of tableau "tab" in "line",
702 * i.e., the constant term and the coefficients of all variables that
703 * appear in the context tableau.
704 * Note that the coefficient of the big parameter M is NOT copied.
705 * The context tableau may not have a big parameter and even when it
706 * does, it is a different big parameter.
708 static void get_row_parameter_line(struct isl_tab *tab, int row, isl_int *line)
710 int i;
711 unsigned off = 2 + tab->M;
713 isl_int_set(line[0], tab->mat->row[row][1]);
714 for (i = 0; i < tab->n_param; ++i) {
715 if (tab->var[i].is_row)
716 isl_int_set_si(line[1 + i], 0);
717 else {
718 int col = tab->var[i].index;
719 isl_int_set(line[1 + i], tab->mat->row[row][off + col]);
722 for (i = 0; i < tab->n_div; ++i) {
723 if (tab->var[tab->n_var - tab->n_div + i].is_row)
724 isl_int_set_si(line[1 + tab->n_param + i], 0);
725 else {
726 int col = tab->var[tab->n_var - tab->n_div + i].index;
727 isl_int_set(line[1 + tab->n_param + i],
728 tab->mat->row[row][off + col]);
733 /* Check if rows "row1" and "row2" have identical "parametric constants",
734 * as explained above.
735 * In this case, we also insist that the coefficients of the big parameter
736 * be the same as the values of the constants will only be the same
737 * if these coefficients are also the same.
739 static int identical_parameter_line(struct isl_tab *tab, int row1, int row2)
741 int i;
742 unsigned off = 2 + tab->M;
744 if (isl_int_ne(tab->mat->row[row1][1], tab->mat->row[row2][1]))
745 return 0;
747 if (tab->M && isl_int_ne(tab->mat->row[row1][2],
748 tab->mat->row[row2][2]))
749 return 0;
751 for (i = 0; i < tab->n_param + tab->n_div; ++i) {
752 int pos = i < tab->n_param ? i :
753 tab->n_var - tab->n_div + i - tab->n_param;
754 int col;
756 if (tab->var[pos].is_row)
757 continue;
758 col = tab->var[pos].index;
759 if (isl_int_ne(tab->mat->row[row1][off + col],
760 tab->mat->row[row2][off + col]))
761 return 0;
763 return 1;
766 /* Return an inequality that expresses that the "parametric constant"
767 * should be non-negative.
768 * This function is only called when the coefficient of the big parameter
769 * is equal to zero.
771 static struct isl_vec *get_row_parameter_ineq(struct isl_tab *tab, int row)
773 struct isl_vec *ineq;
775 ineq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_param + tab->n_div);
776 if (!ineq)
777 return NULL;
779 get_row_parameter_line(tab, row, ineq->el);
780 if (ineq)
781 ineq = isl_vec_normalize(ineq);
783 return ineq;
786 /* Normalize a div expression of the form
788 * [(g*f(x) + c)/(g * m)]
790 * with c the constant term and f(x) the remaining coefficients, to
792 * [(f(x) + [c/g])/m]
794 static void normalize_div(__isl_keep isl_vec *div)
796 isl_ctx *ctx = isl_vec_get_ctx(div);
797 int len = div->size - 2;
799 isl_seq_gcd(div->el + 2, len, &ctx->normalize_gcd);
800 isl_int_gcd(ctx->normalize_gcd, ctx->normalize_gcd, div->el[0]);
802 if (isl_int_is_one(ctx->normalize_gcd))
803 return;
805 isl_int_divexact(div->el[0], div->el[0], ctx->normalize_gcd);
806 isl_int_fdiv_q(div->el[1], div->el[1], ctx->normalize_gcd);
807 isl_seq_scale_down(div->el + 2, div->el + 2, ctx->normalize_gcd, len);
810 /* Return a integer division for use in a parametric cut based on the given row.
811 * In particular, let the parametric constant of the row be
813 * \sum_i a_i y_i
815 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
816 * The div returned is equal to
818 * floor(\sum_i {-a_i} y_i) = floor((\sum_i (-a_i mod d) y_i)/d)
820 static struct isl_vec *get_row_parameter_div(struct isl_tab *tab, int row)
822 struct isl_vec *div;
824 div = isl_vec_alloc(tab->mat->ctx, 1 + 1 + tab->n_param + tab->n_div);
825 if (!div)
826 return NULL;
828 isl_int_set(div->el[0], tab->mat->row[row][0]);
829 get_row_parameter_line(tab, row, div->el + 1);
830 isl_seq_neg(div->el + 1, div->el + 1, div->size - 1);
831 normalize_div(div);
832 isl_seq_fdiv_r(div->el + 1, div->el + 1, div->el[0], div->size - 1);
834 return div;
837 /* Return a integer division for use in transferring an integrality constraint
838 * to the context.
839 * In particular, let the parametric constant of the row be
841 * \sum_i a_i y_i
843 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
844 * The the returned div is equal to
846 * floor(\sum_i {a_i} y_i) = floor((\sum_i (a_i mod d) y_i)/d)
848 static struct isl_vec *get_row_split_div(struct isl_tab *tab, int row)
850 struct isl_vec *div;
852 div = isl_vec_alloc(tab->mat->ctx, 1 + 1 + tab->n_param + tab->n_div);
853 if (!div)
854 return NULL;
856 isl_int_set(div->el[0], tab->mat->row[row][0]);
857 get_row_parameter_line(tab, row, div->el + 1);
858 normalize_div(div);
859 isl_seq_fdiv_r(div->el + 1, div->el + 1, div->el[0], div->size - 1);
861 return div;
864 /* Construct and return an inequality that expresses an upper bound
865 * on the given div.
866 * In particular, if the div is given by
868 * d = floor(e/m)
870 * then the inequality expresses
872 * m d <= e
874 static struct isl_vec *ineq_for_div(struct isl_basic_set *bset, unsigned div)
876 unsigned total;
877 unsigned div_pos;
878 struct isl_vec *ineq;
880 if (!bset)
881 return NULL;
883 total = isl_basic_set_total_dim(bset);
884 div_pos = 1 + total - bset->n_div + div;
886 ineq = isl_vec_alloc(bset->ctx, 1 + total);
887 if (!ineq)
888 return NULL;
890 isl_seq_cpy(ineq->el, bset->div[div] + 1, 1 + total);
891 isl_int_neg(ineq->el[div_pos], bset->div[div][0]);
892 return ineq;
895 /* Given a row in the tableau and a div that was created
896 * using get_row_split_div and that has been constrained to equality, i.e.,
898 * d = floor(\sum_i {a_i} y_i) = \sum_i {a_i} y_i
900 * replace the expression "\sum_i {a_i} y_i" in the row by d,
901 * i.e., we subtract "\sum_i {a_i} y_i" and add 1 d.
902 * The coefficients of the non-parameters in the tableau have been
903 * verified to be integral. We can therefore simply replace coefficient b
904 * by floor(b). For the coefficients of the parameters we have
905 * floor(a_i) = a_i - {a_i}, while for the other coefficients, we have
906 * floor(b) = b.
908 static struct isl_tab *set_row_cst_to_div(struct isl_tab *tab, int row, int div)
910 isl_seq_fdiv_q(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
911 tab->mat->row[row][0], 1 + tab->M + tab->n_col);
913 isl_int_set_si(tab->mat->row[row][0], 1);
915 if (tab->var[tab->n_var - tab->n_div + div].is_row) {
916 int drow = tab->var[tab->n_var - tab->n_div + div].index;
918 isl_assert(tab->mat->ctx,
919 isl_int_is_one(tab->mat->row[drow][0]), goto error);
920 isl_seq_combine(tab->mat->row[row] + 1,
921 tab->mat->ctx->one, tab->mat->row[row] + 1,
922 tab->mat->ctx->one, tab->mat->row[drow] + 1,
923 1 + tab->M + tab->n_col);
924 } else {
925 int dcol = tab->var[tab->n_var - tab->n_div + div].index;
927 isl_int_add_ui(tab->mat->row[row][2 + tab->M + dcol],
928 tab->mat->row[row][2 + tab->M + dcol], 1);
931 return tab;
932 error:
933 isl_tab_free(tab);
934 return NULL;
937 /* Check if the (parametric) constant of the given row is obviously
938 * negative, meaning that we don't need to consult the context tableau.
939 * If there is a big parameter and its coefficient is non-zero,
940 * then this coefficient determines the outcome.
941 * Otherwise, we check whether the constant is negative and
942 * all non-zero coefficients of parameters are negative and
943 * belong to non-negative parameters.
945 static int is_obviously_neg(struct isl_tab *tab, int row)
947 int i;
948 int col;
949 unsigned off = 2 + tab->M;
951 if (tab->M) {
952 if (isl_int_is_pos(tab->mat->row[row][2]))
953 return 0;
954 if (isl_int_is_neg(tab->mat->row[row][2]))
955 return 1;
958 if (isl_int_is_nonneg(tab->mat->row[row][1]))
959 return 0;
960 for (i = 0; i < tab->n_param; ++i) {
961 /* Eliminated parameter */
962 if (tab->var[i].is_row)
963 continue;
964 col = tab->var[i].index;
965 if (isl_int_is_zero(tab->mat->row[row][off + col]))
966 continue;
967 if (!tab->var[i].is_nonneg)
968 return 0;
969 if (isl_int_is_pos(tab->mat->row[row][off + col]))
970 return 0;
972 for (i = 0; i < tab->n_div; ++i) {
973 if (tab->var[tab->n_var - tab->n_div + i].is_row)
974 continue;
975 col = tab->var[tab->n_var - tab->n_div + i].index;
976 if (isl_int_is_zero(tab->mat->row[row][off + col]))
977 continue;
978 if (!tab->var[tab->n_var - tab->n_div + i].is_nonneg)
979 return 0;
980 if (isl_int_is_pos(tab->mat->row[row][off + col]))
981 return 0;
983 return 1;
986 /* Check if the (parametric) constant of the given row is obviously
987 * non-negative, meaning that we don't need to consult the context tableau.
988 * If there is a big parameter and its coefficient is non-zero,
989 * then this coefficient determines the outcome.
990 * Otherwise, we check whether the constant is non-negative and
991 * all non-zero coefficients of parameters are positive and
992 * belong to non-negative parameters.
994 static int is_obviously_nonneg(struct isl_tab *tab, int row)
996 int i;
997 int col;
998 unsigned off = 2 + tab->M;
1000 if (tab->M) {
1001 if (isl_int_is_pos(tab->mat->row[row][2]))
1002 return 1;
1003 if (isl_int_is_neg(tab->mat->row[row][2]))
1004 return 0;
1007 if (isl_int_is_neg(tab->mat->row[row][1]))
1008 return 0;
1009 for (i = 0; i < tab->n_param; ++i) {
1010 /* Eliminated parameter */
1011 if (tab->var[i].is_row)
1012 continue;
1013 col = tab->var[i].index;
1014 if (isl_int_is_zero(tab->mat->row[row][off + col]))
1015 continue;
1016 if (!tab->var[i].is_nonneg)
1017 return 0;
1018 if (isl_int_is_neg(tab->mat->row[row][off + col]))
1019 return 0;
1021 for (i = 0; i < tab->n_div; ++i) {
1022 if (tab->var[tab->n_var - tab->n_div + i].is_row)
1023 continue;
1024 col = tab->var[tab->n_var - tab->n_div + i].index;
1025 if (isl_int_is_zero(tab->mat->row[row][off + col]))
1026 continue;
1027 if (!tab->var[tab->n_var - tab->n_div + i].is_nonneg)
1028 return 0;
1029 if (isl_int_is_neg(tab->mat->row[row][off + col]))
1030 return 0;
1032 return 1;
1035 /* Given a row r and two columns, return the column that would
1036 * lead to the lexicographically smallest increment in the sample
1037 * solution when leaving the basis in favor of the row.
1038 * Pivoting with column c will increment the sample value by a non-negative
1039 * constant times a_{V,c}/a_{r,c}, with a_{V,c} the elements of column c
1040 * corresponding to the non-parametric variables.
1041 * If variable v appears in a column c_v, the a_{v,c} = 1 iff c = c_v,
1042 * with all other entries in this virtual row equal to zero.
1043 * If variable v appears in a row, then a_{v,c} is the element in column c
1044 * of that row.
1046 * Let v be the first variable with a_{v,c1}/a_{r,c1} != a_{v,c2}/a_{r,c2}.
1047 * Then if a_{v,c1}/a_{r,c1} < a_{v,c2}/a_{r,c2}, i.e.,
1048 * a_{v,c2} a_{r,c1} - a_{v,c1} a_{r,c2} > 0, c1 results in the minimal
1049 * increment. Otherwise, it's c2.
1051 static int lexmin_col_pair(struct isl_tab *tab,
1052 int row, int col1, int col2, isl_int tmp)
1054 int i;
1055 isl_int *tr;
1057 tr = tab->mat->row[row] + 2 + tab->M;
1059 for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
1060 int s1, s2;
1061 isl_int *r;
1063 if (!tab->var[i].is_row) {
1064 if (tab->var[i].index == col1)
1065 return col2;
1066 if (tab->var[i].index == col2)
1067 return col1;
1068 continue;
1071 if (tab->var[i].index == row)
1072 continue;
1074 r = tab->mat->row[tab->var[i].index] + 2 + tab->M;
1075 s1 = isl_int_sgn(r[col1]);
1076 s2 = isl_int_sgn(r[col2]);
1077 if (s1 == 0 && s2 == 0)
1078 continue;
1079 if (s1 < s2)
1080 return col1;
1081 if (s2 < s1)
1082 return col2;
1084 isl_int_mul(tmp, r[col2], tr[col1]);
1085 isl_int_submul(tmp, r[col1], tr[col2]);
1086 if (isl_int_is_pos(tmp))
1087 return col1;
1088 if (isl_int_is_neg(tmp))
1089 return col2;
1091 return -1;
1094 /* Given a row in the tableau, find and return the column that would
1095 * result in the lexicographically smallest, but positive, increment
1096 * in the sample point.
1097 * If there is no such column, then return tab->n_col.
1098 * If anything goes wrong, return -1.
1100 static int lexmin_pivot_col(struct isl_tab *tab, int row)
1102 int j;
1103 int col = tab->n_col;
1104 isl_int *tr;
1105 isl_int tmp;
1107 tr = tab->mat->row[row] + 2 + tab->M;
1109 isl_int_init(tmp);
1111 for (j = tab->n_dead; j < tab->n_col; ++j) {
1112 if (tab->col_var[j] >= 0 &&
1113 (tab->col_var[j] < tab->n_param ||
1114 tab->col_var[j] >= tab->n_var - tab->n_div))
1115 continue;
1117 if (!isl_int_is_pos(tr[j]))
1118 continue;
1120 if (col == tab->n_col)
1121 col = j;
1122 else
1123 col = lexmin_col_pair(tab, row, col, j, tmp);
1124 isl_assert(tab->mat->ctx, col >= 0, goto error);
1127 isl_int_clear(tmp);
1128 return col;
1129 error:
1130 isl_int_clear(tmp);
1131 return -1;
1134 /* Return the first known violated constraint, i.e., a non-negative
1135 * constraint that currently has an either obviously negative value
1136 * or a previously determined to be negative value.
1138 * If any constraint has a negative coefficient for the big parameter,
1139 * if any, then we return one of these first.
1141 static int first_neg(struct isl_tab *tab)
1143 int row;
1145 if (tab->M)
1146 for (row = tab->n_redundant; row < tab->n_row; ++row) {
1147 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
1148 continue;
1149 if (!isl_int_is_neg(tab->mat->row[row][2]))
1150 continue;
1151 if (tab->row_sign)
1152 tab->row_sign[row] = isl_tab_row_neg;
1153 return row;
1155 for (row = tab->n_redundant; row < tab->n_row; ++row) {
1156 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
1157 continue;
1158 if (tab->row_sign) {
1159 if (tab->row_sign[row] == 0 &&
1160 is_obviously_neg(tab, row))
1161 tab->row_sign[row] = isl_tab_row_neg;
1162 if (tab->row_sign[row] != isl_tab_row_neg)
1163 continue;
1164 } else if (!is_obviously_neg(tab, row))
1165 continue;
1166 return row;
1168 return -1;
1171 /* Check whether the invariant that all columns are lexico-positive
1172 * is satisfied. This function is not called from the current code
1173 * but is useful during debugging.
1175 static void check_lexpos(struct isl_tab *tab) __attribute__ ((unused));
1176 static void check_lexpos(struct isl_tab *tab)
1178 unsigned off = 2 + tab->M;
1179 int col;
1180 int var;
1181 int row;
1183 for (col = tab->n_dead; col < tab->n_col; ++col) {
1184 if (tab->col_var[col] >= 0 &&
1185 (tab->col_var[col] < tab->n_param ||
1186 tab->col_var[col] >= tab->n_var - tab->n_div))
1187 continue;
1188 for (var = tab->n_param; var < tab->n_var - tab->n_div; ++var) {
1189 if (!tab->var[var].is_row) {
1190 if (tab->var[var].index == col)
1191 break;
1192 else
1193 continue;
1195 row = tab->var[var].index;
1196 if (isl_int_is_zero(tab->mat->row[row][off + col]))
1197 continue;
1198 if (isl_int_is_pos(tab->mat->row[row][off + col]))
1199 break;
1200 fprintf(stderr, "lexneg column %d (row %d)\n",
1201 col, row);
1203 if (var >= tab->n_var - tab->n_div)
1204 fprintf(stderr, "zero column %d\n", col);
1208 /* Report to the caller that the given constraint is part of an encountered
1209 * conflict.
1211 static int report_conflicting_constraint(struct isl_tab *tab, int con)
1213 return tab->conflict(con, tab->conflict_user);
1216 /* Given a conflicting row in the tableau, report all constraints
1217 * involved in the row to the caller. That is, the row itself
1218 * (if it represents a constraint) and all constraint columns with
1219 * non-zero (and therefore negative) coefficients.
1221 static int report_conflict(struct isl_tab *tab, int row)
1223 int j;
1224 isl_int *tr;
1226 if (!tab->conflict)
1227 return 0;
1229 if (tab->row_var[row] < 0 &&
1230 report_conflicting_constraint(tab, ~tab->row_var[row]) < 0)
1231 return -1;
1233 tr = tab->mat->row[row] + 2 + tab->M;
1235 for (j = tab->n_dead; j < tab->n_col; ++j) {
1236 if (tab->col_var[j] >= 0 &&
1237 (tab->col_var[j] < tab->n_param ||
1238 tab->col_var[j] >= tab->n_var - tab->n_div))
1239 continue;
1241 if (!isl_int_is_neg(tr[j]))
1242 continue;
1244 if (tab->col_var[j] < 0 &&
1245 report_conflicting_constraint(tab, ~tab->col_var[j]) < 0)
1246 return -1;
1249 return 0;
1252 /* Resolve all known or obviously violated constraints through pivoting.
1253 * In particular, as long as we can find any violated constraint, we
1254 * look for a pivoting column that would result in the lexicographically
1255 * smallest increment in the sample point. If there is no such column
1256 * then the tableau is infeasible.
1258 static int restore_lexmin(struct isl_tab *tab) WARN_UNUSED;
1259 static int restore_lexmin(struct isl_tab *tab)
1261 int row, col;
1263 if (!tab)
1264 return -1;
1265 if (tab->empty)
1266 return 0;
1267 while ((row = first_neg(tab)) != -1) {
1268 col = lexmin_pivot_col(tab, row);
1269 if (col >= tab->n_col) {
1270 if (report_conflict(tab, row) < 0)
1271 return -1;
1272 if (isl_tab_mark_empty(tab) < 0)
1273 return -1;
1274 return 0;
1276 if (col < 0)
1277 return -1;
1278 if (isl_tab_pivot(tab, row, col) < 0)
1279 return -1;
1281 return 0;
1284 /* Given a row that represents an equality, look for an appropriate
1285 * pivoting column.
1286 * In particular, if there are any non-zero coefficients among
1287 * the non-parameter variables, then we take the last of these
1288 * variables. Eliminating this variable in terms of the other
1289 * variables and/or parameters does not influence the property
1290 * that all column in the initial tableau are lexicographically
1291 * positive. The row corresponding to the eliminated variable
1292 * will only have non-zero entries below the diagonal of the
1293 * initial tableau. That is, we transform
1295 * I I
1296 * 1 into a
1297 * I I
1299 * If there is no such non-parameter variable, then we are dealing with
1300 * pure parameter equality and we pick any parameter with coefficient 1 or -1
1301 * for elimination. This will ensure that the eliminated parameter
1302 * always has an integer value whenever all the other parameters are integral.
1303 * If there is no such parameter then we return -1.
1305 static int last_var_col_or_int_par_col(struct isl_tab *tab, int row)
1307 unsigned off = 2 + tab->M;
1308 int i;
1310 for (i = tab->n_var - tab->n_div - 1; i >= 0 && i >= tab->n_param; --i) {
1311 int col;
1312 if (tab->var[i].is_row)
1313 continue;
1314 col = tab->var[i].index;
1315 if (col <= tab->n_dead)
1316 continue;
1317 if (!isl_int_is_zero(tab->mat->row[row][off + col]))
1318 return col;
1320 for (i = tab->n_dead; i < tab->n_col; ++i) {
1321 if (isl_int_is_one(tab->mat->row[row][off + i]))
1322 return i;
1323 if (isl_int_is_negone(tab->mat->row[row][off + i]))
1324 return i;
1326 return -1;
1329 /* Add an equality that is known to be valid to the tableau.
1330 * We first check if we can eliminate a variable or a parameter.
1331 * If not, we add the equality as two inequalities.
1332 * In this case, the equality was a pure parameter equality and there
1333 * is no need to resolve any constraint violations.
1335 * This function assumes that at least two more rows and at least
1336 * two more elements in the constraint array are available in the tableau.
1338 static struct isl_tab *add_lexmin_valid_eq(struct isl_tab *tab, isl_int *eq)
1340 int i;
1341 int r;
1343 if (!tab)
1344 return NULL;
1345 r = isl_tab_add_row(tab, eq);
1346 if (r < 0)
1347 goto error;
1349 r = tab->con[r].index;
1350 i = last_var_col_or_int_par_col(tab, r);
1351 if (i < 0) {
1352 tab->con[r].is_nonneg = 1;
1353 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1354 goto error;
1355 isl_seq_neg(eq, eq, 1 + tab->n_var);
1356 r = isl_tab_add_row(tab, eq);
1357 if (r < 0)
1358 goto error;
1359 tab->con[r].is_nonneg = 1;
1360 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1361 goto error;
1362 } else {
1363 if (isl_tab_pivot(tab, r, i) < 0)
1364 goto error;
1365 if (isl_tab_kill_col(tab, i) < 0)
1366 goto error;
1367 tab->n_eq++;
1370 return tab;
1371 error:
1372 isl_tab_free(tab);
1373 return NULL;
1376 /* Check if the given row is a pure constant.
1378 static int is_constant(struct isl_tab *tab, int row)
1380 unsigned off = 2 + tab->M;
1382 return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1383 tab->n_col - tab->n_dead) == -1;
1386 /* Add an equality that may or may not be valid to the tableau.
1387 * If the resulting row is a pure constant, then it must be zero.
1388 * Otherwise, the resulting tableau is empty.
1390 * If the row is not a pure constant, then we add two inequalities,
1391 * each time checking that they can be satisfied.
1392 * In the end we try to use one of the two constraints to eliminate
1393 * a column.
1395 * This function assumes that at least two more rows and at least
1396 * two more elements in the constraint array are available in the tableau.
1398 static int add_lexmin_eq(struct isl_tab *tab, isl_int *eq) WARN_UNUSED;
1399 static int add_lexmin_eq(struct isl_tab *tab, isl_int *eq)
1401 int r1, r2;
1402 int row;
1403 struct isl_tab_undo *snap;
1405 if (!tab)
1406 return -1;
1407 snap = isl_tab_snap(tab);
1408 r1 = isl_tab_add_row(tab, eq);
1409 if (r1 < 0)
1410 return -1;
1411 tab->con[r1].is_nonneg = 1;
1412 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r1]) < 0)
1413 return -1;
1415 row = tab->con[r1].index;
1416 if (is_constant(tab, row)) {
1417 if (!isl_int_is_zero(tab->mat->row[row][1]) ||
1418 (tab->M && !isl_int_is_zero(tab->mat->row[row][2]))) {
1419 if (isl_tab_mark_empty(tab) < 0)
1420 return -1;
1421 return 0;
1423 if (isl_tab_rollback(tab, snap) < 0)
1424 return -1;
1425 return 0;
1428 if (restore_lexmin(tab) < 0)
1429 return -1;
1430 if (tab->empty)
1431 return 0;
1433 isl_seq_neg(eq, eq, 1 + tab->n_var);
1435 r2 = isl_tab_add_row(tab, eq);
1436 if (r2 < 0)
1437 return -1;
1438 tab->con[r2].is_nonneg = 1;
1439 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r2]) < 0)
1440 return -1;
1442 if (restore_lexmin(tab) < 0)
1443 return -1;
1444 if (tab->empty)
1445 return 0;
1447 if (!tab->con[r1].is_row) {
1448 if (isl_tab_kill_col(tab, tab->con[r1].index) < 0)
1449 return -1;
1450 } else if (!tab->con[r2].is_row) {
1451 if (isl_tab_kill_col(tab, tab->con[r2].index) < 0)
1452 return -1;
1455 if (tab->bmap) {
1456 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1457 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1458 return -1;
1459 isl_seq_neg(eq, eq, 1 + tab->n_var);
1460 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1461 isl_seq_neg(eq, eq, 1 + tab->n_var);
1462 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1463 return -1;
1464 if (!tab->bmap)
1465 return -1;
1468 return 0;
1471 /* Add an inequality to the tableau, resolving violations using
1472 * restore_lexmin.
1474 * This function assumes that at least one more row and at least
1475 * one more element in the constraint array are available in the tableau.
1477 static struct isl_tab *add_lexmin_ineq(struct isl_tab *tab, isl_int *ineq)
1479 int r;
1481 if (!tab)
1482 return NULL;
1483 if (tab->bmap) {
1484 tab->bmap = isl_basic_map_add_ineq(tab->bmap, ineq);
1485 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1486 goto error;
1487 if (!tab->bmap)
1488 goto error;
1490 r = isl_tab_add_row(tab, ineq);
1491 if (r < 0)
1492 goto error;
1493 tab->con[r].is_nonneg = 1;
1494 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1495 goto error;
1496 if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1497 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1498 goto error;
1499 return tab;
1502 if (restore_lexmin(tab) < 0)
1503 goto error;
1504 if (!tab->empty && tab->con[r].is_row &&
1505 isl_tab_row_is_redundant(tab, tab->con[r].index))
1506 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1507 goto error;
1508 return tab;
1509 error:
1510 isl_tab_free(tab);
1511 return NULL;
1514 /* Check if the coefficients of the parameters are all integral.
1516 static int integer_parameter(struct isl_tab *tab, int row)
1518 int i;
1519 int col;
1520 unsigned off = 2 + tab->M;
1522 for (i = 0; i < tab->n_param; ++i) {
1523 /* Eliminated parameter */
1524 if (tab->var[i].is_row)
1525 continue;
1526 col = tab->var[i].index;
1527 if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
1528 tab->mat->row[row][0]))
1529 return 0;
1531 for (i = 0; i < tab->n_div; ++i) {
1532 if (tab->var[tab->n_var - tab->n_div + i].is_row)
1533 continue;
1534 col = tab->var[tab->n_var - tab->n_div + i].index;
1535 if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
1536 tab->mat->row[row][0]))
1537 return 0;
1539 return 1;
1542 /* Check if the coefficients of the non-parameter variables are all integral.
1544 static int integer_variable(struct isl_tab *tab, int row)
1546 int i;
1547 unsigned off = 2 + tab->M;
1549 for (i = tab->n_dead; i < tab->n_col; ++i) {
1550 if (tab->col_var[i] >= 0 &&
1551 (tab->col_var[i] < tab->n_param ||
1552 tab->col_var[i] >= tab->n_var - tab->n_div))
1553 continue;
1554 if (!isl_int_is_divisible_by(tab->mat->row[row][off + i],
1555 tab->mat->row[row][0]))
1556 return 0;
1558 return 1;
1561 /* Check if the constant term is integral.
1563 static int integer_constant(struct isl_tab *tab, int row)
1565 return isl_int_is_divisible_by(tab->mat->row[row][1],
1566 tab->mat->row[row][0]);
1569 #define I_CST 1 << 0
1570 #define I_PAR 1 << 1
1571 #define I_VAR 1 << 2
1573 /* Check for next (non-parameter) variable after "var" (first if var == -1)
1574 * that is non-integer and therefore requires a cut and return
1575 * the index of the variable.
1576 * For parametric tableaus, there are three parts in a row,
1577 * the constant, the coefficients of the parameters and the rest.
1578 * For each part, we check whether the coefficients in that part
1579 * are all integral and if so, set the corresponding flag in *f.
1580 * If the constant and the parameter part are integral, then the
1581 * current sample value is integral and no cut is required
1582 * (irrespective of whether the variable part is integral).
1584 static int next_non_integer_var(struct isl_tab *tab, int var, int *f)
1586 var = var < 0 ? tab->n_param : var + 1;
1588 for (; var < tab->n_var - tab->n_div; ++var) {
1589 int flags = 0;
1590 int row;
1591 if (!tab->var[var].is_row)
1592 continue;
1593 row = tab->var[var].index;
1594 if (integer_constant(tab, row))
1595 ISL_FL_SET(flags, I_CST);
1596 if (integer_parameter(tab, row))
1597 ISL_FL_SET(flags, I_PAR);
1598 if (ISL_FL_ISSET(flags, I_CST) && ISL_FL_ISSET(flags, I_PAR))
1599 continue;
1600 if (integer_variable(tab, row))
1601 ISL_FL_SET(flags, I_VAR);
1602 *f = flags;
1603 return var;
1605 return -1;
1608 /* Check for first (non-parameter) variable that is non-integer and
1609 * therefore requires a cut and return the corresponding row.
1610 * For parametric tableaus, there are three parts in a row,
1611 * the constant, the coefficients of the parameters and the rest.
1612 * For each part, we check whether the coefficients in that part
1613 * are all integral and if so, set the corresponding flag in *f.
1614 * If the constant and the parameter part are integral, then the
1615 * current sample value is integral and no cut is required
1616 * (irrespective of whether the variable part is integral).
1618 static int first_non_integer_row(struct isl_tab *tab, int *f)
1620 int var = next_non_integer_var(tab, -1, f);
1622 return var < 0 ? -1 : tab->var[var].index;
1625 /* Add a (non-parametric) cut to cut away the non-integral sample
1626 * value of the given row.
1628 * If the row is given by
1630 * m r = f + \sum_i a_i y_i
1632 * then the cut is
1634 * c = - {-f/m} + \sum_i {a_i/m} y_i >= 0
1636 * The big parameter, if any, is ignored, since it is assumed to be big
1637 * enough to be divisible by any integer.
1638 * If the tableau is actually a parametric tableau, then this function
1639 * is only called when all coefficients of the parameters are integral.
1640 * The cut therefore has zero coefficients for the parameters.
1642 * The current value is known to be negative, so row_sign, if it
1643 * exists, is set accordingly.
1645 * Return the row of the cut or -1.
1647 static int add_cut(struct isl_tab *tab, int row)
1649 int i;
1650 int r;
1651 isl_int *r_row;
1652 unsigned off = 2 + tab->M;
1654 if (isl_tab_extend_cons(tab, 1) < 0)
1655 return -1;
1656 r = isl_tab_allocate_con(tab);
1657 if (r < 0)
1658 return -1;
1660 r_row = tab->mat->row[tab->con[r].index];
1661 isl_int_set(r_row[0], tab->mat->row[row][0]);
1662 isl_int_neg(r_row[1], tab->mat->row[row][1]);
1663 isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
1664 isl_int_neg(r_row[1], r_row[1]);
1665 if (tab->M)
1666 isl_int_set_si(r_row[2], 0);
1667 for (i = 0; i < tab->n_col; ++i)
1668 isl_int_fdiv_r(r_row[off + i],
1669 tab->mat->row[row][off + i], tab->mat->row[row][0]);
1671 tab->con[r].is_nonneg = 1;
1672 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1673 return -1;
1674 if (tab->row_sign)
1675 tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
1677 return tab->con[r].index;
1680 #define CUT_ALL 1
1681 #define CUT_ONE 0
1683 /* Given a non-parametric tableau, add cuts until an integer
1684 * sample point is obtained or until the tableau is determined
1685 * to be integer infeasible.
1686 * As long as there is any non-integer value in the sample point,
1687 * we add appropriate cuts, if possible, for each of these
1688 * non-integer values and then resolve the violated
1689 * cut constraints using restore_lexmin.
1690 * If one of the corresponding rows is equal to an integral
1691 * combination of variables/constraints plus a non-integral constant,
1692 * then there is no way to obtain an integer point and we return
1693 * a tableau that is marked empty.
1694 * The parameter cutting_strategy controls the strategy used when adding cuts
1695 * to remove non-integer points. CUT_ALL adds all possible cuts
1696 * before continuing the search. CUT_ONE adds only one cut at a time.
1698 static struct isl_tab *cut_to_integer_lexmin(struct isl_tab *tab,
1699 int cutting_strategy)
1701 int var;
1702 int row;
1703 int flags;
1705 if (!tab)
1706 return NULL;
1707 if (tab->empty)
1708 return tab;
1710 while ((var = next_non_integer_var(tab, -1, &flags)) != -1) {
1711 do {
1712 if (ISL_FL_ISSET(flags, I_VAR)) {
1713 if (isl_tab_mark_empty(tab) < 0)
1714 goto error;
1715 return tab;
1717 row = tab->var[var].index;
1718 row = add_cut(tab, row);
1719 if (row < 0)
1720 goto error;
1721 if (cutting_strategy == CUT_ONE)
1722 break;
1723 } while ((var = next_non_integer_var(tab, var, &flags)) != -1);
1724 if (restore_lexmin(tab) < 0)
1725 goto error;
1726 if (tab->empty)
1727 break;
1729 return tab;
1730 error:
1731 isl_tab_free(tab);
1732 return NULL;
1735 /* Check whether all the currently active samples also satisfy the inequality
1736 * "ineq" (treated as an equality if eq is set).
1737 * Remove those samples that do not.
1739 static struct isl_tab *check_samples(struct isl_tab *tab, isl_int *ineq, int eq)
1741 int i;
1742 isl_int v;
1744 if (!tab)
1745 return NULL;
1747 isl_assert(tab->mat->ctx, tab->bmap, goto error);
1748 isl_assert(tab->mat->ctx, tab->samples, goto error);
1749 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, goto error);
1751 isl_int_init(v);
1752 for (i = tab->n_outside; i < tab->n_sample; ++i) {
1753 int sgn;
1754 isl_seq_inner_product(ineq, tab->samples->row[i],
1755 1 + tab->n_var, &v);
1756 sgn = isl_int_sgn(v);
1757 if (eq ? (sgn == 0) : (sgn >= 0))
1758 continue;
1759 tab = isl_tab_drop_sample(tab, i);
1760 if (!tab)
1761 break;
1763 isl_int_clear(v);
1765 return tab;
1766 error:
1767 isl_tab_free(tab);
1768 return NULL;
1771 /* Check whether the sample value of the tableau is finite,
1772 * i.e., either the tableau does not use a big parameter, or
1773 * all values of the variables are equal to the big parameter plus
1774 * some constant. This constant is the actual sample value.
1776 static int sample_is_finite(struct isl_tab *tab)
1778 int i;
1780 if (!tab->M)
1781 return 1;
1783 for (i = 0; i < tab->n_var; ++i) {
1784 int row;
1785 if (!tab->var[i].is_row)
1786 return 0;
1787 row = tab->var[i].index;
1788 if (isl_int_ne(tab->mat->row[row][0], tab->mat->row[row][2]))
1789 return 0;
1791 return 1;
1794 /* Check if the context tableau of sol has any integer points.
1795 * Leave tab in empty state if no integer point can be found.
1796 * If an integer point can be found and if moreover it is finite,
1797 * then it is added to the list of sample values.
1799 * This function is only called when none of the currently active sample
1800 * values satisfies the most recently added constraint.
1802 static struct isl_tab *check_integer_feasible(struct isl_tab *tab)
1804 struct isl_tab_undo *snap;
1806 if (!tab)
1807 return NULL;
1809 snap = isl_tab_snap(tab);
1810 if (isl_tab_push_basis(tab) < 0)
1811 goto error;
1813 tab = cut_to_integer_lexmin(tab, CUT_ALL);
1814 if (!tab)
1815 goto error;
1817 if (!tab->empty && sample_is_finite(tab)) {
1818 struct isl_vec *sample;
1820 sample = isl_tab_get_sample_value(tab);
1822 if (isl_tab_add_sample(tab, sample) < 0)
1823 goto error;
1826 if (!tab->empty && isl_tab_rollback(tab, snap) < 0)
1827 goto error;
1829 return tab;
1830 error:
1831 isl_tab_free(tab);
1832 return NULL;
1835 /* Check if any of the currently active sample values satisfies
1836 * the inequality "ineq" (an equality if eq is set).
1838 static int tab_has_valid_sample(struct isl_tab *tab, isl_int *ineq, int eq)
1840 int i;
1841 isl_int v;
1843 if (!tab)
1844 return -1;
1846 isl_assert(tab->mat->ctx, tab->bmap, return -1);
1847 isl_assert(tab->mat->ctx, tab->samples, return -1);
1848 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, return -1);
1850 isl_int_init(v);
1851 for (i = tab->n_outside; i < tab->n_sample; ++i) {
1852 int sgn;
1853 isl_seq_inner_product(ineq, tab->samples->row[i],
1854 1 + tab->n_var, &v);
1855 sgn = isl_int_sgn(v);
1856 if (eq ? (sgn == 0) : (sgn >= 0))
1857 break;
1859 isl_int_clear(v);
1861 return i < tab->n_sample;
1864 /* Add a div specified by "div" to the tableau "tab" and return
1865 * isl_bool_true if the div is obviously non-negative.
1867 static isl_bool context_tab_add_div(struct isl_tab *tab,
1868 __isl_keep isl_vec *div,
1869 int (*add_ineq)(void *user, isl_int *), void *user)
1871 int i;
1872 int r;
1873 struct isl_mat *samples;
1874 int nonneg;
1876 r = isl_tab_add_div(tab, div, add_ineq, user);
1877 if (r < 0)
1878 return isl_bool_error;
1879 nonneg = tab->var[r].is_nonneg;
1880 tab->var[r].frozen = 1;
1882 samples = isl_mat_extend(tab->samples,
1883 tab->n_sample, 1 + tab->n_var);
1884 tab->samples = samples;
1885 if (!samples)
1886 return isl_bool_error;
1887 for (i = tab->n_outside; i < samples->n_row; ++i) {
1888 isl_seq_inner_product(div->el + 1, samples->row[i],
1889 div->size - 1, &samples->row[i][samples->n_col - 1]);
1890 isl_int_fdiv_q(samples->row[i][samples->n_col - 1],
1891 samples->row[i][samples->n_col - 1], div->el[0]);
1894 return nonneg;
1897 /* Add a div specified by "div" to both the main tableau and
1898 * the context tableau. In case of the main tableau, we only
1899 * need to add an extra div. In the context tableau, we also
1900 * need to express the meaning of the div.
1901 * Return the index of the div or -1 if anything went wrong.
1903 static int add_div(struct isl_tab *tab, struct isl_context *context,
1904 __isl_keep isl_vec *div)
1906 int r;
1907 isl_bool nonneg;
1909 if ((nonneg = context->op->add_div(context, div)) < 0)
1910 goto error;
1912 if (!context->op->is_ok(context))
1913 goto error;
1915 if (isl_tab_extend_vars(tab, 1) < 0)
1916 goto error;
1917 r = isl_tab_allocate_var(tab);
1918 if (r < 0)
1919 goto error;
1920 if (nonneg)
1921 tab->var[r].is_nonneg = 1;
1922 tab->var[r].frozen = 1;
1923 tab->n_div++;
1925 return tab->n_div - 1;
1926 error:
1927 context->op->invalidate(context);
1928 return -1;
1931 static int find_div(struct isl_tab *tab, isl_int *div, isl_int denom)
1933 int i;
1934 unsigned total = isl_basic_map_total_dim(tab->bmap);
1936 for (i = 0; i < tab->bmap->n_div; ++i) {
1937 if (isl_int_ne(tab->bmap->div[i][0], denom))
1938 continue;
1939 if (!isl_seq_eq(tab->bmap->div[i] + 1, div, 1 + total))
1940 continue;
1941 return i;
1943 return -1;
1946 /* Return the index of a div that corresponds to "div".
1947 * We first check if we already have such a div and if not, we create one.
1949 static int get_div(struct isl_tab *tab, struct isl_context *context,
1950 struct isl_vec *div)
1952 int d;
1953 struct isl_tab *context_tab = context->op->peek_tab(context);
1955 if (!context_tab)
1956 return -1;
1958 d = find_div(context_tab, div->el + 1, div->el[0]);
1959 if (d != -1)
1960 return d;
1962 return add_div(tab, context, div);
1965 /* Add a parametric cut to cut away the non-integral sample value
1966 * of the give row.
1967 * Let a_i be the coefficients of the constant term and the parameters
1968 * and let b_i be the coefficients of the variables or constraints
1969 * in basis of the tableau.
1970 * Let q be the div q = floor(\sum_i {-a_i} y_i).
1972 * The cut is expressed as
1974 * c = \sum_i -{-a_i} y_i + \sum_i {b_i} x_i + q >= 0
1976 * If q did not already exist in the context tableau, then it is added first.
1977 * If q is in a column of the main tableau then the "+ q" can be accomplished
1978 * by setting the corresponding entry to the denominator of the constraint.
1979 * If q happens to be in a row of the main tableau, then the corresponding
1980 * row needs to be added instead (taking care of the denominators).
1981 * Note that this is very unlikely, but perhaps not entirely impossible.
1983 * The current value of the cut is known to be negative (or at least
1984 * non-positive), so row_sign is set accordingly.
1986 * Return the row of the cut or -1.
1988 static int add_parametric_cut(struct isl_tab *tab, int row,
1989 struct isl_context *context)
1991 struct isl_vec *div;
1992 int d;
1993 int i;
1994 int r;
1995 isl_int *r_row;
1996 int col;
1997 int n;
1998 unsigned off = 2 + tab->M;
2000 if (!context)
2001 return -1;
2003 div = get_row_parameter_div(tab, row);
2004 if (!div)
2005 return -1;
2007 n = tab->n_div;
2008 d = context->op->get_div(context, tab, div);
2009 isl_vec_free(div);
2010 if (d < 0)
2011 return -1;
2013 if (isl_tab_extend_cons(tab, 1) < 0)
2014 return -1;
2015 r = isl_tab_allocate_con(tab);
2016 if (r < 0)
2017 return -1;
2019 r_row = tab->mat->row[tab->con[r].index];
2020 isl_int_set(r_row[0], tab->mat->row[row][0]);
2021 isl_int_neg(r_row[1], tab->mat->row[row][1]);
2022 isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
2023 isl_int_neg(r_row[1], r_row[1]);
2024 if (tab->M)
2025 isl_int_set_si(r_row[2], 0);
2026 for (i = 0; i < tab->n_param; ++i) {
2027 if (tab->var[i].is_row)
2028 continue;
2029 col = tab->var[i].index;
2030 isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
2031 isl_int_fdiv_r(r_row[off + col], r_row[off + col],
2032 tab->mat->row[row][0]);
2033 isl_int_neg(r_row[off + col], r_row[off + col]);
2035 for (i = 0; i < tab->n_div; ++i) {
2036 if (tab->var[tab->n_var - tab->n_div + i].is_row)
2037 continue;
2038 col = tab->var[tab->n_var - tab->n_div + i].index;
2039 isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
2040 isl_int_fdiv_r(r_row[off + col], r_row[off + col],
2041 tab->mat->row[row][0]);
2042 isl_int_neg(r_row[off + col], r_row[off + col]);
2044 for (i = 0; i < tab->n_col; ++i) {
2045 if (tab->col_var[i] >= 0 &&
2046 (tab->col_var[i] < tab->n_param ||
2047 tab->col_var[i] >= tab->n_var - tab->n_div))
2048 continue;
2049 isl_int_fdiv_r(r_row[off + i],
2050 tab->mat->row[row][off + i], tab->mat->row[row][0]);
2052 if (tab->var[tab->n_var - tab->n_div + d].is_row) {
2053 isl_int gcd;
2054 int d_row = tab->var[tab->n_var - tab->n_div + d].index;
2055 isl_int_init(gcd);
2056 isl_int_gcd(gcd, tab->mat->row[d_row][0], r_row[0]);
2057 isl_int_divexact(r_row[0], r_row[0], gcd);
2058 isl_int_divexact(gcd, tab->mat->row[d_row][0], gcd);
2059 isl_seq_combine(r_row + 1, gcd, r_row + 1,
2060 r_row[0], tab->mat->row[d_row] + 1,
2061 off - 1 + tab->n_col);
2062 isl_int_mul(r_row[0], r_row[0], tab->mat->row[d_row][0]);
2063 isl_int_clear(gcd);
2064 } else {
2065 col = tab->var[tab->n_var - tab->n_div + d].index;
2066 isl_int_set(r_row[off + col], tab->mat->row[row][0]);
2069 tab->con[r].is_nonneg = 1;
2070 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
2071 return -1;
2072 if (tab->row_sign)
2073 tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
2075 row = tab->con[r].index;
2077 if (d >= n && context->op->detect_equalities(context, tab) < 0)
2078 return -1;
2080 return row;
2083 /* Construct a tableau for bmap that can be used for computing
2084 * the lexicographic minimum (or maximum) of bmap.
2085 * If not NULL, then dom is the domain where the minimum
2086 * should be computed. In this case, we set up a parametric
2087 * tableau with row signs (initialized to "unknown").
2088 * If M is set, then the tableau will use a big parameter.
2089 * If max is set, then a maximum should be computed instead of a minimum.
2090 * This means that for each variable x, the tableau will contain the variable
2091 * x' = M - x, rather than x' = M + x. This in turn means that the coefficient
2092 * of the variables in all constraints are negated prior to adding them
2093 * to the tableau.
2095 static struct isl_tab *tab_for_lexmin(struct isl_basic_map *bmap,
2096 struct isl_basic_set *dom, unsigned M, int max)
2098 int i;
2099 struct isl_tab *tab;
2100 unsigned n_var;
2101 unsigned o_var;
2103 tab = isl_tab_alloc(bmap->ctx, 2 * bmap->n_eq + bmap->n_ineq + 1,
2104 isl_basic_map_total_dim(bmap), M);
2105 if (!tab)
2106 return NULL;
2108 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
2109 if (dom) {
2110 tab->n_param = isl_basic_set_total_dim(dom) - dom->n_div;
2111 tab->n_div = dom->n_div;
2112 tab->row_sign = isl_calloc_array(bmap->ctx,
2113 enum isl_tab_row_sign, tab->mat->n_row);
2114 if (tab->mat->n_row && !tab->row_sign)
2115 goto error;
2117 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
2118 if (isl_tab_mark_empty(tab) < 0)
2119 goto error;
2120 return tab;
2123 for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
2124 tab->var[i].is_nonneg = 1;
2125 tab->var[i].frozen = 1;
2127 o_var = 1 + tab->n_param;
2128 n_var = tab->n_var - tab->n_param - tab->n_div;
2129 for (i = 0; i < bmap->n_eq; ++i) {
2130 if (max)
2131 isl_seq_neg(bmap->eq[i] + o_var,
2132 bmap->eq[i] + o_var, n_var);
2133 tab = add_lexmin_valid_eq(tab, bmap->eq[i]);
2134 if (max)
2135 isl_seq_neg(bmap->eq[i] + o_var,
2136 bmap->eq[i] + o_var, n_var);
2137 if (!tab || tab->empty)
2138 return tab;
2140 if (bmap->n_eq && restore_lexmin(tab) < 0)
2141 goto error;
2142 for (i = 0; i < bmap->n_ineq; ++i) {
2143 if (max)
2144 isl_seq_neg(bmap->ineq[i] + o_var,
2145 bmap->ineq[i] + o_var, n_var);
2146 tab = add_lexmin_ineq(tab, bmap->ineq[i]);
2147 if (max)
2148 isl_seq_neg(bmap->ineq[i] + o_var,
2149 bmap->ineq[i] + o_var, n_var);
2150 if (!tab || tab->empty)
2151 return tab;
2153 return tab;
2154 error:
2155 isl_tab_free(tab);
2156 return NULL;
2159 /* Given a main tableau where more than one row requires a split,
2160 * determine and return the "best" row to split on.
2162 * Given two rows in the main tableau, if the inequality corresponding
2163 * to the first row is redundant with respect to that of the second row
2164 * in the current tableau, then it is better to split on the second row,
2165 * since in the positive part, both rows will be positive.
2166 * (In the negative part a pivot will have to be performed and just about
2167 * anything can happen to the sign of the other row.)
2169 * As a simple heuristic, we therefore select the row that makes the most
2170 * of the other rows redundant.
2172 * Perhaps it would also be useful to look at the number of constraints
2173 * that conflict with any given constraint.
2175 * best is the best row so far (-1 when we have not found any row yet).
2176 * best_r is the number of other rows made redundant by row best.
2177 * When best is still -1, bset_r is meaningless, but it is initialized
2178 * to some arbitrary value (0) anyway. Without this redundant initialization
2179 * valgrind may warn about uninitialized memory accesses when isl
2180 * is compiled with some versions of gcc.
2182 static int best_split(struct isl_tab *tab, struct isl_tab *context_tab)
2184 struct isl_tab_undo *snap;
2185 int split;
2186 int row;
2187 int best = -1;
2188 int best_r = 0;
2190 if (isl_tab_extend_cons(context_tab, 2) < 0)
2191 return -1;
2193 snap = isl_tab_snap(context_tab);
2195 for (split = tab->n_redundant; split < tab->n_row; ++split) {
2196 struct isl_tab_undo *snap2;
2197 struct isl_vec *ineq = NULL;
2198 int r = 0;
2199 int ok;
2201 if (!isl_tab_var_from_row(tab, split)->is_nonneg)
2202 continue;
2203 if (tab->row_sign[split] != isl_tab_row_any)
2204 continue;
2206 ineq = get_row_parameter_ineq(tab, split);
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;
2214 snap2 = isl_tab_snap(context_tab);
2216 for (row = tab->n_redundant; row < tab->n_row; ++row) {
2217 struct isl_tab_var *var;
2219 if (row == split)
2220 continue;
2221 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
2222 continue;
2223 if (tab->row_sign[row] != isl_tab_row_any)
2224 continue;
2226 ineq = get_row_parameter_ineq(tab, row);
2227 if (!ineq)
2228 return -1;
2229 ok = isl_tab_add_ineq(context_tab, ineq->el) >= 0;
2230 isl_vec_free(ineq);
2231 if (!ok)
2232 return -1;
2233 var = &context_tab->con[context_tab->n_con - 1];
2234 if (!context_tab->empty &&
2235 !isl_tab_min_at_most_neg_one(context_tab, var))
2236 r++;
2237 if (isl_tab_rollback(context_tab, snap2) < 0)
2238 return -1;
2240 if (best == -1 || r > best_r) {
2241 best = split;
2242 best_r = r;
2244 if (isl_tab_rollback(context_tab, snap) < 0)
2245 return -1;
2248 return best;
2251 static struct isl_basic_set *context_lex_peek_basic_set(
2252 struct isl_context *context)
2254 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2255 if (!clex->tab)
2256 return NULL;
2257 return isl_tab_peek_bset(clex->tab);
2260 static struct isl_tab *context_lex_peek_tab(struct isl_context *context)
2262 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2263 return clex->tab;
2266 static void context_lex_add_eq(struct isl_context *context, isl_int *eq,
2267 int check, int update)
2269 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2270 if (isl_tab_extend_cons(clex->tab, 2) < 0)
2271 goto error;
2272 if (add_lexmin_eq(clex->tab, eq) < 0)
2273 goto error;
2274 if (check) {
2275 int v = tab_has_valid_sample(clex->tab, eq, 1);
2276 if (v < 0)
2277 goto error;
2278 if (!v)
2279 clex->tab = check_integer_feasible(clex->tab);
2281 if (update)
2282 clex->tab = check_samples(clex->tab, eq, 1);
2283 return;
2284 error:
2285 isl_tab_free(clex->tab);
2286 clex->tab = NULL;
2289 static void context_lex_add_ineq(struct isl_context *context, isl_int *ineq,
2290 int check, int update)
2292 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2293 if (isl_tab_extend_cons(clex->tab, 1) < 0)
2294 goto error;
2295 clex->tab = add_lexmin_ineq(clex->tab, ineq);
2296 if (check) {
2297 int v = tab_has_valid_sample(clex->tab, ineq, 0);
2298 if (v < 0)
2299 goto error;
2300 if (!v)
2301 clex->tab = check_integer_feasible(clex->tab);
2303 if (update)
2304 clex->tab = check_samples(clex->tab, ineq, 0);
2305 return;
2306 error:
2307 isl_tab_free(clex->tab);
2308 clex->tab = NULL;
2311 static int context_lex_add_ineq_wrap(void *user, isl_int *ineq)
2313 struct isl_context *context = (struct isl_context *)user;
2314 context_lex_add_ineq(context, ineq, 0, 0);
2315 return context->op->is_ok(context) ? 0 : -1;
2318 /* Check which signs can be obtained by "ineq" on all the currently
2319 * active sample values. See row_sign for more information.
2321 static enum isl_tab_row_sign tab_ineq_sign(struct isl_tab *tab, isl_int *ineq,
2322 int strict)
2324 int i;
2325 int sgn;
2326 isl_int tmp;
2327 enum isl_tab_row_sign res = isl_tab_row_unknown;
2329 isl_assert(tab->mat->ctx, tab->samples, return isl_tab_row_unknown);
2330 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var,
2331 return isl_tab_row_unknown);
2333 isl_int_init(tmp);
2334 for (i = tab->n_outside; i < tab->n_sample; ++i) {
2335 isl_seq_inner_product(tab->samples->row[i], ineq,
2336 1 + tab->n_var, &tmp);
2337 sgn = isl_int_sgn(tmp);
2338 if (sgn > 0 || (sgn == 0 && strict)) {
2339 if (res == isl_tab_row_unknown)
2340 res = isl_tab_row_pos;
2341 if (res == isl_tab_row_neg)
2342 res = isl_tab_row_any;
2344 if (sgn < 0) {
2345 if (res == isl_tab_row_unknown)
2346 res = isl_tab_row_neg;
2347 if (res == isl_tab_row_pos)
2348 res = isl_tab_row_any;
2350 if (res == isl_tab_row_any)
2351 break;
2353 isl_int_clear(tmp);
2355 return res;
2358 static enum isl_tab_row_sign context_lex_ineq_sign(struct isl_context *context,
2359 isl_int *ineq, int strict)
2361 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2362 return tab_ineq_sign(clex->tab, ineq, strict);
2365 /* Check whether "ineq" can be added to the tableau without rendering
2366 * it infeasible.
2368 static int context_lex_test_ineq(struct isl_context *context, isl_int *ineq)
2370 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2371 struct isl_tab_undo *snap;
2372 int feasible;
2374 if (!clex->tab)
2375 return -1;
2377 if (isl_tab_extend_cons(clex->tab, 1) < 0)
2378 return -1;
2380 snap = isl_tab_snap(clex->tab);
2381 if (isl_tab_push_basis(clex->tab) < 0)
2382 return -1;
2383 clex->tab = add_lexmin_ineq(clex->tab, ineq);
2384 clex->tab = check_integer_feasible(clex->tab);
2385 if (!clex->tab)
2386 return -1;
2387 feasible = !clex->tab->empty;
2388 if (isl_tab_rollback(clex->tab, snap) < 0)
2389 return -1;
2391 return feasible;
2394 static int context_lex_get_div(struct isl_context *context, struct isl_tab *tab,
2395 struct isl_vec *div)
2397 return get_div(tab, context, div);
2400 /* Add a div specified by "div" to the context tableau and return
2401 * isl_bool_true if the div is obviously non-negative.
2402 * context_tab_add_div will always return isl_bool_true, because all variables
2403 * in a isl_context_lex tableau are non-negative.
2404 * However, if we are using a big parameter in the context, then this only
2405 * reflects the non-negativity of the variable used to _encode_ the
2406 * div, i.e., div' = M + div, so we can't draw any conclusions.
2408 static isl_bool context_lex_add_div(struct isl_context *context,
2409 __isl_keep isl_vec *div)
2411 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2412 isl_bool nonneg;
2413 nonneg = context_tab_add_div(clex->tab, div,
2414 context_lex_add_ineq_wrap, context);
2415 if (nonneg < 0)
2416 return isl_bool_error;
2417 if (clex->tab->M)
2418 return isl_bool_false;
2419 return nonneg;
2422 static int context_lex_detect_equalities(struct isl_context *context,
2423 struct isl_tab *tab)
2425 return 0;
2428 static int context_lex_best_split(struct isl_context *context,
2429 struct isl_tab *tab)
2431 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2432 struct isl_tab_undo *snap;
2433 int r;
2435 snap = isl_tab_snap(clex->tab);
2436 if (isl_tab_push_basis(clex->tab) < 0)
2437 return -1;
2438 r = best_split(tab, clex->tab);
2440 if (r >= 0 && isl_tab_rollback(clex->tab, snap) < 0)
2441 return -1;
2443 return r;
2446 static int context_lex_is_empty(struct isl_context *context)
2448 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2449 if (!clex->tab)
2450 return -1;
2451 return clex->tab->empty;
2454 static void *context_lex_save(struct isl_context *context)
2456 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2457 struct isl_tab_undo *snap;
2459 snap = isl_tab_snap(clex->tab);
2460 if (isl_tab_push_basis(clex->tab) < 0)
2461 return NULL;
2462 if (isl_tab_save_samples(clex->tab) < 0)
2463 return NULL;
2465 return snap;
2468 static void context_lex_restore(struct isl_context *context, void *save)
2470 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2471 if (isl_tab_rollback(clex->tab, (struct isl_tab_undo *)save) < 0) {
2472 isl_tab_free(clex->tab);
2473 clex->tab = NULL;
2477 static void context_lex_discard(void *save)
2481 static int context_lex_is_ok(struct isl_context *context)
2483 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2484 return !!clex->tab;
2487 /* For each variable in the context tableau, check if the variable can
2488 * only attain non-negative values. If so, mark the parameter as non-negative
2489 * in the main tableau. This allows for a more direct identification of some
2490 * cases of violated constraints.
2492 static struct isl_tab *tab_detect_nonnegative_parameters(struct isl_tab *tab,
2493 struct isl_tab *context_tab)
2495 int i;
2496 struct isl_tab_undo *snap;
2497 struct isl_vec *ineq = NULL;
2498 struct isl_tab_var *var;
2499 int n;
2501 if (context_tab->n_var == 0)
2502 return tab;
2504 ineq = isl_vec_alloc(tab->mat->ctx, 1 + context_tab->n_var);
2505 if (!ineq)
2506 goto error;
2508 if (isl_tab_extend_cons(context_tab, 1) < 0)
2509 goto error;
2511 snap = isl_tab_snap(context_tab);
2513 n = 0;
2514 isl_seq_clr(ineq->el, ineq->size);
2515 for (i = 0; i < context_tab->n_var; ++i) {
2516 isl_int_set_si(ineq->el[1 + i], 1);
2517 if (isl_tab_add_ineq(context_tab, ineq->el) < 0)
2518 goto error;
2519 var = &context_tab->con[context_tab->n_con - 1];
2520 if (!context_tab->empty &&
2521 !isl_tab_min_at_most_neg_one(context_tab, var)) {
2522 int j = i;
2523 if (i >= tab->n_param)
2524 j = i - tab->n_param + tab->n_var - tab->n_div;
2525 tab->var[j].is_nonneg = 1;
2526 n++;
2528 isl_int_set_si(ineq->el[1 + i], 0);
2529 if (isl_tab_rollback(context_tab, snap) < 0)
2530 goto error;
2533 if (context_tab->M && n == context_tab->n_var) {
2534 context_tab->mat = isl_mat_drop_cols(context_tab->mat, 2, 1);
2535 context_tab->M = 0;
2538 isl_vec_free(ineq);
2539 return tab;
2540 error:
2541 isl_vec_free(ineq);
2542 isl_tab_free(tab);
2543 return NULL;
2546 static struct isl_tab *context_lex_detect_nonnegative_parameters(
2547 struct isl_context *context, struct isl_tab *tab)
2549 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2550 struct isl_tab_undo *snap;
2552 if (!tab)
2553 return NULL;
2555 snap = isl_tab_snap(clex->tab);
2556 if (isl_tab_push_basis(clex->tab) < 0)
2557 goto error;
2559 tab = tab_detect_nonnegative_parameters(tab, clex->tab);
2561 if (isl_tab_rollback(clex->tab, snap) < 0)
2562 goto error;
2564 return tab;
2565 error:
2566 isl_tab_free(tab);
2567 return NULL;
2570 static void context_lex_invalidate(struct isl_context *context)
2572 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2573 isl_tab_free(clex->tab);
2574 clex->tab = NULL;
2577 static void context_lex_free(struct isl_context *context)
2579 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2580 isl_tab_free(clex->tab);
2581 free(clex);
2584 struct isl_context_op isl_context_lex_op = {
2585 context_lex_detect_nonnegative_parameters,
2586 context_lex_peek_basic_set,
2587 context_lex_peek_tab,
2588 context_lex_add_eq,
2589 context_lex_add_ineq,
2590 context_lex_ineq_sign,
2591 context_lex_test_ineq,
2592 context_lex_get_div,
2593 context_lex_add_div,
2594 context_lex_detect_equalities,
2595 context_lex_best_split,
2596 context_lex_is_empty,
2597 context_lex_is_ok,
2598 context_lex_save,
2599 context_lex_restore,
2600 context_lex_discard,
2601 context_lex_invalidate,
2602 context_lex_free,
2605 static struct isl_tab *context_tab_for_lexmin(struct isl_basic_set *bset)
2607 struct isl_tab *tab;
2609 if (!bset)
2610 return NULL;
2611 tab = tab_for_lexmin((struct isl_basic_map *)bset, NULL, 1, 0);
2612 if (!tab)
2613 goto error;
2614 if (isl_tab_track_bset(tab, bset) < 0)
2615 goto error;
2616 tab = isl_tab_init_samples(tab);
2617 return tab;
2618 error:
2619 isl_basic_set_free(bset);
2620 return NULL;
2623 static struct isl_context *isl_context_lex_alloc(struct isl_basic_set *dom)
2625 struct isl_context_lex *clex;
2627 if (!dom)
2628 return NULL;
2630 clex = isl_alloc_type(dom->ctx, struct isl_context_lex);
2631 if (!clex)
2632 return NULL;
2634 clex->context.op = &isl_context_lex_op;
2636 clex->tab = context_tab_for_lexmin(isl_basic_set_copy(dom));
2637 if (restore_lexmin(clex->tab) < 0)
2638 goto error;
2639 clex->tab = check_integer_feasible(clex->tab);
2640 if (!clex->tab)
2641 goto error;
2643 return &clex->context;
2644 error:
2645 clex->context.op->free(&clex->context);
2646 return NULL;
2649 /* Representation of the context when using generalized basis reduction.
2651 * "shifted" contains the offsets of the unit hypercubes that lie inside the
2652 * context. Any rational point in "shifted" can therefore be rounded
2653 * up to an integer point in the context.
2654 * If the context is constrained by any equality, then "shifted" is not used
2655 * as it would be empty.
2657 struct isl_context_gbr {
2658 struct isl_context context;
2659 struct isl_tab *tab;
2660 struct isl_tab *shifted;
2661 struct isl_tab *cone;
2664 static struct isl_tab *context_gbr_detect_nonnegative_parameters(
2665 struct isl_context *context, struct isl_tab *tab)
2667 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2668 if (!tab)
2669 return NULL;
2670 return tab_detect_nonnegative_parameters(tab, cgbr->tab);
2673 static struct isl_basic_set *context_gbr_peek_basic_set(
2674 struct isl_context *context)
2676 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2677 if (!cgbr->tab)
2678 return NULL;
2679 return isl_tab_peek_bset(cgbr->tab);
2682 static struct isl_tab *context_gbr_peek_tab(struct isl_context *context)
2684 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2685 return cgbr->tab;
2688 /* Initialize the "shifted" tableau of the context, which
2689 * contains the constraints of the original tableau shifted
2690 * by the sum of all negative coefficients. This ensures
2691 * that any rational point in the shifted tableau can
2692 * be rounded up to yield an integer point in the original tableau.
2694 static void gbr_init_shifted(struct isl_context_gbr *cgbr)
2696 int i, j;
2697 struct isl_vec *cst;
2698 struct isl_basic_set *bset = isl_tab_peek_bset(cgbr->tab);
2699 unsigned dim = isl_basic_set_total_dim(bset);
2701 cst = isl_vec_alloc(cgbr->tab->mat->ctx, bset->n_ineq);
2702 if (!cst)
2703 return;
2705 for (i = 0; i < bset->n_ineq; ++i) {
2706 isl_int_set(cst->el[i], bset->ineq[i][0]);
2707 for (j = 0; j < dim; ++j) {
2708 if (!isl_int_is_neg(bset->ineq[i][1 + j]))
2709 continue;
2710 isl_int_add(bset->ineq[i][0], bset->ineq[i][0],
2711 bset->ineq[i][1 + j]);
2715 cgbr->shifted = isl_tab_from_basic_set(bset, 0);
2717 for (i = 0; i < bset->n_ineq; ++i)
2718 isl_int_set(bset->ineq[i][0], cst->el[i]);
2720 isl_vec_free(cst);
2723 /* Check if the shifted tableau is non-empty, and if so
2724 * use the sample point to construct an integer point
2725 * of the context tableau.
2727 static struct isl_vec *gbr_get_shifted_sample(struct isl_context_gbr *cgbr)
2729 struct isl_vec *sample;
2731 if (!cgbr->shifted)
2732 gbr_init_shifted(cgbr);
2733 if (!cgbr->shifted)
2734 return NULL;
2735 if (cgbr->shifted->empty)
2736 return isl_vec_alloc(cgbr->tab->mat->ctx, 0);
2738 sample = isl_tab_get_sample_value(cgbr->shifted);
2739 sample = isl_vec_ceil(sample);
2741 return sample;
2744 static struct isl_basic_set *drop_constant_terms(struct isl_basic_set *bset)
2746 int i;
2748 if (!bset)
2749 return NULL;
2751 for (i = 0; i < bset->n_eq; ++i)
2752 isl_int_set_si(bset->eq[i][0], 0);
2754 for (i = 0; i < bset->n_ineq; ++i)
2755 isl_int_set_si(bset->ineq[i][0], 0);
2757 return bset;
2760 static int use_shifted(struct isl_context_gbr *cgbr)
2762 if (!cgbr->tab)
2763 return 0;
2764 return cgbr->tab->bmap->n_eq == 0 && cgbr->tab->bmap->n_div == 0;
2767 static struct isl_vec *gbr_get_sample(struct isl_context_gbr *cgbr)
2769 struct isl_basic_set *bset;
2770 struct isl_basic_set *cone;
2772 if (isl_tab_sample_is_integer(cgbr->tab))
2773 return isl_tab_get_sample_value(cgbr->tab);
2775 if (use_shifted(cgbr)) {
2776 struct isl_vec *sample;
2778 sample = gbr_get_shifted_sample(cgbr);
2779 if (!sample || sample->size > 0)
2780 return sample;
2782 isl_vec_free(sample);
2785 if (!cgbr->cone) {
2786 bset = isl_tab_peek_bset(cgbr->tab);
2787 cgbr->cone = isl_tab_from_recession_cone(bset, 0);
2788 if (!cgbr->cone)
2789 return NULL;
2790 if (isl_tab_track_bset(cgbr->cone,
2791 isl_basic_set_copy(bset)) < 0)
2792 return NULL;
2794 if (isl_tab_detect_implicit_equalities(cgbr->cone) < 0)
2795 return NULL;
2797 if (cgbr->cone->n_dead == cgbr->cone->n_col) {
2798 struct isl_vec *sample;
2799 struct isl_tab_undo *snap;
2801 if (cgbr->tab->basis) {
2802 if (cgbr->tab->basis->n_col != 1 + cgbr->tab->n_var) {
2803 isl_mat_free(cgbr->tab->basis);
2804 cgbr->tab->basis = NULL;
2806 cgbr->tab->n_zero = 0;
2807 cgbr->tab->n_unbounded = 0;
2810 snap = isl_tab_snap(cgbr->tab);
2812 sample = isl_tab_sample(cgbr->tab);
2814 if (!sample || isl_tab_rollback(cgbr->tab, snap) < 0) {
2815 isl_vec_free(sample);
2816 return NULL;
2819 return sample;
2822 cone = isl_basic_set_dup(isl_tab_peek_bset(cgbr->cone));
2823 cone = drop_constant_terms(cone);
2824 cone = isl_basic_set_update_from_tab(cone, cgbr->cone);
2825 cone = isl_basic_set_underlying_set(cone);
2826 cone = isl_basic_set_gauss(cone, NULL);
2828 bset = isl_basic_set_dup(isl_tab_peek_bset(cgbr->tab));
2829 bset = isl_basic_set_update_from_tab(bset, cgbr->tab);
2830 bset = isl_basic_set_underlying_set(bset);
2831 bset = isl_basic_set_gauss(bset, NULL);
2833 return isl_basic_set_sample_with_cone(bset, cone);
2836 static void check_gbr_integer_feasible(struct isl_context_gbr *cgbr)
2838 struct isl_vec *sample;
2840 if (!cgbr->tab)
2841 return;
2843 if (cgbr->tab->empty)
2844 return;
2846 sample = gbr_get_sample(cgbr);
2847 if (!sample)
2848 goto error;
2850 if (sample->size == 0) {
2851 isl_vec_free(sample);
2852 if (isl_tab_mark_empty(cgbr->tab) < 0)
2853 goto error;
2854 return;
2857 if (isl_tab_add_sample(cgbr->tab, sample) < 0)
2858 goto error;
2860 return;
2861 error:
2862 isl_tab_free(cgbr->tab);
2863 cgbr->tab = NULL;
2866 static struct isl_tab *add_gbr_eq(struct isl_tab *tab, isl_int *eq)
2868 if (!tab)
2869 return NULL;
2871 if (isl_tab_extend_cons(tab, 2) < 0)
2872 goto error;
2874 if (isl_tab_add_eq(tab, eq) < 0)
2875 goto error;
2877 return tab;
2878 error:
2879 isl_tab_free(tab);
2880 return NULL;
2883 /* Add the equality described by "eq" to the context.
2884 * If "check" is set, then we check if the context is empty after
2885 * adding the equality.
2886 * If "update" is set, then we check if the samples are still valid.
2888 * We do not explicitly add shifted copies of the equality to
2889 * cgbr->shifted since they would conflict with each other.
2890 * Instead, we directly mark cgbr->shifted empty.
2892 static void context_gbr_add_eq(struct isl_context *context, isl_int *eq,
2893 int check, int update)
2895 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2897 cgbr->tab = add_gbr_eq(cgbr->tab, eq);
2899 if (cgbr->shifted && !cgbr->shifted->empty && use_shifted(cgbr)) {
2900 if (isl_tab_mark_empty(cgbr->shifted) < 0)
2901 goto error;
2904 if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
2905 if (isl_tab_extend_cons(cgbr->cone, 2) < 0)
2906 goto error;
2907 if (isl_tab_add_eq(cgbr->cone, eq) < 0)
2908 goto error;
2911 if (check) {
2912 int v = tab_has_valid_sample(cgbr->tab, eq, 1);
2913 if (v < 0)
2914 goto error;
2915 if (!v)
2916 check_gbr_integer_feasible(cgbr);
2918 if (update)
2919 cgbr->tab = check_samples(cgbr->tab, eq, 1);
2920 return;
2921 error:
2922 isl_tab_free(cgbr->tab);
2923 cgbr->tab = NULL;
2926 static void add_gbr_ineq(struct isl_context_gbr *cgbr, isl_int *ineq)
2928 if (!cgbr->tab)
2929 return;
2931 if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
2932 goto error;
2934 if (isl_tab_add_ineq(cgbr->tab, ineq) < 0)
2935 goto error;
2937 if (cgbr->shifted && !cgbr->shifted->empty && use_shifted(cgbr)) {
2938 int i;
2939 unsigned dim;
2940 dim = isl_basic_map_total_dim(cgbr->tab->bmap);
2942 if (isl_tab_extend_cons(cgbr->shifted, 1) < 0)
2943 goto error;
2945 for (i = 0; i < dim; ++i) {
2946 if (!isl_int_is_neg(ineq[1 + i]))
2947 continue;
2948 isl_int_add(ineq[0], ineq[0], ineq[1 + i]);
2951 if (isl_tab_add_ineq(cgbr->shifted, ineq) < 0)
2952 goto error;
2954 for (i = 0; i < dim; ++i) {
2955 if (!isl_int_is_neg(ineq[1 + i]))
2956 continue;
2957 isl_int_sub(ineq[0], ineq[0], ineq[1 + i]);
2961 if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
2962 if (isl_tab_extend_cons(cgbr->cone, 1) < 0)
2963 goto error;
2964 if (isl_tab_add_ineq(cgbr->cone, ineq) < 0)
2965 goto error;
2968 return;
2969 error:
2970 isl_tab_free(cgbr->tab);
2971 cgbr->tab = NULL;
2974 static void context_gbr_add_ineq(struct isl_context *context, isl_int *ineq,
2975 int check, int update)
2977 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2979 add_gbr_ineq(cgbr, ineq);
2980 if (!cgbr->tab)
2981 return;
2983 if (check) {
2984 int v = tab_has_valid_sample(cgbr->tab, ineq, 0);
2985 if (v < 0)
2986 goto error;
2987 if (!v)
2988 check_gbr_integer_feasible(cgbr);
2990 if (update)
2991 cgbr->tab = check_samples(cgbr->tab, ineq, 0);
2992 return;
2993 error:
2994 isl_tab_free(cgbr->tab);
2995 cgbr->tab = NULL;
2998 static int context_gbr_add_ineq_wrap(void *user, isl_int *ineq)
3000 struct isl_context *context = (struct isl_context *)user;
3001 context_gbr_add_ineq(context, ineq, 0, 0);
3002 return context->op->is_ok(context) ? 0 : -1;
3005 static enum isl_tab_row_sign context_gbr_ineq_sign(struct isl_context *context,
3006 isl_int *ineq, int strict)
3008 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3009 return tab_ineq_sign(cgbr->tab, ineq, strict);
3012 /* Check whether "ineq" can be added to the tableau without rendering
3013 * it infeasible.
3015 static int context_gbr_test_ineq(struct isl_context *context, isl_int *ineq)
3017 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3018 struct isl_tab_undo *snap;
3019 struct isl_tab_undo *shifted_snap = NULL;
3020 struct isl_tab_undo *cone_snap = NULL;
3021 int feasible;
3023 if (!cgbr->tab)
3024 return -1;
3026 if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
3027 return -1;
3029 snap = isl_tab_snap(cgbr->tab);
3030 if (cgbr->shifted)
3031 shifted_snap = isl_tab_snap(cgbr->shifted);
3032 if (cgbr->cone)
3033 cone_snap = isl_tab_snap(cgbr->cone);
3034 add_gbr_ineq(cgbr, ineq);
3035 check_gbr_integer_feasible(cgbr);
3036 if (!cgbr->tab)
3037 return -1;
3038 feasible = !cgbr->tab->empty;
3039 if (isl_tab_rollback(cgbr->tab, snap) < 0)
3040 return -1;
3041 if (shifted_snap) {
3042 if (isl_tab_rollback(cgbr->shifted, shifted_snap))
3043 return -1;
3044 } else if (cgbr->shifted) {
3045 isl_tab_free(cgbr->shifted);
3046 cgbr->shifted = NULL;
3048 if (cone_snap) {
3049 if (isl_tab_rollback(cgbr->cone, cone_snap))
3050 return -1;
3051 } else if (cgbr->cone) {
3052 isl_tab_free(cgbr->cone);
3053 cgbr->cone = NULL;
3056 return feasible;
3059 /* Return the column of the last of the variables associated to
3060 * a column that has a non-zero coefficient.
3061 * This function is called in a context where only coefficients
3062 * of parameters or divs can be non-zero.
3064 static int last_non_zero_var_col(struct isl_tab *tab, isl_int *p)
3066 int i;
3067 int col;
3069 if (tab->n_var == 0)
3070 return -1;
3072 for (i = tab->n_var - 1; i >= 0; --i) {
3073 if (i >= tab->n_param && i < tab->n_var - tab->n_div)
3074 continue;
3075 if (tab->var[i].is_row)
3076 continue;
3077 col = tab->var[i].index;
3078 if (!isl_int_is_zero(p[col]))
3079 return col;
3082 return -1;
3085 /* Look through all the recently added equalities in the context
3086 * to see if we can propagate any of them to the main tableau.
3088 * The newly added equalities in the context are encoded as pairs
3089 * of inequalities starting at inequality "first".
3091 * We tentatively add each of these equalities to the main tableau
3092 * and if this happens to result in a row with a final coefficient
3093 * that is one or negative one, we use it to kill a column
3094 * in the main tableau. Otherwise, we discard the tentatively
3095 * added row.
3096 * This tentative addition of equality constraints turns
3097 * on the undo facility of the tableau. Turn it off again
3098 * at the end, assuming it was turned off to begin with.
3100 * Return 0 on success and -1 on failure.
3102 static int propagate_equalities(struct isl_context_gbr *cgbr,
3103 struct isl_tab *tab, unsigned first)
3105 int i;
3106 struct isl_vec *eq = NULL;
3107 isl_bool needs_undo;
3109 needs_undo = isl_tab_need_undo(tab);
3110 if (needs_undo < 0)
3111 goto error;
3112 eq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
3113 if (!eq)
3114 goto error;
3116 if (isl_tab_extend_cons(tab, (cgbr->tab->bmap->n_ineq - first)/2) < 0)
3117 goto error;
3119 isl_seq_clr(eq->el + 1 + tab->n_param,
3120 tab->n_var - tab->n_param - tab->n_div);
3121 for (i = first; i < cgbr->tab->bmap->n_ineq; i += 2) {
3122 int j;
3123 int r;
3124 struct isl_tab_undo *snap;
3125 snap = isl_tab_snap(tab);
3127 isl_seq_cpy(eq->el, cgbr->tab->bmap->ineq[i], 1 + tab->n_param);
3128 isl_seq_cpy(eq->el + 1 + tab->n_var - tab->n_div,
3129 cgbr->tab->bmap->ineq[i] + 1 + tab->n_param,
3130 tab->n_div);
3132 r = isl_tab_add_row(tab, eq->el);
3133 if (r < 0)
3134 goto error;
3135 r = tab->con[r].index;
3136 j = last_non_zero_var_col(tab, tab->mat->row[r] + 2 + tab->M);
3137 if (j < 0 || j < tab->n_dead ||
3138 !isl_int_is_one(tab->mat->row[r][0]) ||
3139 (!isl_int_is_one(tab->mat->row[r][2 + tab->M + j]) &&
3140 !isl_int_is_negone(tab->mat->row[r][2 + tab->M + j]))) {
3141 if (isl_tab_rollback(tab, snap) < 0)
3142 goto error;
3143 continue;
3145 if (isl_tab_pivot(tab, r, j) < 0)
3146 goto error;
3147 if (isl_tab_kill_col(tab, j) < 0)
3148 goto error;
3150 if (restore_lexmin(tab) < 0)
3151 goto error;
3154 if (!needs_undo)
3155 isl_tab_clear_undo(tab);
3156 isl_vec_free(eq);
3158 return 0;
3159 error:
3160 isl_vec_free(eq);
3161 isl_tab_free(cgbr->tab);
3162 cgbr->tab = NULL;
3163 return -1;
3166 static int context_gbr_detect_equalities(struct isl_context *context,
3167 struct isl_tab *tab)
3169 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3170 unsigned n_ineq;
3172 if (!cgbr->cone) {
3173 struct isl_basic_set *bset = isl_tab_peek_bset(cgbr->tab);
3174 cgbr->cone = isl_tab_from_recession_cone(bset, 0);
3175 if (!cgbr->cone)
3176 goto error;
3177 if (isl_tab_track_bset(cgbr->cone,
3178 isl_basic_set_copy(bset)) < 0)
3179 goto error;
3181 if (isl_tab_detect_implicit_equalities(cgbr->cone) < 0)
3182 goto error;
3184 n_ineq = cgbr->tab->bmap->n_ineq;
3185 cgbr->tab = isl_tab_detect_equalities(cgbr->tab, cgbr->cone);
3186 if (!cgbr->tab)
3187 return -1;
3188 if (cgbr->tab->bmap->n_ineq > n_ineq &&
3189 propagate_equalities(cgbr, tab, n_ineq) < 0)
3190 return -1;
3192 return 0;
3193 error:
3194 isl_tab_free(cgbr->tab);
3195 cgbr->tab = NULL;
3196 return -1;
3199 static int context_gbr_get_div(struct isl_context *context, struct isl_tab *tab,
3200 struct isl_vec *div)
3202 return get_div(tab, context, div);
3205 static isl_bool context_gbr_add_div(struct isl_context *context,
3206 __isl_keep isl_vec *div)
3208 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3209 if (cgbr->cone) {
3210 int k;
3212 if (isl_tab_extend_cons(cgbr->cone, 3) < 0)
3213 return isl_bool_error;
3214 if (isl_tab_extend_vars(cgbr->cone, 1) < 0)
3215 return isl_bool_error;
3216 if (isl_tab_allocate_var(cgbr->cone) <0)
3217 return isl_bool_error;
3219 cgbr->cone->bmap = isl_basic_map_extend_space(cgbr->cone->bmap,
3220 isl_basic_map_get_space(cgbr->cone->bmap), 1, 0, 2);
3221 k = isl_basic_map_alloc_div(cgbr->cone->bmap);
3222 if (k < 0)
3223 return isl_bool_error;
3224 isl_seq_cpy(cgbr->cone->bmap->div[k], div->el, div->size);
3225 if (isl_tab_push(cgbr->cone, isl_tab_undo_bmap_div) < 0)
3226 return isl_bool_error;
3228 return context_tab_add_div(cgbr->tab, div,
3229 context_gbr_add_ineq_wrap, context);
3232 static int context_gbr_best_split(struct isl_context *context,
3233 struct isl_tab *tab)
3235 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3236 struct isl_tab_undo *snap;
3237 int r;
3239 snap = isl_tab_snap(cgbr->tab);
3240 r = best_split(tab, cgbr->tab);
3242 if (r >= 0 && isl_tab_rollback(cgbr->tab, snap) < 0)
3243 return -1;
3245 return r;
3248 static int context_gbr_is_empty(struct isl_context *context)
3250 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3251 if (!cgbr->tab)
3252 return -1;
3253 return cgbr->tab->empty;
3256 struct isl_gbr_tab_undo {
3257 struct isl_tab_undo *tab_snap;
3258 struct isl_tab_undo *shifted_snap;
3259 struct isl_tab_undo *cone_snap;
3262 static void *context_gbr_save(struct isl_context *context)
3264 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3265 struct isl_gbr_tab_undo *snap;
3267 if (!cgbr->tab)
3268 return NULL;
3270 snap = isl_alloc_type(cgbr->tab->mat->ctx, struct isl_gbr_tab_undo);
3271 if (!snap)
3272 return NULL;
3274 snap->tab_snap = isl_tab_snap(cgbr->tab);
3275 if (isl_tab_save_samples(cgbr->tab) < 0)
3276 goto error;
3278 if (cgbr->shifted)
3279 snap->shifted_snap = isl_tab_snap(cgbr->shifted);
3280 else
3281 snap->shifted_snap = NULL;
3283 if (cgbr->cone)
3284 snap->cone_snap = isl_tab_snap(cgbr->cone);
3285 else
3286 snap->cone_snap = NULL;
3288 return snap;
3289 error:
3290 free(snap);
3291 return NULL;
3294 static void context_gbr_restore(struct isl_context *context, void *save)
3296 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3297 struct isl_gbr_tab_undo *snap = (struct isl_gbr_tab_undo *)save;
3298 if (!snap)
3299 goto error;
3300 if (isl_tab_rollback(cgbr->tab, snap->tab_snap) < 0)
3301 goto error;
3303 if (snap->shifted_snap) {
3304 if (isl_tab_rollback(cgbr->shifted, snap->shifted_snap) < 0)
3305 goto error;
3306 } else if (cgbr->shifted) {
3307 isl_tab_free(cgbr->shifted);
3308 cgbr->shifted = NULL;
3311 if (snap->cone_snap) {
3312 if (isl_tab_rollback(cgbr->cone, snap->cone_snap) < 0)
3313 goto error;
3314 } else if (cgbr->cone) {
3315 isl_tab_free(cgbr->cone);
3316 cgbr->cone = NULL;
3319 free(snap);
3321 return;
3322 error:
3323 free(snap);
3324 isl_tab_free(cgbr->tab);
3325 cgbr->tab = NULL;
3328 static void context_gbr_discard(void *save)
3330 struct isl_gbr_tab_undo *snap = (struct isl_gbr_tab_undo *)save;
3331 free(snap);
3334 static int context_gbr_is_ok(struct isl_context *context)
3336 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3337 return !!cgbr->tab;
3340 static void context_gbr_invalidate(struct isl_context *context)
3342 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3343 isl_tab_free(cgbr->tab);
3344 cgbr->tab = NULL;
3347 static void context_gbr_free(struct isl_context *context)
3349 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3350 isl_tab_free(cgbr->tab);
3351 isl_tab_free(cgbr->shifted);
3352 isl_tab_free(cgbr->cone);
3353 free(cgbr);
3356 struct isl_context_op isl_context_gbr_op = {
3357 context_gbr_detect_nonnegative_parameters,
3358 context_gbr_peek_basic_set,
3359 context_gbr_peek_tab,
3360 context_gbr_add_eq,
3361 context_gbr_add_ineq,
3362 context_gbr_ineq_sign,
3363 context_gbr_test_ineq,
3364 context_gbr_get_div,
3365 context_gbr_add_div,
3366 context_gbr_detect_equalities,
3367 context_gbr_best_split,
3368 context_gbr_is_empty,
3369 context_gbr_is_ok,
3370 context_gbr_save,
3371 context_gbr_restore,
3372 context_gbr_discard,
3373 context_gbr_invalidate,
3374 context_gbr_free,
3377 static struct isl_context *isl_context_gbr_alloc(__isl_keep isl_basic_set *dom)
3379 struct isl_context_gbr *cgbr;
3381 if (!dom)
3382 return NULL;
3384 cgbr = isl_calloc_type(dom->ctx, struct isl_context_gbr);
3385 if (!cgbr)
3386 return NULL;
3388 cgbr->context.op = &isl_context_gbr_op;
3390 cgbr->shifted = NULL;
3391 cgbr->cone = NULL;
3392 cgbr->tab = isl_tab_from_basic_set(dom, 1);
3393 cgbr->tab = isl_tab_init_samples(cgbr->tab);
3394 if (!cgbr->tab)
3395 goto error;
3396 check_gbr_integer_feasible(cgbr);
3398 return &cgbr->context;
3399 error:
3400 cgbr->context.op->free(&cgbr->context);
3401 return NULL;
3404 static struct isl_context *isl_context_alloc(__isl_keep isl_basic_set *dom)
3406 if (!dom)
3407 return NULL;
3409 if (dom->ctx->opt->context == ISL_CONTEXT_LEXMIN)
3410 return isl_context_lex_alloc(dom);
3411 else
3412 return isl_context_gbr_alloc(dom);
3415 /* Construct an isl_sol_map structure for accumulating the solution.
3416 * If track_empty is set, then we also keep track of the parts
3417 * of the context where there is no solution.
3418 * If max is set, then we are solving a maximization, rather than
3419 * a minimization problem, which means that the variables in the
3420 * tableau have value "M - x" rather than "M + x".
3422 static struct isl_sol *sol_map_init(struct isl_basic_map *bmap,
3423 struct isl_basic_set *dom, int track_empty, int max)
3425 struct isl_sol_map *sol_map = NULL;
3427 if (!bmap)
3428 goto error;
3430 sol_map = isl_calloc_type(bmap->ctx, struct isl_sol_map);
3431 if (!sol_map)
3432 goto error;
3434 sol_map->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
3435 sol_map->sol.dec_level.callback.run = &sol_dec_level_wrap;
3436 sol_map->sol.dec_level.sol = &sol_map->sol;
3437 sol_map->sol.max = max;
3438 sol_map->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
3439 sol_map->sol.add = &sol_map_add_wrap;
3440 sol_map->sol.add_empty = track_empty ? &sol_map_add_empty_wrap : NULL;
3441 sol_map->sol.free = &sol_map_free_wrap;
3442 sol_map->map = isl_map_alloc_space(isl_basic_map_get_space(bmap), 1,
3443 ISL_MAP_DISJOINT);
3444 if (!sol_map->map)
3445 goto error;
3447 sol_map->sol.context = isl_context_alloc(dom);
3448 if (!sol_map->sol.context)
3449 goto error;
3451 if (track_empty) {
3452 sol_map->empty = isl_set_alloc_space(isl_basic_set_get_space(dom),
3453 1, ISL_SET_DISJOINT);
3454 if (!sol_map->empty)
3455 goto error;
3458 isl_basic_set_free(dom);
3459 return &sol_map->sol;
3460 error:
3461 isl_basic_set_free(dom);
3462 sol_map_free(sol_map);
3463 return NULL;
3466 /* Check whether all coefficients of (non-parameter) variables
3467 * are non-positive, meaning that no pivots can be performed on the row.
3469 static int is_critical(struct isl_tab *tab, int row)
3471 int j;
3472 unsigned off = 2 + tab->M;
3474 for (j = tab->n_dead; j < tab->n_col; ++j) {
3475 if (tab->col_var[j] >= 0 &&
3476 (tab->col_var[j] < tab->n_param ||
3477 tab->col_var[j] >= tab->n_var - tab->n_div))
3478 continue;
3480 if (isl_int_is_pos(tab->mat->row[row][off + j]))
3481 return 0;
3484 return 1;
3487 /* Check whether the inequality represented by vec is strict over the integers,
3488 * i.e., there are no integer values satisfying the constraint with
3489 * equality. This happens if the gcd of the coefficients is not a divisor
3490 * of the constant term. If so, scale the constraint down by the gcd
3491 * of the coefficients.
3493 static int is_strict(struct isl_vec *vec)
3495 isl_int gcd;
3496 int strict = 0;
3498 isl_int_init(gcd);
3499 isl_seq_gcd(vec->el + 1, vec->size - 1, &gcd);
3500 if (!isl_int_is_one(gcd)) {
3501 strict = !isl_int_is_divisible_by(vec->el[0], gcd);
3502 isl_int_fdiv_q(vec->el[0], vec->el[0], gcd);
3503 isl_seq_scale_down(vec->el + 1, vec->el + 1, gcd, vec->size-1);
3505 isl_int_clear(gcd);
3507 return strict;
3510 /* Determine the sign of the given row of the main tableau.
3511 * The result is one of
3512 * isl_tab_row_pos: always non-negative; no pivot needed
3513 * isl_tab_row_neg: always non-positive; pivot
3514 * isl_tab_row_any: can be both positive and negative; split
3516 * We first handle some simple cases
3517 * - the row sign may be known already
3518 * - the row may be obviously non-negative
3519 * - the parametric constant may be equal to that of another row
3520 * for which we know the sign. This sign will be either "pos" or
3521 * "any". If it had been "neg" then we would have pivoted before.
3523 * If none of these cases hold, we check the value of the row for each
3524 * of the currently active samples. Based on the signs of these values
3525 * we make an initial determination of the sign of the row.
3527 * all zero -> unk(nown)
3528 * all non-negative -> pos
3529 * all non-positive -> neg
3530 * both negative and positive -> all
3532 * If we end up with "all", we are done.
3533 * Otherwise, we perform a check for positive and/or negative
3534 * values as follows.
3536 * samples neg unk pos
3537 * <0 ? Y N Y N
3538 * pos any pos
3539 * >0 ? Y N Y N
3540 * any neg any neg
3542 * There is no special sign for "zero", because we can usually treat zero
3543 * as either non-negative or non-positive, whatever works out best.
3544 * However, if the row is "critical", meaning that pivoting is impossible
3545 * then we don't want to limp zero with the non-positive case, because
3546 * then we we would lose the solution for those values of the parameters
3547 * where the value of the row is zero. Instead, we treat 0 as non-negative
3548 * ensuring a split if the row can attain both zero and negative values.
3549 * The same happens when the original constraint was one that could not
3550 * be satisfied with equality by any integer values of the parameters.
3551 * In this case, we normalize the constraint, but then a value of zero
3552 * for the normalized constraint is actually a positive value for the
3553 * original constraint, so again we need to treat zero as non-negative.
3554 * In both these cases, we have the following decision tree instead:
3556 * all non-negative -> pos
3557 * all negative -> neg
3558 * both negative and non-negative -> all
3560 * samples neg pos
3561 * <0 ? Y N
3562 * any pos
3563 * >=0 ? Y N
3564 * any neg
3566 static enum isl_tab_row_sign row_sign(struct isl_tab *tab,
3567 struct isl_sol *sol, int row)
3569 struct isl_vec *ineq = NULL;
3570 enum isl_tab_row_sign res = isl_tab_row_unknown;
3571 int critical;
3572 int strict;
3573 int row2;
3575 if (tab->row_sign[row] != isl_tab_row_unknown)
3576 return tab->row_sign[row];
3577 if (is_obviously_nonneg(tab, row))
3578 return isl_tab_row_pos;
3579 for (row2 = tab->n_redundant; row2 < tab->n_row; ++row2) {
3580 if (tab->row_sign[row2] == isl_tab_row_unknown)
3581 continue;
3582 if (identical_parameter_line(tab, row, row2))
3583 return tab->row_sign[row2];
3586 critical = is_critical(tab, row);
3588 ineq = get_row_parameter_ineq(tab, row);
3589 if (!ineq)
3590 goto error;
3592 strict = is_strict(ineq);
3594 res = sol->context->op->ineq_sign(sol->context, ineq->el,
3595 critical || strict);
3597 if (res == isl_tab_row_unknown || res == isl_tab_row_pos) {
3598 /* test for negative values */
3599 int feasible;
3600 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3601 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3603 feasible = sol->context->op->test_ineq(sol->context, ineq->el);
3604 if (feasible < 0)
3605 goto error;
3606 if (!feasible)
3607 res = isl_tab_row_pos;
3608 else
3609 res = (res == isl_tab_row_unknown) ? isl_tab_row_neg
3610 : isl_tab_row_any;
3611 if (res == isl_tab_row_neg) {
3612 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3613 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3617 if (res == isl_tab_row_neg) {
3618 /* test for positive values */
3619 int feasible;
3620 if (!critical && !strict)
3621 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3623 feasible = sol->context->op->test_ineq(sol->context, ineq->el);
3624 if (feasible < 0)
3625 goto error;
3626 if (feasible)
3627 res = isl_tab_row_any;
3630 isl_vec_free(ineq);
3631 return res;
3632 error:
3633 isl_vec_free(ineq);
3634 return isl_tab_row_unknown;
3637 static void find_solutions(struct isl_sol *sol, struct isl_tab *tab);
3639 /* Find solutions for values of the parameters that satisfy the given
3640 * inequality.
3642 * We currently take a snapshot of the context tableau that is reset
3643 * when we return from this function, while we make a copy of the main
3644 * tableau, leaving the original main tableau untouched.
3645 * These are fairly arbitrary choices. Making a copy also of the context
3646 * tableau would obviate the need to undo any changes made to it later,
3647 * while taking a snapshot of the main tableau could reduce memory usage.
3648 * If we were to switch to taking a snapshot of the main tableau,
3649 * we would have to keep in mind that we need to save the row signs
3650 * and that we need to do this before saving the current basis
3651 * such that the basis has been restore before we restore the row signs.
3653 static void find_in_pos(struct isl_sol *sol, struct isl_tab *tab, isl_int *ineq)
3655 void *saved;
3657 if (!sol->context)
3658 goto error;
3659 saved = sol->context->op->save(sol->context);
3661 tab = isl_tab_dup(tab);
3662 if (!tab)
3663 goto error;
3665 sol->context->op->add_ineq(sol->context, ineq, 0, 1);
3667 find_solutions(sol, tab);
3669 if (!sol->error)
3670 sol->context->op->restore(sol->context, saved);
3671 else
3672 sol->context->op->discard(saved);
3673 return;
3674 error:
3675 sol->error = 1;
3678 /* Record the absence of solutions for those values of the parameters
3679 * that do not satisfy the given inequality with equality.
3681 static void no_sol_in_strict(struct isl_sol *sol,
3682 struct isl_tab *tab, struct isl_vec *ineq)
3684 int empty;
3685 void *saved;
3687 if (!sol->context || sol->error)
3688 goto error;
3689 saved = sol->context->op->save(sol->context);
3691 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3693 sol->context->op->add_ineq(sol->context, ineq->el, 1, 0);
3694 if (!sol->context)
3695 goto error;
3697 empty = tab->empty;
3698 tab->empty = 1;
3699 sol_add(sol, tab);
3700 tab->empty = empty;
3702 isl_int_add_ui(ineq->el[0], ineq->el[0], 1);
3704 sol->context->op->restore(sol->context, saved);
3705 return;
3706 error:
3707 sol->error = 1;
3710 /* Reset all row variables that are marked to have a sign that may
3711 * be both positive and negative to have an unknown sign.
3713 static void reset_any_to_unknown(struct isl_tab *tab)
3715 int row;
3717 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3718 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
3719 continue;
3720 if (tab->row_sign[row] == isl_tab_row_any)
3721 tab->row_sign[row] = isl_tab_row_unknown;
3725 /* Compute the lexicographic minimum of the set represented by the main
3726 * tableau "tab" within the context "sol->context_tab".
3727 * On entry the sample value of the main tableau is lexicographically
3728 * less than or equal to this lexicographic minimum.
3729 * Pivots are performed until a feasible point is found, which is then
3730 * necessarily equal to the minimum, or until the tableau is found to
3731 * be infeasible. Some pivots may need to be performed for only some
3732 * feasible values of the context tableau. If so, the context tableau
3733 * is split into a part where the pivot is needed and a part where it is not.
3735 * Whenever we enter the main loop, the main tableau is such that no
3736 * "obvious" pivots need to be performed on it, where "obvious" means
3737 * that the given row can be seen to be negative without looking at
3738 * the context tableau. In particular, for non-parametric problems,
3739 * no pivots need to be performed on the main tableau.
3740 * The caller of find_solutions is responsible for making this property
3741 * hold prior to the first iteration of the loop, while restore_lexmin
3742 * is called before every other iteration.
3744 * Inside the main loop, we first examine the signs of the rows of
3745 * the main tableau within the context of the context tableau.
3746 * If we find a row that is always non-positive for all values of
3747 * the parameters satisfying the context tableau and negative for at
3748 * least one value of the parameters, we perform the appropriate pivot
3749 * and start over. An exception is the case where no pivot can be
3750 * performed on the row. In this case, we require that the sign of
3751 * the row is negative for all values of the parameters (rather than just
3752 * non-positive). This special case is handled inside row_sign, which
3753 * will say that the row can have any sign if it determines that it can
3754 * attain both negative and zero values.
3756 * If we can't find a row that always requires a pivot, but we can find
3757 * one or more rows that require a pivot for some values of the parameters
3758 * (i.e., the row can attain both positive and negative signs), then we split
3759 * the context tableau into two parts, one where we force the sign to be
3760 * non-negative and one where we force is to be negative.
3761 * The non-negative part is handled by a recursive call (through find_in_pos).
3762 * Upon returning from this call, we continue with the negative part and
3763 * perform the required pivot.
3765 * If no such rows can be found, all rows are non-negative and we have
3766 * found a (rational) feasible point. If we only wanted a rational point
3767 * then we are done.
3768 * Otherwise, we check if all values of the sample point of the tableau
3769 * are integral for the variables. If so, we have found the minimal
3770 * integral point and we are done.
3771 * If the sample point is not integral, then we need to make a distinction
3772 * based on whether the constant term is non-integral or the coefficients
3773 * of the parameters. Furthermore, in order to decide how to handle
3774 * the non-integrality, we also need to know whether the coefficients
3775 * of the other columns in the tableau are integral. This leads
3776 * to the following table. The first two rows do not correspond
3777 * to a non-integral sample point and are only mentioned for completeness.
3779 * constant parameters other
3781 * int int int |
3782 * int int rat | -> no problem
3784 * rat int int -> fail
3786 * rat int rat -> cut
3788 * int rat rat |
3789 * rat rat rat | -> parametric cut
3791 * int rat int |
3792 * rat rat int | -> split context
3794 * If the parametric constant is completely integral, then there is nothing
3795 * to be done. If the constant term is non-integral, but all the other
3796 * coefficient are integral, then there is nothing that can be done
3797 * and the tableau has no integral solution.
3798 * If, on the other hand, one or more of the other columns have rational
3799 * coefficients, but the parameter coefficients are all integral, then
3800 * we can perform a regular (non-parametric) cut.
3801 * Finally, if there is any parameter coefficient that is non-integral,
3802 * then we need to involve the context tableau. There are two cases here.
3803 * If at least one other column has a rational coefficient, then we
3804 * can perform a parametric cut in the main tableau by adding a new
3805 * integer division in the context tableau.
3806 * If all other columns have integral coefficients, then we need to
3807 * enforce that the rational combination of parameters (c + \sum a_i y_i)/m
3808 * is always integral. We do this by introducing an integer division
3809 * q = floor((c + \sum a_i y_i)/m) and stipulating that its argument should
3810 * always be integral in the context tableau, i.e., m q = c + \sum a_i y_i.
3811 * Since q is expressed in the tableau as
3812 * c + \sum a_i y_i - m q >= 0
3813 * -c - \sum a_i y_i + m q + m - 1 >= 0
3814 * it is sufficient to add the inequality
3815 * -c - \sum a_i y_i + m q >= 0
3816 * In the part of the context where this inequality does not hold, the
3817 * main tableau is marked as being empty.
3819 static void find_solutions(struct isl_sol *sol, struct isl_tab *tab)
3821 struct isl_context *context;
3822 int r;
3824 if (!tab || sol->error)
3825 goto error;
3827 context = sol->context;
3829 if (tab->empty)
3830 goto done;
3831 if (context->op->is_empty(context))
3832 goto done;
3834 for (r = 0; r >= 0 && tab && !tab->empty; r = restore_lexmin(tab)) {
3835 int flags;
3836 int row;
3837 enum isl_tab_row_sign sgn;
3838 int split = -1;
3839 int n_split = 0;
3841 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3842 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
3843 continue;
3844 sgn = row_sign(tab, sol, row);
3845 if (!sgn)
3846 goto error;
3847 tab->row_sign[row] = sgn;
3848 if (sgn == isl_tab_row_any)
3849 n_split++;
3850 if (sgn == isl_tab_row_any && split == -1)
3851 split = row;
3852 if (sgn == isl_tab_row_neg)
3853 break;
3855 if (row < tab->n_row)
3856 continue;
3857 if (split != -1) {
3858 struct isl_vec *ineq;
3859 if (n_split != 1)
3860 split = context->op->best_split(context, tab);
3861 if (split < 0)
3862 goto error;
3863 ineq = get_row_parameter_ineq(tab, split);
3864 if (!ineq)
3865 goto error;
3866 is_strict(ineq);
3867 reset_any_to_unknown(tab);
3868 tab->row_sign[split] = isl_tab_row_pos;
3869 sol_inc_level(sol);
3870 find_in_pos(sol, tab, ineq->el);
3871 tab->row_sign[split] = isl_tab_row_neg;
3872 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3873 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3874 if (!sol->error)
3875 context->op->add_ineq(context, ineq->el, 0, 1);
3876 isl_vec_free(ineq);
3877 if (sol->error)
3878 goto error;
3879 continue;
3881 if (tab->rational)
3882 break;
3883 row = first_non_integer_row(tab, &flags);
3884 if (row < 0)
3885 break;
3886 if (ISL_FL_ISSET(flags, I_PAR)) {
3887 if (ISL_FL_ISSET(flags, I_VAR)) {
3888 if (isl_tab_mark_empty(tab) < 0)
3889 goto error;
3890 break;
3892 row = add_cut(tab, row);
3893 } else if (ISL_FL_ISSET(flags, I_VAR)) {
3894 struct isl_vec *div;
3895 struct isl_vec *ineq;
3896 int d;
3897 div = get_row_split_div(tab, row);
3898 if (!div)
3899 goto error;
3900 d = context->op->get_div(context, tab, div);
3901 isl_vec_free(div);
3902 if (d < 0)
3903 goto error;
3904 ineq = ineq_for_div(context->op->peek_basic_set(context), d);
3905 if (!ineq)
3906 goto error;
3907 sol_inc_level(sol);
3908 no_sol_in_strict(sol, tab, ineq);
3909 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3910 context->op->add_ineq(context, ineq->el, 1, 1);
3911 isl_vec_free(ineq);
3912 if (sol->error || !context->op->is_ok(context))
3913 goto error;
3914 tab = set_row_cst_to_div(tab, row, d);
3915 if (context->op->is_empty(context))
3916 break;
3917 } else
3918 row = add_parametric_cut(tab, row, context);
3919 if (row < 0)
3920 goto error;
3922 if (r < 0)
3923 goto error;
3924 done:
3925 sol_add(sol, tab);
3926 isl_tab_free(tab);
3927 return;
3928 error:
3929 isl_tab_free(tab);
3930 sol->error = 1;
3933 /* Does "sol" contain a pair of partial solutions that could potentially
3934 * be merged?
3936 * We currently only check that "sol" is not in an error state
3937 * and that there are at least two partial solutions of which the final two
3938 * are defined at the same level.
3940 static int sol_has_mergeable_solutions(struct isl_sol *sol)
3942 if (sol->error)
3943 return 0;
3944 if (!sol->partial)
3945 return 0;
3946 if (!sol->partial->next)
3947 return 0;
3948 return sol->partial->level == sol->partial->next->level;
3951 /* Compute the lexicographic minimum of the set represented by the main
3952 * tableau "tab" within the context "sol->context_tab".
3954 * As a preprocessing step, we first transfer all the purely parametric
3955 * equalities from the main tableau to the context tableau, i.e.,
3956 * parameters that have been pivoted to a row.
3957 * These equalities are ignored by the main algorithm, because the
3958 * corresponding rows may not be marked as being non-negative.
3959 * In parts of the context where the added equality does not hold,
3960 * the main tableau is marked as being empty.
3962 * Before we embark on the actual computation, we save a copy
3963 * of the context. When we return, we check if there are any
3964 * partial solutions that can potentially be merged. If so,
3965 * we perform a rollback to the initial state of the context.
3966 * The merging of partial solutions happens inside calls to
3967 * sol_dec_level that are pushed onto the undo stack of the context.
3968 * If there are no partial solutions that can potentially be merged
3969 * then the rollback is skipped as it would just be wasted effort.
3971 static void find_solutions_main(struct isl_sol *sol, struct isl_tab *tab)
3973 int row;
3974 void *saved;
3976 if (!tab)
3977 goto error;
3979 sol->level = 0;
3981 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3982 int p;
3983 struct isl_vec *eq;
3985 if (tab->row_var[row] < 0)
3986 continue;
3987 if (tab->row_var[row] >= tab->n_param &&
3988 tab->row_var[row] < tab->n_var - tab->n_div)
3989 continue;
3990 if (tab->row_var[row] < tab->n_param)
3991 p = tab->row_var[row];
3992 else
3993 p = tab->row_var[row]
3994 + tab->n_param - (tab->n_var - tab->n_div);
3996 eq = isl_vec_alloc(tab->mat->ctx, 1+tab->n_param+tab->n_div);
3997 if (!eq)
3998 goto error;
3999 get_row_parameter_line(tab, row, eq->el);
4000 isl_int_neg(eq->el[1 + p], tab->mat->row[row][0]);
4001 eq = isl_vec_normalize(eq);
4003 sol_inc_level(sol);
4004 no_sol_in_strict(sol, tab, eq);
4006 isl_seq_neg(eq->el, eq->el, eq->size);
4007 sol_inc_level(sol);
4008 no_sol_in_strict(sol, tab, eq);
4009 isl_seq_neg(eq->el, eq->el, eq->size);
4011 sol->context->op->add_eq(sol->context, eq->el, 1, 1);
4013 isl_vec_free(eq);
4015 if (isl_tab_mark_redundant(tab, row) < 0)
4016 goto error;
4018 if (sol->context->op->is_empty(sol->context))
4019 break;
4021 row = tab->n_redundant - 1;
4024 saved = sol->context->op->save(sol->context);
4026 find_solutions(sol, tab);
4028 if (sol_has_mergeable_solutions(sol))
4029 sol->context->op->restore(sol->context, saved);
4030 else
4031 sol->context->op->discard(saved);
4033 sol->level = 0;
4034 sol_pop(sol);
4036 return;
4037 error:
4038 isl_tab_free(tab);
4039 sol->error = 1;
4042 /* Check if integer division "div" of "dom" also occurs in "bmap".
4043 * If so, return its position within the divs.
4044 * If not, return -1.
4046 static int find_context_div(struct isl_basic_map *bmap,
4047 struct isl_basic_set *dom, unsigned div)
4049 int i;
4050 unsigned b_dim = isl_space_dim(bmap->dim, isl_dim_all);
4051 unsigned d_dim = isl_space_dim(dom->dim, isl_dim_all);
4053 if (isl_int_is_zero(dom->div[div][0]))
4054 return -1;
4055 if (isl_seq_first_non_zero(dom->div[div] + 2 + d_dim, dom->n_div) != -1)
4056 return -1;
4058 for (i = 0; i < bmap->n_div; ++i) {
4059 if (isl_int_is_zero(bmap->div[i][0]))
4060 continue;
4061 if (isl_seq_first_non_zero(bmap->div[i] + 2 + d_dim,
4062 (b_dim - d_dim) + bmap->n_div) != -1)
4063 continue;
4064 if (isl_seq_eq(bmap->div[i], dom->div[div], 2 + d_dim))
4065 return i;
4067 return -1;
4070 /* The correspondence between the variables in the main tableau,
4071 * the context tableau, and the input map and domain is as follows.
4072 * The first n_param and the last n_div variables of the main tableau
4073 * form the variables of the context tableau.
4074 * In the basic map, these n_param variables correspond to the
4075 * parameters and the input dimensions. In the domain, they correspond
4076 * to the parameters and the set dimensions.
4077 * The n_div variables correspond to the integer divisions in the domain.
4078 * To ensure that everything lines up, we may need to copy some of the
4079 * integer divisions of the domain to the map. These have to be placed
4080 * in the same order as those in the context and they have to be placed
4081 * after any other integer divisions that the map may have.
4082 * This function performs the required reordering.
4084 static struct isl_basic_map *align_context_divs(struct isl_basic_map *bmap,
4085 struct isl_basic_set *dom)
4087 int i;
4088 int common = 0;
4089 int other;
4091 for (i = 0; i < dom->n_div; ++i)
4092 if (find_context_div(bmap, dom, i) != -1)
4093 common++;
4094 other = bmap->n_div - common;
4095 if (dom->n_div - common > 0) {
4096 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
4097 dom->n_div - common, 0, 0);
4098 if (!bmap)
4099 return NULL;
4101 for (i = 0; i < dom->n_div; ++i) {
4102 int pos = find_context_div(bmap, dom, i);
4103 if (pos < 0) {
4104 pos = isl_basic_map_alloc_div(bmap);
4105 if (pos < 0)
4106 goto error;
4107 isl_int_set_si(bmap->div[pos][0], 0);
4109 if (pos != other + i)
4110 isl_basic_map_swap_div(bmap, pos, other + i);
4112 return bmap;
4113 error:
4114 isl_basic_map_free(bmap);
4115 return NULL;
4118 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4119 * some obvious symmetries.
4121 * We make sure the divs in the domain are properly ordered,
4122 * because they will be added one by one in the given order
4123 * during the construction of the solution map.
4125 static struct isl_sol *basic_map_partial_lexopt_base_sol(
4126 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4127 __isl_give isl_set **empty, int max,
4128 struct isl_sol *(*init)(__isl_keep isl_basic_map *bmap,
4129 __isl_take isl_basic_set *dom, int track_empty, int max))
4131 struct isl_tab *tab;
4132 struct isl_sol *sol = NULL;
4133 struct isl_context *context;
4135 if (dom->n_div) {
4136 dom = isl_basic_set_order_divs(dom);
4137 bmap = align_context_divs(bmap, dom);
4139 sol = init(bmap, dom, !!empty, max);
4140 if (!sol)
4141 goto error;
4143 context = sol->context;
4144 if (isl_basic_set_plain_is_empty(context->op->peek_basic_set(context)))
4145 /* nothing */;
4146 else if (isl_basic_map_plain_is_empty(bmap)) {
4147 if (sol->add_empty)
4148 sol->add_empty(sol,
4149 isl_basic_set_copy(context->op->peek_basic_set(context)));
4150 } else {
4151 tab = tab_for_lexmin(bmap,
4152 context->op->peek_basic_set(context), 1, max);
4153 tab = context->op->detect_nonnegative_parameters(context, tab);
4154 find_solutions_main(sol, tab);
4156 if (sol->error)
4157 goto error;
4159 isl_basic_map_free(bmap);
4160 return sol;
4161 error:
4162 sol_free(sol);
4163 isl_basic_map_free(bmap);
4164 return NULL;
4167 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4168 * some obvious symmetries.
4170 * We call basic_map_partial_lexopt_base_sol and extract the results.
4172 static __isl_give isl_map *basic_map_partial_lexopt_base(
4173 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4174 __isl_give isl_set **empty, int max)
4176 isl_map *result = NULL;
4177 struct isl_sol *sol;
4178 struct isl_sol_map *sol_map;
4180 sol = basic_map_partial_lexopt_base_sol(bmap, dom, empty, max,
4181 &sol_map_init);
4182 if (!sol)
4183 return NULL;
4184 sol_map = (struct isl_sol_map *) sol;
4186 result = isl_map_copy(sol_map->map);
4187 if (empty)
4188 *empty = isl_set_copy(sol_map->empty);
4189 sol_free(&sol_map->sol);
4190 return result;
4193 /* Return a count of the number of occurrences of the "n" first
4194 * variables in the inequality constraints of "bmap".
4196 static __isl_give int *count_occurrences(__isl_keep isl_basic_map *bmap,
4197 int n)
4199 int i, j;
4200 isl_ctx *ctx;
4201 int *occurrences;
4203 if (!bmap)
4204 return NULL;
4205 ctx = isl_basic_map_get_ctx(bmap);
4206 occurrences = isl_calloc_array(ctx, int, n);
4207 if (!occurrences)
4208 return NULL;
4210 for (i = 0; i < bmap->n_ineq; ++i) {
4211 for (j = 0; j < n; ++j) {
4212 if (!isl_int_is_zero(bmap->ineq[i][1 + j]))
4213 occurrences[j]++;
4217 return occurrences;
4220 /* Do all of the "n" variables with non-zero coefficients in "c"
4221 * occur in exactly a single constraint.
4222 * "occurrences" is an array of length "n" containing the number
4223 * of occurrences of each of the variables in the inequality constraints.
4225 static int single_occurrence(int n, isl_int *c, int *occurrences)
4227 int i;
4229 for (i = 0; i < n; ++i) {
4230 if (isl_int_is_zero(c[i]))
4231 continue;
4232 if (occurrences[i] != 1)
4233 return 0;
4236 return 1;
4239 /* Do all of the "n" initial variables that occur in inequality constraint
4240 * "ineq" of "bmap" only occur in that constraint?
4242 static int all_single_occurrence(__isl_keep isl_basic_map *bmap, int ineq,
4243 int n)
4245 int i, j;
4247 for (i = 0; i < n; ++i) {
4248 if (isl_int_is_zero(bmap->ineq[ineq][1 + i]))
4249 continue;
4250 for (j = 0; j < bmap->n_ineq; ++j) {
4251 if (j == ineq)
4252 continue;
4253 if (!isl_int_is_zero(bmap->ineq[j][1 + i]))
4254 return 0;
4258 return 1;
4261 /* Structure used during detection of parallel constraints.
4262 * n_in: number of "input" variables: isl_dim_param + isl_dim_in
4263 * n_out: number of "output" variables: isl_dim_out + isl_dim_div
4264 * val: the coefficients of the output variables
4266 struct isl_constraint_equal_info {
4267 isl_basic_map *bmap;
4268 unsigned n_in;
4269 unsigned n_out;
4270 isl_int *val;
4273 /* Check whether the coefficients of the output variables
4274 * of the constraint in "entry" are equal to info->val.
4276 static int constraint_equal(const void *entry, const void *val)
4278 isl_int **row = (isl_int **)entry;
4279 const struct isl_constraint_equal_info *info = val;
4281 return isl_seq_eq((*row) + 1 + info->n_in, info->val, info->n_out);
4284 /* Check whether "bmap" has a pair of constraints that have
4285 * the same coefficients for the output variables.
4286 * Note that the coefficients of the existentially quantified
4287 * variables need to be zero since the existentially quantified
4288 * of the result are usually not the same as those of the input.
4289 * Furthermore, check that each of the input variables that occur
4290 * in those constraints does not occur in any other constraint.
4291 * If so, return 1 and return the row indices of the two constraints
4292 * in *first and *second.
4294 static int parallel_constraints(__isl_keep isl_basic_map *bmap,
4295 int *first, int *second)
4297 int i;
4298 isl_ctx *ctx;
4299 int *occurrences = NULL;
4300 struct isl_hash_table *table = NULL;
4301 struct isl_hash_table_entry *entry;
4302 struct isl_constraint_equal_info info;
4303 unsigned n_out;
4304 unsigned n_div;
4306 ctx = isl_basic_map_get_ctx(bmap);
4307 table = isl_hash_table_alloc(ctx, bmap->n_ineq);
4308 if (!table)
4309 goto error;
4311 info.n_in = isl_basic_map_dim(bmap, isl_dim_param) +
4312 isl_basic_map_dim(bmap, isl_dim_in);
4313 occurrences = count_occurrences(bmap, info.n_in);
4314 if (info.n_in && !occurrences)
4315 goto error;
4316 info.bmap = bmap;
4317 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4318 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4319 info.n_out = n_out + n_div;
4320 for (i = 0; i < bmap->n_ineq; ++i) {
4321 uint32_t hash;
4323 info.val = bmap->ineq[i] + 1 + info.n_in;
4324 if (isl_seq_first_non_zero(info.val, n_out) < 0)
4325 continue;
4326 if (isl_seq_first_non_zero(info.val + n_out, n_div) >= 0)
4327 continue;
4328 if (!single_occurrence(info.n_in, bmap->ineq[i] + 1,
4329 occurrences))
4330 continue;
4331 hash = isl_seq_get_hash(info.val, info.n_out);
4332 entry = isl_hash_table_find(ctx, table, hash,
4333 constraint_equal, &info, 1);
4334 if (!entry)
4335 goto error;
4336 if (entry->data)
4337 break;
4338 entry->data = &bmap->ineq[i];
4341 if (i < bmap->n_ineq) {
4342 *first = ((isl_int **)entry->data) - bmap->ineq;
4343 *second = i;
4346 isl_hash_table_free(ctx, table);
4347 free(occurrences);
4349 return i < bmap->n_ineq;
4350 error:
4351 isl_hash_table_free(ctx, table);
4352 free(occurrences);
4353 return -1;
4356 /* Given a set of upper bounds in "var", add constraints to "bset"
4357 * that make the i-th bound smallest.
4359 * In particular, if there are n bounds b_i, then add the constraints
4361 * b_i <= b_j for j > i
4362 * b_i < b_j for j < i
4364 static __isl_give isl_basic_set *select_minimum(__isl_take isl_basic_set *bset,
4365 __isl_keep isl_mat *var, int i)
4367 isl_ctx *ctx;
4368 int j, k;
4370 ctx = isl_mat_get_ctx(var);
4372 for (j = 0; j < var->n_row; ++j) {
4373 if (j == i)
4374 continue;
4375 k = isl_basic_set_alloc_inequality(bset);
4376 if (k < 0)
4377 goto error;
4378 isl_seq_combine(bset->ineq[k], ctx->one, var->row[j],
4379 ctx->negone, var->row[i], var->n_col);
4380 isl_int_set_si(bset->ineq[k][var->n_col], 0);
4381 if (j < i)
4382 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
4385 bset = isl_basic_set_finalize(bset);
4387 return bset;
4388 error:
4389 isl_basic_set_free(bset);
4390 return NULL;
4393 /* Given a set of upper bounds on the last "input" variable m,
4394 * construct a set that assigns the minimal upper bound to m, i.e.,
4395 * construct a set that divides the space into cells where one
4396 * of the upper bounds is smaller than all the others and assign
4397 * this upper bound to m.
4399 * In particular, if there are n bounds b_i, then the result
4400 * consists of n basic sets, each one of the form
4402 * m = b_i
4403 * b_i <= b_j for j > i
4404 * b_i < b_j for j < i
4406 static __isl_give isl_set *set_minimum(__isl_take isl_space *dim,
4407 __isl_take isl_mat *var)
4409 int i, k;
4410 isl_basic_set *bset = NULL;
4411 isl_set *set = NULL;
4413 if (!dim || !var)
4414 goto error;
4416 set = isl_set_alloc_space(isl_space_copy(dim),
4417 var->n_row, ISL_SET_DISJOINT);
4419 for (i = 0; i < var->n_row; ++i) {
4420 bset = isl_basic_set_alloc_space(isl_space_copy(dim), 0,
4421 1, var->n_row - 1);
4422 k = isl_basic_set_alloc_equality(bset);
4423 if (k < 0)
4424 goto error;
4425 isl_seq_cpy(bset->eq[k], var->row[i], var->n_col);
4426 isl_int_set_si(bset->eq[k][var->n_col], -1);
4427 bset = select_minimum(bset, var, i);
4428 set = isl_set_add_basic_set(set, bset);
4431 isl_space_free(dim);
4432 isl_mat_free(var);
4433 return set;
4434 error:
4435 isl_basic_set_free(bset);
4436 isl_set_free(set);
4437 isl_space_free(dim);
4438 isl_mat_free(var);
4439 return NULL;
4442 /* Given that the last input variable of "bmap" represents the minimum
4443 * of the bounds in "cst", check whether we need to split the domain
4444 * based on which bound attains the minimum.
4446 * A split is needed when the minimum appears in an integer division
4447 * or in an equality. Otherwise, it is only needed if it appears in
4448 * an upper bound that is different from the upper bounds on which it
4449 * is defined.
4451 static int need_split_basic_map(__isl_keep isl_basic_map *bmap,
4452 __isl_keep isl_mat *cst)
4454 int i, j;
4455 unsigned total;
4456 unsigned pos;
4458 pos = cst->n_col - 1;
4459 total = isl_basic_map_dim(bmap, isl_dim_all);
4461 for (i = 0; i < bmap->n_div; ++i)
4462 if (!isl_int_is_zero(bmap->div[i][2 + pos]))
4463 return 1;
4465 for (i = 0; i < bmap->n_eq; ++i)
4466 if (!isl_int_is_zero(bmap->eq[i][1 + pos]))
4467 return 1;
4469 for (i = 0; i < bmap->n_ineq; ++i) {
4470 if (isl_int_is_nonneg(bmap->ineq[i][1 + pos]))
4471 continue;
4472 if (!isl_int_is_negone(bmap->ineq[i][1 + pos]))
4473 return 1;
4474 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + pos + 1,
4475 total - pos - 1) >= 0)
4476 return 1;
4478 for (j = 0; j < cst->n_row; ++j)
4479 if (isl_seq_eq(bmap->ineq[i], cst->row[j], cst->n_col))
4480 break;
4481 if (j >= cst->n_row)
4482 return 1;
4485 return 0;
4488 /* Given that the last set variable of "bset" represents the minimum
4489 * of the bounds in "cst", check whether we need to split the domain
4490 * based on which bound attains the minimum.
4492 * We simply call need_split_basic_map here. This is safe because
4493 * the position of the minimum is computed from "cst" and not
4494 * from "bmap".
4496 static int need_split_basic_set(__isl_keep isl_basic_set *bset,
4497 __isl_keep isl_mat *cst)
4499 return need_split_basic_map((isl_basic_map *)bset, cst);
4502 /* Given that the last set variable of "set" represents the minimum
4503 * of the bounds in "cst", check whether we need to split the domain
4504 * based on which bound attains the minimum.
4506 static int need_split_set(__isl_keep isl_set *set, __isl_keep isl_mat *cst)
4508 int i;
4510 for (i = 0; i < set->n; ++i)
4511 if (need_split_basic_set(set->p[i], cst))
4512 return 1;
4514 return 0;
4517 /* Given a set of which the last set variable is the minimum
4518 * of the bounds in "cst", split each basic set in the set
4519 * in pieces where one of the bounds is (strictly) smaller than the others.
4520 * This subdivision is given in "min_expr".
4521 * The variable is subsequently projected out.
4523 * We only do the split when it is needed.
4524 * For example if the last input variable m = min(a,b) and the only
4525 * constraints in the given basic set are lower bounds on m,
4526 * i.e., l <= m = min(a,b), then we can simply project out m
4527 * to obtain l <= a and l <= b, without having to split on whether
4528 * m is equal to a or b.
4530 static __isl_give isl_set *split(__isl_take isl_set *empty,
4531 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
4533 int n_in;
4534 int i;
4535 isl_space *dim;
4536 isl_set *res;
4538 if (!empty || !min_expr || !cst)
4539 goto error;
4541 n_in = isl_set_dim(empty, isl_dim_set);
4542 dim = isl_set_get_space(empty);
4543 dim = isl_space_drop_dims(dim, isl_dim_set, n_in - 1, 1);
4544 res = isl_set_empty(dim);
4546 for (i = 0; i < empty->n; ++i) {
4547 isl_set *set;
4549 set = isl_set_from_basic_set(isl_basic_set_copy(empty->p[i]));
4550 if (need_split_basic_set(empty->p[i], cst))
4551 set = isl_set_intersect(set, isl_set_copy(min_expr));
4552 set = isl_set_remove_dims(set, isl_dim_set, n_in - 1, 1);
4554 res = isl_set_union_disjoint(res, set);
4557 isl_set_free(empty);
4558 isl_set_free(min_expr);
4559 isl_mat_free(cst);
4560 return res;
4561 error:
4562 isl_set_free(empty);
4563 isl_set_free(min_expr);
4564 isl_mat_free(cst);
4565 return NULL;
4568 /* Given a map of which the last input variable is the minimum
4569 * of the bounds in "cst", split each basic set in the set
4570 * in pieces where one of the bounds is (strictly) smaller than the others.
4571 * This subdivision is given in "min_expr".
4572 * The variable is subsequently projected out.
4574 * The implementation is essentially the same as that of "split".
4576 static __isl_give isl_map *split_domain(__isl_take isl_map *opt,
4577 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
4579 int n_in;
4580 int i;
4581 isl_space *dim;
4582 isl_map *res;
4584 if (!opt || !min_expr || !cst)
4585 goto error;
4587 n_in = isl_map_dim(opt, isl_dim_in);
4588 dim = isl_map_get_space(opt);
4589 dim = isl_space_drop_dims(dim, isl_dim_in, n_in - 1, 1);
4590 res = isl_map_empty(dim);
4592 for (i = 0; i < opt->n; ++i) {
4593 isl_map *map;
4595 map = isl_map_from_basic_map(isl_basic_map_copy(opt->p[i]));
4596 if (need_split_basic_map(opt->p[i], cst))
4597 map = isl_map_intersect_domain(map,
4598 isl_set_copy(min_expr));
4599 map = isl_map_remove_dims(map, isl_dim_in, n_in - 1, 1);
4601 res = isl_map_union_disjoint(res, map);
4604 isl_map_free(opt);
4605 isl_set_free(min_expr);
4606 isl_mat_free(cst);
4607 return res;
4608 error:
4609 isl_map_free(opt);
4610 isl_set_free(min_expr);
4611 isl_mat_free(cst);
4612 return NULL;
4615 static __isl_give isl_map *basic_map_partial_lexopt(
4616 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4617 __isl_give isl_set **empty, int max);
4619 /* This function is called from basic_map_partial_lexopt_symm.
4620 * The last variable of "bmap" and "dom" corresponds to the minimum
4621 * of the bounds in "cst". "map_space" is the space of the original
4622 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
4623 * is the space of the original domain.
4625 * We recursively call basic_map_partial_lexopt and then plug in
4626 * the definition of the minimum in the result.
4628 static __isl_give isl_map *basic_map_partial_lexopt_symm_core(
4629 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4630 __isl_give isl_set **empty, int max, __isl_take isl_mat *cst,
4631 __isl_take isl_space *map_space, __isl_take isl_space *set_space)
4633 isl_map *opt;
4634 isl_set *min_expr;
4636 min_expr = set_minimum(isl_basic_set_get_space(dom), isl_mat_copy(cst));
4638 opt = basic_map_partial_lexopt(bmap, dom, empty, max);
4640 if (empty) {
4641 *empty = split(*empty,
4642 isl_set_copy(min_expr), isl_mat_copy(cst));
4643 *empty = isl_set_reset_space(*empty, set_space);
4646 opt = split_domain(opt, min_expr, cst);
4647 opt = isl_map_reset_space(opt, map_space);
4649 return opt;
4652 /* Extract a domain from "bmap" for the purpose of computing
4653 * a lexicographic optimum.
4655 * This function is only called when the caller wants to compute a full
4656 * lexicographic optimum, i.e., without specifying a domain. In this case,
4657 * the caller is not interested in the part of the domain space where
4658 * there is no solution and the domain can be initialized to those constraints
4659 * of "bmap" that only involve the parameters and the input dimensions.
4660 * This relieves the parametric programming engine from detecting those
4661 * inequalities and transferring them to the context. More importantly,
4662 * it ensures that those inequalities are transferred first and not
4663 * intermixed with inequalities that actually split the domain.
4665 * If the caller does not require the absence of existentially quantified
4666 * variables in the result (i.e., if ISL_OPT_QE is not set in "flags"),
4667 * then the actual domain of "bmap" can be used. This ensures that
4668 * the domain does not need to be split at all just to separate out
4669 * pieces of the domain that do not have a solution from piece that do.
4670 * This domain cannot be used in general because it may involve
4671 * (unknown) existentially quantified variables which will then also
4672 * appear in the solution.
4674 static __isl_give isl_basic_set *extract_domain(__isl_keep isl_basic_map *bmap,
4675 unsigned flags)
4677 int n_div;
4678 int n_out;
4680 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4681 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4682 bmap = isl_basic_map_copy(bmap);
4683 if (ISL_FL_ISSET(flags, ISL_OPT_QE)) {
4684 bmap = isl_basic_map_drop_constraints_involving_dims(bmap,
4685 isl_dim_div, 0, n_div);
4686 bmap = isl_basic_map_drop_constraints_involving_dims(bmap,
4687 isl_dim_out, 0, n_out);
4689 return isl_basic_map_domain(bmap);
4692 #undef TYPE
4693 #define TYPE isl_map
4694 #undef SUFFIX
4695 #define SUFFIX
4696 #include "isl_tab_lexopt_templ.c"
4698 struct isl_sol_for {
4699 struct isl_sol sol;
4700 int (*fn)(__isl_take isl_basic_set *dom,
4701 __isl_take isl_aff_list *list, void *user);
4702 void *user;
4705 static void sol_for_free(struct isl_sol_for *sol_for)
4707 if (!sol_for)
4708 return;
4709 if (sol_for->sol.context)
4710 sol_for->sol.context->op->free(sol_for->sol.context);
4711 free(sol_for);
4714 static void sol_for_free_wrap(struct isl_sol *sol)
4716 sol_for_free((struct isl_sol_for *)sol);
4719 /* Add the solution identified by the tableau and the context tableau.
4721 * See documentation of sol_add for more details.
4723 * Instead of constructing a basic map, this function calls a user
4724 * defined function with the current context as a basic set and
4725 * a list of affine expressions representing the relation between
4726 * the input and output. The space over which the affine expressions
4727 * are defined is the same as that of the domain. The number of
4728 * affine expressions in the list is equal to the number of output variables.
4730 static void sol_for_add(struct isl_sol_for *sol,
4731 struct isl_basic_set *dom, struct isl_mat *M)
4733 int i;
4734 isl_ctx *ctx;
4735 isl_local_space *ls;
4736 isl_aff *aff;
4737 isl_aff_list *list;
4739 if (sol->sol.error || !dom || !M)
4740 goto error;
4742 ctx = isl_basic_set_get_ctx(dom);
4743 ls = isl_basic_set_get_local_space(dom);
4744 list = isl_aff_list_alloc(ctx, M->n_row - 1);
4745 for (i = 1; i < M->n_row; ++i) {
4746 aff = isl_aff_alloc(isl_local_space_copy(ls));
4747 if (aff) {
4748 isl_int_set(aff->v->el[0], M->row[0][0]);
4749 isl_seq_cpy(aff->v->el + 1, M->row[i], M->n_col);
4751 aff = isl_aff_normalize(aff);
4752 list = isl_aff_list_add(list, aff);
4754 isl_local_space_free(ls);
4756 dom = isl_basic_set_finalize(dom);
4758 if (sol->fn(isl_basic_set_copy(dom), list, sol->user) < 0)
4759 goto error;
4761 isl_basic_set_free(dom);
4762 isl_mat_free(M);
4763 return;
4764 error:
4765 isl_basic_set_free(dom);
4766 isl_mat_free(M);
4767 sol->sol.error = 1;
4770 static void sol_for_add_wrap(struct isl_sol *sol,
4771 struct isl_basic_set *dom, struct isl_mat *M)
4773 sol_for_add((struct isl_sol_for *)sol, dom, M);
4776 static struct isl_sol_for *sol_for_init(struct isl_basic_map *bmap, int max,
4777 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4778 void *user),
4779 void *user)
4781 struct isl_sol_for *sol_for = NULL;
4782 isl_space *dom_dim;
4783 struct isl_basic_set *dom = NULL;
4785 sol_for = isl_calloc_type(bmap->ctx, struct isl_sol_for);
4786 if (!sol_for)
4787 goto error;
4789 dom_dim = isl_space_domain(isl_space_copy(bmap->dim));
4790 dom = isl_basic_set_universe(dom_dim);
4792 sol_for->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
4793 sol_for->sol.dec_level.callback.run = &sol_dec_level_wrap;
4794 sol_for->sol.dec_level.sol = &sol_for->sol;
4795 sol_for->fn = fn;
4796 sol_for->user = user;
4797 sol_for->sol.max = max;
4798 sol_for->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
4799 sol_for->sol.add = &sol_for_add_wrap;
4800 sol_for->sol.add_empty = NULL;
4801 sol_for->sol.free = &sol_for_free_wrap;
4803 sol_for->sol.context = isl_context_alloc(dom);
4804 if (!sol_for->sol.context)
4805 goto error;
4807 isl_basic_set_free(dom);
4808 return sol_for;
4809 error:
4810 isl_basic_set_free(dom);
4811 sol_for_free(sol_for);
4812 return NULL;
4815 static void sol_for_find_solutions(struct isl_sol_for *sol_for,
4816 struct isl_tab *tab)
4818 find_solutions_main(&sol_for->sol, tab);
4821 int isl_basic_map_foreach_lexopt(__isl_keep isl_basic_map *bmap, int max,
4822 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4823 void *user),
4824 void *user)
4826 struct isl_sol_for *sol_for = NULL;
4828 bmap = isl_basic_map_copy(bmap);
4829 bmap = isl_basic_map_detect_equalities(bmap);
4830 if (!bmap)
4831 return -1;
4833 sol_for = sol_for_init(bmap, max, fn, user);
4834 if (!sol_for)
4835 goto error;
4837 if (isl_basic_map_plain_is_empty(bmap))
4838 /* nothing */;
4839 else {
4840 struct isl_tab *tab;
4841 struct isl_context *context = sol_for->sol.context;
4842 tab = tab_for_lexmin(bmap,
4843 context->op->peek_basic_set(context), 1, max);
4844 tab = context->op->detect_nonnegative_parameters(context, tab);
4845 sol_for_find_solutions(sol_for, tab);
4846 if (sol_for->sol.error)
4847 goto error;
4850 sol_free(&sol_for->sol);
4851 isl_basic_map_free(bmap);
4852 return 0;
4853 error:
4854 sol_free(&sol_for->sol);
4855 isl_basic_map_free(bmap);
4856 return -1;
4859 int isl_basic_set_foreach_lexopt(__isl_keep isl_basic_set *bset, int max,
4860 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4861 void *user),
4862 void *user)
4864 return isl_basic_map_foreach_lexopt(bset, max, fn, user);
4867 /* Check if the given sequence of len variables starting at pos
4868 * represents a trivial (i.e., zero) solution.
4869 * The variables are assumed to be non-negative and to come in pairs,
4870 * with each pair representing a variable of unrestricted sign.
4871 * The solution is trivial if each such pair in the sequence consists
4872 * of two identical values, meaning that the variable being represented
4873 * has value zero.
4875 static int region_is_trivial(struct isl_tab *tab, int pos, int len)
4877 int i;
4879 if (len == 0)
4880 return 0;
4882 for (i = 0; i < len; i += 2) {
4883 int neg_row;
4884 int pos_row;
4886 neg_row = tab->var[pos + i].is_row ?
4887 tab->var[pos + i].index : -1;
4888 pos_row = tab->var[pos + i + 1].is_row ?
4889 tab->var[pos + i + 1].index : -1;
4891 if ((neg_row < 0 ||
4892 isl_int_is_zero(tab->mat->row[neg_row][1])) &&
4893 (pos_row < 0 ||
4894 isl_int_is_zero(tab->mat->row[pos_row][1])))
4895 continue;
4897 if (neg_row < 0 || pos_row < 0)
4898 return 0;
4899 if (isl_int_ne(tab->mat->row[neg_row][1],
4900 tab->mat->row[pos_row][1]))
4901 return 0;
4904 return 1;
4907 /* Return the index of the first trivial region or -1 if all regions
4908 * are non-trivial.
4910 static int first_trivial_region(struct isl_tab *tab,
4911 int n_region, struct isl_region *region)
4913 int i;
4915 for (i = 0; i < n_region; ++i) {
4916 if (region_is_trivial(tab, region[i].pos, region[i].len))
4917 return i;
4920 return -1;
4923 /* Check if the solution is optimal, i.e., whether the first
4924 * n_op entries are zero.
4926 static int is_optimal(__isl_keep isl_vec *sol, int n_op)
4928 int i;
4930 for (i = 0; i < n_op; ++i)
4931 if (!isl_int_is_zero(sol->el[1 + i]))
4932 return 0;
4933 return 1;
4936 /* Add constraints to "tab" that ensure that any solution is significantly
4937 * better than that represented by "sol". That is, find the first
4938 * relevant (within first n_op) non-zero coefficient and force it (along
4939 * with all previous coefficients) to be zero.
4940 * If the solution is already optimal (all relevant coefficients are zero),
4941 * then just mark the table as empty.
4943 * This function assumes that at least 2 * n_op more rows and at least
4944 * 2 * n_op more elements in the constraint array are available in the tableau.
4946 static int force_better_solution(struct isl_tab *tab,
4947 __isl_keep isl_vec *sol, int n_op)
4949 int i;
4950 isl_ctx *ctx;
4951 isl_vec *v = NULL;
4953 if (!sol)
4954 return -1;
4956 for (i = 0; i < n_op; ++i)
4957 if (!isl_int_is_zero(sol->el[1 + i]))
4958 break;
4960 if (i == n_op) {
4961 if (isl_tab_mark_empty(tab) < 0)
4962 return -1;
4963 return 0;
4966 ctx = isl_vec_get_ctx(sol);
4967 v = isl_vec_alloc(ctx, 1 + tab->n_var);
4968 if (!v)
4969 return -1;
4971 for (; i >= 0; --i) {
4972 v = isl_vec_clr(v);
4973 isl_int_set_si(v->el[1 + i], -1);
4974 if (add_lexmin_eq(tab, v->el) < 0)
4975 goto error;
4978 isl_vec_free(v);
4979 return 0;
4980 error:
4981 isl_vec_free(v);
4982 return -1;
4985 struct isl_trivial {
4986 int update;
4987 int region;
4988 int side;
4989 struct isl_tab_undo *snap;
4992 /* Return the lexicographically smallest non-trivial solution of the
4993 * given ILP problem.
4995 * All variables are assumed to be non-negative.
4997 * n_op is the number of initial coordinates to optimize.
4998 * That is, once a solution has been found, we will only continue looking
4999 * for solution that result in significantly better values for those
5000 * initial coordinates. That is, we only continue looking for solutions
5001 * that increase the number of initial zeros in this sequence.
5003 * A solution is non-trivial, if it is non-trivial on each of the
5004 * specified regions. Each region represents a sequence of pairs
5005 * of variables. A solution is non-trivial on such a region if
5006 * at least one of these pairs consists of different values, i.e.,
5007 * such that the non-negative variable represented by the pair is non-zero.
5009 * Whenever a conflict is encountered, all constraints involved are
5010 * reported to the caller through a call to "conflict".
5012 * We perform a simple branch-and-bound backtracking search.
5013 * Each level in the search represents initially trivial region that is forced
5014 * to be non-trivial.
5015 * At each level we consider n cases, where n is the length of the region.
5016 * In terms of the n/2 variables of unrestricted signs being encoded by
5017 * the region, we consider the cases
5018 * x_0 >= 1
5019 * x_0 <= -1
5020 * x_0 = 0 and x_1 >= 1
5021 * x_0 = 0 and x_1 <= -1
5022 * x_0 = 0 and x_1 = 0 and x_2 >= 1
5023 * x_0 = 0 and x_1 = 0 and x_2 <= -1
5024 * ...
5025 * The cases are considered in this order, assuming that each pair
5026 * x_i_a x_i_b represents the value x_i_b - x_i_a.
5027 * That is, x_0 >= 1 is enforced by adding the constraint
5028 * x_0_b - x_0_a >= 1
5030 __isl_give isl_vec *isl_tab_basic_set_non_trivial_lexmin(
5031 __isl_take isl_basic_set *bset, int n_op, int n_region,
5032 struct isl_region *region,
5033 int (*conflict)(int con, void *user), void *user)
5035 int i, j;
5036 int r;
5037 isl_ctx *ctx;
5038 isl_vec *v = NULL;
5039 isl_vec *sol = NULL;
5040 struct isl_tab *tab;
5041 struct isl_trivial *triv = NULL;
5042 int level, init;
5044 if (!bset)
5045 return NULL;
5047 ctx = isl_basic_set_get_ctx(bset);
5048 sol = isl_vec_alloc(ctx, 0);
5050 tab = tab_for_lexmin(bset, NULL, 0, 0);
5051 if (!tab)
5052 goto error;
5053 tab->conflict = conflict;
5054 tab->conflict_user = user;
5056 v = isl_vec_alloc(ctx, 1 + tab->n_var);
5057 triv = isl_calloc_array(ctx, struct isl_trivial, n_region);
5058 if (!v || (n_region && !triv))
5059 goto error;
5061 level = 0;
5062 init = 1;
5064 while (level >= 0) {
5065 int side, base;
5067 if (init) {
5068 tab = cut_to_integer_lexmin(tab, CUT_ONE);
5069 if (!tab)
5070 goto error;
5071 if (tab->empty)
5072 goto backtrack;
5073 r = first_trivial_region(tab, n_region, region);
5074 if (r < 0) {
5075 for (i = 0; i < level; ++i)
5076 triv[i].update = 1;
5077 isl_vec_free(sol);
5078 sol = isl_tab_get_sample_value(tab);
5079 if (!sol)
5080 goto error;
5081 if (is_optimal(sol, n_op))
5082 break;
5083 goto backtrack;
5085 if (level >= n_region)
5086 isl_die(ctx, isl_error_internal,
5087 "nesting level too deep", goto error);
5088 if (isl_tab_extend_cons(tab,
5089 2 * region[r].len + 2 * n_op) < 0)
5090 goto error;
5091 triv[level].region = r;
5092 triv[level].side = 0;
5095 r = triv[level].region;
5096 side = triv[level].side;
5097 base = 2 * (side/2);
5099 if (side >= region[r].len) {
5100 backtrack:
5101 level--;
5102 init = 0;
5103 if (level >= 0)
5104 if (isl_tab_rollback(tab, triv[level].snap) < 0)
5105 goto error;
5106 continue;
5109 if (triv[level].update) {
5110 if (force_better_solution(tab, sol, n_op) < 0)
5111 goto error;
5112 triv[level].update = 0;
5115 if (side == base && base >= 2) {
5116 for (j = base - 2; j < base; ++j) {
5117 v = isl_vec_clr(v);
5118 isl_int_set_si(v->el[1 + region[r].pos + j], 1);
5119 if (add_lexmin_eq(tab, v->el) < 0)
5120 goto error;
5124 triv[level].snap = isl_tab_snap(tab);
5125 if (isl_tab_push_basis(tab) < 0)
5126 goto error;
5128 v = isl_vec_clr(v);
5129 isl_int_set_si(v->el[0], -1);
5130 isl_int_set_si(v->el[1 + region[r].pos + side], -1);
5131 isl_int_set_si(v->el[1 + region[r].pos + (side ^ 1)], 1);
5132 tab = add_lexmin_ineq(tab, v->el);
5134 triv[level].side++;
5135 level++;
5136 init = 1;
5139 free(triv);
5140 isl_vec_free(v);
5141 isl_tab_free(tab);
5142 isl_basic_set_free(bset);
5144 return sol;
5145 error:
5146 free(triv);
5147 isl_vec_free(v);
5148 isl_tab_free(tab);
5149 isl_basic_set_free(bset);
5150 isl_vec_free(sol);
5151 return NULL;
5154 /* Wrapper for a tableau that is used for computing
5155 * the lexicographically smallest rational point of a non-negative set.
5156 * This point is represented by the sample value of "tab",
5157 * unless "tab" is empty.
5159 struct isl_tab_lexmin {
5160 isl_ctx *ctx;
5161 struct isl_tab *tab;
5164 /* Free "tl" and return NULL.
5166 __isl_null isl_tab_lexmin *isl_tab_lexmin_free(__isl_take isl_tab_lexmin *tl)
5168 if (!tl)
5169 return NULL;
5170 isl_ctx_deref(tl->ctx);
5171 isl_tab_free(tl->tab);
5172 free(tl);
5174 return NULL;
5177 /* Construct an isl_tab_lexmin for computing
5178 * the lexicographically smallest rational point in "bset",
5179 * assuming that all variables are non-negative.
5181 __isl_give isl_tab_lexmin *isl_tab_lexmin_from_basic_set(
5182 __isl_take isl_basic_set *bset)
5184 isl_ctx *ctx;
5185 isl_tab_lexmin *tl;
5187 if (!bset)
5188 return NULL;
5190 ctx = isl_basic_set_get_ctx(bset);
5191 tl = isl_calloc_type(ctx, struct isl_tab_lexmin);
5192 if (!tl)
5193 goto error;
5194 tl->ctx = ctx;
5195 isl_ctx_ref(ctx);
5196 tl->tab = tab_for_lexmin(bset, NULL, 0, 0);
5197 isl_basic_set_free(bset);
5198 if (!tl->tab)
5199 return isl_tab_lexmin_free(tl);
5200 return tl;
5201 error:
5202 isl_basic_set_free(bset);
5203 isl_tab_lexmin_free(tl);
5204 return NULL;
5207 /* Return the dimension of the set represented by "tl".
5209 int isl_tab_lexmin_dim(__isl_keep isl_tab_lexmin *tl)
5211 return tl ? tl->tab->n_var : -1;
5214 /* Add the equality with coefficients "eq" to "tl", updating the optimal
5215 * solution if needed.
5216 * The equality is added as two opposite inequality constraints.
5218 __isl_give isl_tab_lexmin *isl_tab_lexmin_add_eq(__isl_take isl_tab_lexmin *tl,
5219 isl_int *eq)
5221 unsigned n_var;
5223 if (!tl || !eq)
5224 return isl_tab_lexmin_free(tl);
5226 if (isl_tab_extend_cons(tl->tab, 2) < 0)
5227 return isl_tab_lexmin_free(tl);
5228 n_var = tl->tab->n_var;
5229 isl_seq_neg(eq, eq, 1 + n_var);
5230 tl->tab = add_lexmin_ineq(tl->tab, eq);
5231 isl_seq_neg(eq, eq, 1 + n_var);
5232 tl->tab = add_lexmin_ineq(tl->tab, eq);
5234 if (!tl->tab)
5235 return isl_tab_lexmin_free(tl);
5237 return tl;
5240 /* Return the lexicographically smallest rational point in the basic set
5241 * from which "tl" was constructed.
5242 * If the original input was empty, then return a zero-length vector.
5244 __isl_give isl_vec *isl_tab_lexmin_get_solution(__isl_keep isl_tab_lexmin *tl)
5246 if (!tl)
5247 return NULL;
5248 if (tl->tab->empty)
5249 return isl_vec_alloc(tl->ctx, 0);
5250 else
5251 return isl_tab_get_sample_value(tl->tab);
5254 /* Return the lexicographically smallest rational point in "bset",
5255 * assuming that all variables are non-negative.
5256 * If "bset" is empty, then return a zero-length vector.
5258 __isl_give isl_vec *isl_tab_basic_set_non_neg_lexmin(
5259 __isl_take isl_basic_set *bset)
5261 isl_tab_lexmin *tl;
5262 isl_vec *sol;
5264 tl = isl_tab_lexmin_from_basic_set(bset);
5265 sol = isl_tab_lexmin_get_solution(tl);
5266 isl_tab_lexmin_free(tl);
5267 return sol;
5270 struct isl_sol_pma {
5271 struct isl_sol sol;
5272 isl_pw_multi_aff *pma;
5273 isl_set *empty;
5276 static void sol_pma_free(struct isl_sol_pma *sol_pma)
5278 if (!sol_pma)
5279 return;
5280 if (sol_pma->sol.context)
5281 sol_pma->sol.context->op->free(sol_pma->sol.context);
5282 isl_pw_multi_aff_free(sol_pma->pma);
5283 isl_set_free(sol_pma->empty);
5284 free(sol_pma);
5287 /* This function is called for parts of the context where there is
5288 * no solution, with "bset" corresponding to the context tableau.
5289 * Simply add the basic set to the set "empty".
5291 static void sol_pma_add_empty(struct isl_sol_pma *sol,
5292 __isl_take isl_basic_set *bset)
5294 if (!bset || !sol->empty)
5295 goto error;
5297 sol->empty = isl_set_grow(sol->empty, 1);
5298 bset = isl_basic_set_simplify(bset);
5299 bset = isl_basic_set_finalize(bset);
5300 sol->empty = isl_set_add_basic_set(sol->empty, bset);
5301 if (!sol->empty)
5302 sol->sol.error = 1;
5303 return;
5304 error:
5305 isl_basic_set_free(bset);
5306 sol->sol.error = 1;
5309 /* Return the equality constraint in "bset" that defines existentially
5310 * quantified variable "pos" in terms of earlier dimensions.
5311 * The equality constraint is guaranteed to exist by the caller.
5312 * If "c" is not NULL, then it is the result of a previous call
5313 * to this function for the same variable, so simply return the input "c"
5314 * in that case.
5316 static __isl_give isl_constraint *get_equality(__isl_keep isl_basic_set *bset,
5317 int pos, __isl_take isl_constraint *c)
5319 int r;
5321 if (c)
5322 return c;
5323 r = isl_basic_set_has_defining_equality(bset, isl_dim_div, pos, &c);
5324 if (r < 0)
5325 return NULL;
5326 if (!r)
5327 isl_die(isl_basic_set_get_ctx(bset), isl_error_internal,
5328 "unexpected missing equality", return NULL);
5329 return c;
5332 /* Given a set "dom", of which only the first "n_known" existentially
5333 * quantified variables have a known explicit representation, and
5334 * a matrix "M", the rows of which are defined in terms of the dimensions
5335 * of "dom", eliminate all references to the existentially quantified
5336 * variables without a known explicit representation from "M"
5337 * by exploiting the equality constraints of "dom".
5339 * In particular, for each of those existentially quantified variables,
5340 * if there are non-zero entries in the corresponding column of "M",
5341 * then look for an equality constraint of "dom" that defines that variable
5342 * in terms of earlier variables and use it to clear the entries.
5344 * In particular, if the equality is of the form
5346 * f() + a alpha = 0
5348 * while the matrix entry is b/d (with d the global denominator of "M"),
5349 * then first scale the matrix such that the entry becomes b'/d' with
5350 * b' a multiple of a. Do this by multiplying the entire matrix
5351 * by abs(a/gcd(a,b)). Then subtract the equality multiplied by b'/a
5352 * from the row of "M" to clear the entry.
5354 static __isl_give isl_mat *eliminate_unknown_divs(__isl_take isl_mat *M,
5355 __isl_keep isl_basic_set *dom, int n_known)
5357 int i, j, n_div, off;
5358 isl_int t;
5359 isl_constraint *c = NULL;
5361 if (!M)
5362 return NULL;
5364 n_div = isl_basic_set_dim(dom, isl_dim_div);
5365 off = M->n_col - n_div;
5367 isl_int_init(t);
5368 for (i = n_div - 1; i >= n_known; --i) {
5369 for (j = 1; j < M->n_row; ++j) {
5370 if (isl_int_is_zero(M->row[j][off + i]))
5371 continue;
5372 c = get_equality(dom, i, c);
5373 if (!c)
5374 goto error;
5375 isl_int_gcd(t, M->row[j][off + i], c->v->el[off + i]);
5376 isl_int_divexact(t, c->v->el[off + i], t);
5377 isl_int_abs(t, t);
5378 M = isl_mat_scale(M, t);
5379 M = isl_mat_cow(M);
5380 if (!M)
5381 goto error;
5382 isl_int_divexact(t,
5383 M->row[j][off + i], c->v->el[off + i]);
5384 isl_seq_submul(M->row[j], t, c->v->el, M->n_col);
5386 c = isl_constraint_free(c);
5388 isl_int_clear(t);
5390 return M;
5391 error:
5392 isl_int_clear(t);
5393 isl_constraint_free(c);
5394 isl_mat_free(M);
5395 return NULL;
5398 /* Return the index of the last known div of "bset" after "start" and
5399 * up to (but not including) "end".
5400 * Return "start" if there is no such known div.
5402 static int last_known_div_after(__isl_keep isl_basic_set *bset,
5403 int start, int end)
5405 for (end = end - 1; end > start; --end) {
5406 if (isl_basic_set_div_is_known(bset, end))
5407 return end;
5410 return start;
5413 /* Set the affine expressions in "ma" according to the rows in "M", which
5414 * are defined over the local space "ls".
5415 * The matrix "M" may have extra (zero) columns beyond the number
5416 * of variables in "ls".
5418 static __isl_give isl_multi_aff *set_from_affine_matrix(
5419 __isl_take isl_multi_aff *ma, __isl_take isl_local_space *ls,
5420 __isl_take isl_mat *M)
5422 int i, dim;
5423 isl_aff *aff;
5425 if (!ma || !ls || !M)
5426 goto error;
5428 dim = isl_local_space_dim(ls, isl_dim_all);
5429 for (i = 1; i < M->n_row; ++i) {
5430 aff = isl_aff_alloc(isl_local_space_copy(ls));
5431 if (aff) {
5432 isl_int_set(aff->v->el[0], M->row[0][0]);
5433 isl_seq_cpy(aff->v->el + 1, M->row[i], 1 + dim);
5435 aff = isl_aff_normalize(aff);
5436 ma = isl_multi_aff_set_aff(ma, i - 1, aff);
5438 isl_local_space_free(ls);
5439 isl_mat_free(M);
5441 return ma;
5442 error:
5443 isl_local_space_free(ls);
5444 isl_mat_free(M);
5445 isl_multi_aff_free(ma);
5446 return NULL;
5449 /* Given a basic map "dom" that represents the context and an affine
5450 * matrix "M" that maps the dimensions of the context to the
5451 * output variables, construct an isl_pw_multi_aff with a single
5452 * cell corresponding to "dom" and affine expressions copied from "M".
5454 * Note that the description of the initial context may have involved
5455 * existentially quantified variables, in which case they also appear
5456 * in "dom". These need to be removed before creating the affine
5457 * expression because an affine expression cannot be defined in terms
5458 * of existentially quantified variables without a known representation.
5459 * In particular, they are first moved to the end in both "dom" and "M" and
5460 * then ignored in "M". In principle, the final columns of "M"
5461 * (i.e., those that will be ignored) should be zero at this stage
5462 * because align_context_divs adds the existentially quantified
5463 * variables of the context to the main tableau without any constraints.
5464 * The computed minimal value can therefore not depend on these variables.
5465 * However, additional integer divisions that get added for parametric cuts
5466 * get added to the end and they may happen to be equal to some affine
5467 * expression involving the original existentially quantified variables.
5468 * These equality constraints are then propagated to the main tableau
5469 * such that the computed minimum can in fact depend on those existentially
5470 * quantified variables. This dependence can however be removed again
5471 * by exploiting the equality constraints in "dom".
5472 * eliminate_unknown_divs takes care of this.
5474 static void sol_pma_add(struct isl_sol_pma *sol,
5475 __isl_take isl_basic_set *dom, __isl_take isl_mat *M)
5477 isl_local_space *ls;
5478 isl_multi_aff *maff;
5479 isl_pw_multi_aff *pma;
5480 int n_div, n_known, end, off;
5482 n_div = isl_basic_set_dim(dom, isl_dim_div);
5483 off = M->n_col - n_div;
5484 end = n_div;
5485 for (n_known = 0; n_known < end; ++n_known) {
5486 if (isl_basic_set_div_is_known(dom, n_known))
5487 continue;
5488 end = last_known_div_after(dom, n_known, end);
5489 if (end == n_known)
5490 break;
5491 isl_basic_set_swap_div(dom, n_known, end);
5492 M = isl_mat_swap_cols(M, off + n_known, off + end);
5494 dom = isl_basic_set_gauss(dom, NULL);
5495 if (n_known < n_div)
5496 M = eliminate_unknown_divs(M, dom, n_known);
5498 maff = isl_multi_aff_alloc(isl_pw_multi_aff_get_space(sol->pma));
5499 ls = isl_basic_set_get_local_space(dom);
5500 ls = isl_local_space_drop_dims(ls, isl_dim_div,
5501 n_known, n_div - n_known);
5502 maff = set_from_affine_matrix(maff, ls, M);
5503 dom = isl_basic_set_simplify(dom);
5504 dom = isl_basic_set_finalize(dom);
5505 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(dom), maff);
5506 sol->pma = isl_pw_multi_aff_add_disjoint(sol->pma, pma);
5507 if (!sol->pma)
5508 sol->sol.error = 1;
5511 static void sol_pma_free_wrap(struct isl_sol *sol)
5513 sol_pma_free((struct isl_sol_pma *)sol);
5516 static void sol_pma_add_empty_wrap(struct isl_sol *sol,
5517 __isl_take isl_basic_set *bset)
5519 sol_pma_add_empty((struct isl_sol_pma *)sol, bset);
5522 static void sol_pma_add_wrap(struct isl_sol *sol,
5523 __isl_take isl_basic_set *dom, __isl_take isl_mat *M)
5525 sol_pma_add((struct isl_sol_pma *)sol, dom, M);
5528 /* Construct an isl_sol_pma structure for accumulating the solution.
5529 * If track_empty is set, then we also keep track of the parts
5530 * of the context where there is no solution.
5531 * If max is set, then we are solving a maximization, rather than
5532 * a minimization problem, which means that the variables in the
5533 * tableau have value "M - x" rather than "M + x".
5535 static struct isl_sol *sol_pma_init(__isl_keep isl_basic_map *bmap,
5536 __isl_take isl_basic_set *dom, int track_empty, int max)
5538 struct isl_sol_pma *sol_pma = NULL;
5540 if (!bmap)
5541 goto error;
5543 sol_pma = isl_calloc_type(bmap->ctx, struct isl_sol_pma);
5544 if (!sol_pma)
5545 goto error;
5547 sol_pma->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
5548 sol_pma->sol.dec_level.callback.run = &sol_dec_level_wrap;
5549 sol_pma->sol.dec_level.sol = &sol_pma->sol;
5550 sol_pma->sol.max = max;
5551 sol_pma->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
5552 sol_pma->sol.add = &sol_pma_add_wrap;
5553 sol_pma->sol.add_empty = track_empty ? &sol_pma_add_empty_wrap : NULL;
5554 sol_pma->sol.free = &sol_pma_free_wrap;
5555 sol_pma->pma = isl_pw_multi_aff_empty(isl_basic_map_get_space(bmap));
5556 if (!sol_pma->pma)
5557 goto error;
5559 sol_pma->sol.context = isl_context_alloc(dom);
5560 if (!sol_pma->sol.context)
5561 goto error;
5563 if (track_empty) {
5564 sol_pma->empty = isl_set_alloc_space(isl_basic_set_get_space(dom),
5565 1, ISL_SET_DISJOINT);
5566 if (!sol_pma->empty)
5567 goto error;
5570 isl_basic_set_free(dom);
5571 return &sol_pma->sol;
5572 error:
5573 isl_basic_set_free(dom);
5574 sol_pma_free(sol_pma);
5575 return NULL;
5578 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
5579 * some obvious symmetries.
5581 * We call basic_map_partial_lexopt_base_sol and extract the results.
5583 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_base_pw_multi_aff(
5584 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5585 __isl_give isl_set **empty, int max)
5587 isl_pw_multi_aff *result = NULL;
5588 struct isl_sol *sol;
5589 struct isl_sol_pma *sol_pma;
5591 sol = basic_map_partial_lexopt_base_sol(bmap, dom, empty, max,
5592 &sol_pma_init);
5593 if (!sol)
5594 return NULL;
5595 sol_pma = (struct isl_sol_pma *) sol;
5597 result = isl_pw_multi_aff_copy(sol_pma->pma);
5598 if (empty)
5599 *empty = isl_set_copy(sol_pma->empty);
5600 sol_free(&sol_pma->sol);
5601 return result;
5604 /* Given that the last input variable of "maff" represents the minimum
5605 * of some bounds, check whether we need to plug in the expression
5606 * of the minimum.
5608 * In particular, check if the last input variable appears in any
5609 * of the expressions in "maff".
5611 static int need_substitution(__isl_keep isl_multi_aff *maff)
5613 int i;
5614 unsigned pos;
5616 pos = isl_multi_aff_dim(maff, isl_dim_in) - 1;
5618 for (i = 0; i < maff->n; ++i)
5619 if (isl_aff_involves_dims(maff->p[i], isl_dim_in, pos, 1))
5620 return 1;
5622 return 0;
5625 /* Given a set of upper bounds on the last "input" variable m,
5626 * construct a piecewise affine expression that selects
5627 * the minimal upper bound to m, i.e.,
5628 * divide the space into cells where one
5629 * of the upper bounds is smaller than all the others and select
5630 * this upper bound on that cell.
5632 * In particular, if there are n bounds b_i, then the result
5633 * consists of n cell, each one of the form
5635 * b_i <= b_j for j > i
5636 * b_i < b_j for j < i
5638 * The affine expression on this cell is
5640 * b_i
5642 static __isl_give isl_pw_aff *set_minimum_pa(__isl_take isl_space *space,
5643 __isl_take isl_mat *var)
5645 int i;
5646 isl_aff *aff = NULL;
5647 isl_basic_set *bset = NULL;
5648 isl_pw_aff *paff = NULL;
5649 isl_space *pw_space;
5650 isl_local_space *ls = NULL;
5652 if (!space || !var)
5653 goto error;
5655 ls = isl_local_space_from_space(isl_space_copy(space));
5656 pw_space = isl_space_copy(space);
5657 pw_space = isl_space_from_domain(pw_space);
5658 pw_space = isl_space_add_dims(pw_space, isl_dim_out, 1);
5659 paff = isl_pw_aff_alloc_size(pw_space, var->n_row);
5661 for (i = 0; i < var->n_row; ++i) {
5662 isl_pw_aff *paff_i;
5664 aff = isl_aff_alloc(isl_local_space_copy(ls));
5665 bset = isl_basic_set_alloc_space(isl_space_copy(space), 0,
5666 0, var->n_row - 1);
5667 if (!aff || !bset)
5668 goto error;
5669 isl_int_set_si(aff->v->el[0], 1);
5670 isl_seq_cpy(aff->v->el + 1, var->row[i], var->n_col);
5671 isl_int_set_si(aff->v->el[1 + var->n_col], 0);
5672 bset = select_minimum(bset, var, i);
5673 paff_i = isl_pw_aff_alloc(isl_set_from_basic_set(bset), aff);
5674 paff = isl_pw_aff_add_disjoint(paff, paff_i);
5677 isl_local_space_free(ls);
5678 isl_space_free(space);
5679 isl_mat_free(var);
5680 return paff;
5681 error:
5682 isl_aff_free(aff);
5683 isl_basic_set_free(bset);
5684 isl_pw_aff_free(paff);
5685 isl_local_space_free(ls);
5686 isl_space_free(space);
5687 isl_mat_free(var);
5688 return NULL;
5691 /* Given a piecewise multi-affine expression of which the last input variable
5692 * is the minimum of the bounds in "cst", plug in the value of the minimum.
5693 * This minimum expression is given in "min_expr_pa".
5694 * The set "min_expr" contains the same information, but in the form of a set.
5695 * The variable is subsequently projected out.
5697 * The implementation is similar to those of "split" and "split_domain".
5698 * If the variable appears in a given expression, then minimum expression
5699 * is plugged in. Otherwise, if the variable appears in the constraints
5700 * and a split is required, then the domain is split. Otherwise, no split
5701 * is performed.
5703 static __isl_give isl_pw_multi_aff *split_domain_pma(
5704 __isl_take isl_pw_multi_aff *opt, __isl_take isl_pw_aff *min_expr_pa,
5705 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
5707 int n_in;
5708 int i;
5709 isl_space *space;
5710 isl_pw_multi_aff *res;
5712 if (!opt || !min_expr || !cst)
5713 goto error;
5715 n_in = isl_pw_multi_aff_dim(opt, isl_dim_in);
5716 space = isl_pw_multi_aff_get_space(opt);
5717 space = isl_space_drop_dims(space, isl_dim_in, n_in - 1, 1);
5718 res = isl_pw_multi_aff_empty(space);
5720 for (i = 0; i < opt->n; ++i) {
5721 isl_pw_multi_aff *pma;
5723 pma = isl_pw_multi_aff_alloc(isl_set_copy(opt->p[i].set),
5724 isl_multi_aff_copy(opt->p[i].maff));
5725 if (need_substitution(opt->p[i].maff))
5726 pma = isl_pw_multi_aff_substitute(pma,
5727 isl_dim_in, n_in - 1, min_expr_pa);
5728 else if (need_split_set(opt->p[i].set, cst))
5729 pma = isl_pw_multi_aff_intersect_domain(pma,
5730 isl_set_copy(min_expr));
5731 pma = isl_pw_multi_aff_project_out(pma,
5732 isl_dim_in, n_in - 1, 1);
5734 res = isl_pw_multi_aff_add_disjoint(res, pma);
5737 isl_pw_multi_aff_free(opt);
5738 isl_pw_aff_free(min_expr_pa);
5739 isl_set_free(min_expr);
5740 isl_mat_free(cst);
5741 return res;
5742 error:
5743 isl_pw_multi_aff_free(opt);
5744 isl_pw_aff_free(min_expr_pa);
5745 isl_set_free(min_expr);
5746 isl_mat_free(cst);
5747 return NULL;
5750 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_pw_multi_aff(
5751 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5752 __isl_give isl_set **empty, int max);
5754 /* This function is called from basic_map_partial_lexopt_symm.
5755 * The last variable of "bmap" and "dom" corresponds to the minimum
5756 * of the bounds in "cst". "map_space" is the space of the original
5757 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
5758 * is the space of the original domain.
5760 * We recursively call basic_map_partial_lexopt and then plug in
5761 * the definition of the minimum in the result.
5763 static __isl_give isl_pw_multi_aff *
5764 basic_map_partial_lexopt_symm_core_pw_multi_aff(
5765 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5766 __isl_give isl_set **empty, int max, __isl_take isl_mat *cst,
5767 __isl_take isl_space *map_space, __isl_take isl_space *set_space)
5769 isl_pw_multi_aff *opt;
5770 isl_pw_aff *min_expr_pa;
5771 isl_set *min_expr;
5773 min_expr = set_minimum(isl_basic_set_get_space(dom), isl_mat_copy(cst));
5774 min_expr_pa = set_minimum_pa(isl_basic_set_get_space(dom),
5775 isl_mat_copy(cst));
5777 opt = basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, max);
5779 if (empty) {
5780 *empty = split(*empty,
5781 isl_set_copy(min_expr), isl_mat_copy(cst));
5782 *empty = isl_set_reset_space(*empty, set_space);
5785 opt = split_domain_pma(opt, min_expr_pa, min_expr, cst);
5786 opt = isl_pw_multi_aff_reset_space(opt, map_space);
5788 return opt;
5791 #undef TYPE
5792 #define TYPE isl_pw_multi_aff
5793 #undef SUFFIX
5794 #define SUFFIX _pw_multi_aff
5795 #include "isl_tab_lexopt_templ.c"