isl_tab_add_div: extract out isl_tab_insert_div
[isl.git] / isl_tab_pip.c
bloba6b1010f8cd79f0a9c40a30f48eb1e716f68425d
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 __isl_null struct isl_context *(*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_insert_div(tab, tab->n_var, 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 __isl_null struct isl_context *context_lex_free(
2578 struct isl_context *context)
2580 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2581 isl_tab_free(clex->tab);
2582 free(clex);
2584 return NULL;
2587 struct isl_context_op isl_context_lex_op = {
2588 context_lex_detect_nonnegative_parameters,
2589 context_lex_peek_basic_set,
2590 context_lex_peek_tab,
2591 context_lex_add_eq,
2592 context_lex_add_ineq,
2593 context_lex_ineq_sign,
2594 context_lex_test_ineq,
2595 context_lex_get_div,
2596 context_lex_add_div,
2597 context_lex_detect_equalities,
2598 context_lex_best_split,
2599 context_lex_is_empty,
2600 context_lex_is_ok,
2601 context_lex_save,
2602 context_lex_restore,
2603 context_lex_discard,
2604 context_lex_invalidate,
2605 context_lex_free,
2608 static struct isl_tab *context_tab_for_lexmin(struct isl_basic_set *bset)
2610 struct isl_tab *tab;
2612 if (!bset)
2613 return NULL;
2614 tab = tab_for_lexmin((struct isl_basic_map *)bset, NULL, 1, 0);
2615 if (!tab)
2616 goto error;
2617 if (isl_tab_track_bset(tab, bset) < 0)
2618 goto error;
2619 tab = isl_tab_init_samples(tab);
2620 return tab;
2621 error:
2622 isl_basic_set_free(bset);
2623 return NULL;
2626 static struct isl_context *isl_context_lex_alloc(struct isl_basic_set *dom)
2628 struct isl_context_lex *clex;
2630 if (!dom)
2631 return NULL;
2633 clex = isl_alloc_type(dom->ctx, struct isl_context_lex);
2634 if (!clex)
2635 return NULL;
2637 clex->context.op = &isl_context_lex_op;
2639 clex->tab = context_tab_for_lexmin(isl_basic_set_copy(dom));
2640 if (restore_lexmin(clex->tab) < 0)
2641 goto error;
2642 clex->tab = check_integer_feasible(clex->tab);
2643 if (!clex->tab)
2644 goto error;
2646 return &clex->context;
2647 error:
2648 clex->context.op->free(&clex->context);
2649 return NULL;
2652 /* Representation of the context when using generalized basis reduction.
2654 * "shifted" contains the offsets of the unit hypercubes that lie inside the
2655 * context. Any rational point in "shifted" can therefore be rounded
2656 * up to an integer point in the context.
2657 * If the context is constrained by any equality, then "shifted" is not used
2658 * as it would be empty.
2660 struct isl_context_gbr {
2661 struct isl_context context;
2662 struct isl_tab *tab;
2663 struct isl_tab *shifted;
2664 struct isl_tab *cone;
2667 static struct isl_tab *context_gbr_detect_nonnegative_parameters(
2668 struct isl_context *context, struct isl_tab *tab)
2670 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2671 if (!tab)
2672 return NULL;
2673 return tab_detect_nonnegative_parameters(tab, cgbr->tab);
2676 static struct isl_basic_set *context_gbr_peek_basic_set(
2677 struct isl_context *context)
2679 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2680 if (!cgbr->tab)
2681 return NULL;
2682 return isl_tab_peek_bset(cgbr->tab);
2685 static struct isl_tab *context_gbr_peek_tab(struct isl_context *context)
2687 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2688 return cgbr->tab;
2691 /* Initialize the "shifted" tableau of the context, which
2692 * contains the constraints of the original tableau shifted
2693 * by the sum of all negative coefficients. This ensures
2694 * that any rational point in the shifted tableau can
2695 * be rounded up to yield an integer point in the original tableau.
2697 static void gbr_init_shifted(struct isl_context_gbr *cgbr)
2699 int i, j;
2700 struct isl_vec *cst;
2701 struct isl_basic_set *bset = isl_tab_peek_bset(cgbr->tab);
2702 unsigned dim = isl_basic_set_total_dim(bset);
2704 cst = isl_vec_alloc(cgbr->tab->mat->ctx, bset->n_ineq);
2705 if (!cst)
2706 return;
2708 for (i = 0; i < bset->n_ineq; ++i) {
2709 isl_int_set(cst->el[i], bset->ineq[i][0]);
2710 for (j = 0; j < dim; ++j) {
2711 if (!isl_int_is_neg(bset->ineq[i][1 + j]))
2712 continue;
2713 isl_int_add(bset->ineq[i][0], bset->ineq[i][0],
2714 bset->ineq[i][1 + j]);
2718 cgbr->shifted = isl_tab_from_basic_set(bset, 0);
2720 for (i = 0; i < bset->n_ineq; ++i)
2721 isl_int_set(bset->ineq[i][0], cst->el[i]);
2723 isl_vec_free(cst);
2726 /* Check if the shifted tableau is non-empty, and if so
2727 * use the sample point to construct an integer point
2728 * of the context tableau.
2730 static struct isl_vec *gbr_get_shifted_sample(struct isl_context_gbr *cgbr)
2732 struct isl_vec *sample;
2734 if (!cgbr->shifted)
2735 gbr_init_shifted(cgbr);
2736 if (!cgbr->shifted)
2737 return NULL;
2738 if (cgbr->shifted->empty)
2739 return isl_vec_alloc(cgbr->tab->mat->ctx, 0);
2741 sample = isl_tab_get_sample_value(cgbr->shifted);
2742 sample = isl_vec_ceil(sample);
2744 return sample;
2747 static struct isl_basic_set *drop_constant_terms(struct isl_basic_set *bset)
2749 int i;
2751 if (!bset)
2752 return NULL;
2754 for (i = 0; i < bset->n_eq; ++i)
2755 isl_int_set_si(bset->eq[i][0], 0);
2757 for (i = 0; i < bset->n_ineq; ++i)
2758 isl_int_set_si(bset->ineq[i][0], 0);
2760 return bset;
2763 static int use_shifted(struct isl_context_gbr *cgbr)
2765 if (!cgbr->tab)
2766 return 0;
2767 return cgbr->tab->bmap->n_eq == 0 && cgbr->tab->bmap->n_div == 0;
2770 static struct isl_vec *gbr_get_sample(struct isl_context_gbr *cgbr)
2772 struct isl_basic_set *bset;
2773 struct isl_basic_set *cone;
2775 if (isl_tab_sample_is_integer(cgbr->tab))
2776 return isl_tab_get_sample_value(cgbr->tab);
2778 if (use_shifted(cgbr)) {
2779 struct isl_vec *sample;
2781 sample = gbr_get_shifted_sample(cgbr);
2782 if (!sample || sample->size > 0)
2783 return sample;
2785 isl_vec_free(sample);
2788 if (!cgbr->cone) {
2789 bset = isl_tab_peek_bset(cgbr->tab);
2790 cgbr->cone = isl_tab_from_recession_cone(bset, 0);
2791 if (!cgbr->cone)
2792 return NULL;
2793 if (isl_tab_track_bset(cgbr->cone,
2794 isl_basic_set_copy(bset)) < 0)
2795 return NULL;
2797 if (isl_tab_detect_implicit_equalities(cgbr->cone) < 0)
2798 return NULL;
2800 if (cgbr->cone->n_dead == cgbr->cone->n_col) {
2801 struct isl_vec *sample;
2802 struct isl_tab_undo *snap;
2804 if (cgbr->tab->basis) {
2805 if (cgbr->tab->basis->n_col != 1 + cgbr->tab->n_var) {
2806 isl_mat_free(cgbr->tab->basis);
2807 cgbr->tab->basis = NULL;
2809 cgbr->tab->n_zero = 0;
2810 cgbr->tab->n_unbounded = 0;
2813 snap = isl_tab_snap(cgbr->tab);
2815 sample = isl_tab_sample(cgbr->tab);
2817 if (!sample || isl_tab_rollback(cgbr->tab, snap) < 0) {
2818 isl_vec_free(sample);
2819 return NULL;
2822 return sample;
2825 cone = isl_basic_set_dup(isl_tab_peek_bset(cgbr->cone));
2826 cone = drop_constant_terms(cone);
2827 cone = isl_basic_set_update_from_tab(cone, cgbr->cone);
2828 cone = isl_basic_set_underlying_set(cone);
2829 cone = isl_basic_set_gauss(cone, NULL);
2831 bset = isl_basic_set_dup(isl_tab_peek_bset(cgbr->tab));
2832 bset = isl_basic_set_update_from_tab(bset, cgbr->tab);
2833 bset = isl_basic_set_underlying_set(bset);
2834 bset = isl_basic_set_gauss(bset, NULL);
2836 return isl_basic_set_sample_with_cone(bset, cone);
2839 static void check_gbr_integer_feasible(struct isl_context_gbr *cgbr)
2841 struct isl_vec *sample;
2843 if (!cgbr->tab)
2844 return;
2846 if (cgbr->tab->empty)
2847 return;
2849 sample = gbr_get_sample(cgbr);
2850 if (!sample)
2851 goto error;
2853 if (sample->size == 0) {
2854 isl_vec_free(sample);
2855 if (isl_tab_mark_empty(cgbr->tab) < 0)
2856 goto error;
2857 return;
2860 if (isl_tab_add_sample(cgbr->tab, sample) < 0)
2861 goto error;
2863 return;
2864 error:
2865 isl_tab_free(cgbr->tab);
2866 cgbr->tab = NULL;
2869 static struct isl_tab *add_gbr_eq(struct isl_tab *tab, isl_int *eq)
2871 if (!tab)
2872 return NULL;
2874 if (isl_tab_extend_cons(tab, 2) < 0)
2875 goto error;
2877 if (isl_tab_add_eq(tab, eq) < 0)
2878 goto error;
2880 return tab;
2881 error:
2882 isl_tab_free(tab);
2883 return NULL;
2886 /* Add the equality described by "eq" to the context.
2887 * If "check" is set, then we check if the context is empty after
2888 * adding the equality.
2889 * If "update" is set, then we check if the samples are still valid.
2891 * We do not explicitly add shifted copies of the equality to
2892 * cgbr->shifted since they would conflict with each other.
2893 * Instead, we directly mark cgbr->shifted empty.
2895 static void context_gbr_add_eq(struct isl_context *context, isl_int *eq,
2896 int check, int update)
2898 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2900 cgbr->tab = add_gbr_eq(cgbr->tab, eq);
2902 if (cgbr->shifted && !cgbr->shifted->empty && use_shifted(cgbr)) {
2903 if (isl_tab_mark_empty(cgbr->shifted) < 0)
2904 goto error;
2907 if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
2908 if (isl_tab_extend_cons(cgbr->cone, 2) < 0)
2909 goto error;
2910 if (isl_tab_add_eq(cgbr->cone, eq) < 0)
2911 goto error;
2914 if (check) {
2915 int v = tab_has_valid_sample(cgbr->tab, eq, 1);
2916 if (v < 0)
2917 goto error;
2918 if (!v)
2919 check_gbr_integer_feasible(cgbr);
2921 if (update)
2922 cgbr->tab = check_samples(cgbr->tab, eq, 1);
2923 return;
2924 error:
2925 isl_tab_free(cgbr->tab);
2926 cgbr->tab = NULL;
2929 static void add_gbr_ineq(struct isl_context_gbr *cgbr, isl_int *ineq)
2931 if (!cgbr->tab)
2932 return;
2934 if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
2935 goto error;
2937 if (isl_tab_add_ineq(cgbr->tab, ineq) < 0)
2938 goto error;
2940 if (cgbr->shifted && !cgbr->shifted->empty && use_shifted(cgbr)) {
2941 int i;
2942 unsigned dim;
2943 dim = isl_basic_map_total_dim(cgbr->tab->bmap);
2945 if (isl_tab_extend_cons(cgbr->shifted, 1) < 0)
2946 goto error;
2948 for (i = 0; i < dim; ++i) {
2949 if (!isl_int_is_neg(ineq[1 + i]))
2950 continue;
2951 isl_int_add(ineq[0], ineq[0], ineq[1 + i]);
2954 if (isl_tab_add_ineq(cgbr->shifted, ineq) < 0)
2955 goto error;
2957 for (i = 0; i < dim; ++i) {
2958 if (!isl_int_is_neg(ineq[1 + i]))
2959 continue;
2960 isl_int_sub(ineq[0], ineq[0], ineq[1 + i]);
2964 if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
2965 if (isl_tab_extend_cons(cgbr->cone, 1) < 0)
2966 goto error;
2967 if (isl_tab_add_ineq(cgbr->cone, ineq) < 0)
2968 goto error;
2971 return;
2972 error:
2973 isl_tab_free(cgbr->tab);
2974 cgbr->tab = NULL;
2977 static void context_gbr_add_ineq(struct isl_context *context, isl_int *ineq,
2978 int check, int update)
2980 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2982 add_gbr_ineq(cgbr, ineq);
2983 if (!cgbr->tab)
2984 return;
2986 if (check) {
2987 int v = tab_has_valid_sample(cgbr->tab, ineq, 0);
2988 if (v < 0)
2989 goto error;
2990 if (!v)
2991 check_gbr_integer_feasible(cgbr);
2993 if (update)
2994 cgbr->tab = check_samples(cgbr->tab, ineq, 0);
2995 return;
2996 error:
2997 isl_tab_free(cgbr->tab);
2998 cgbr->tab = NULL;
3001 static int context_gbr_add_ineq_wrap(void *user, isl_int *ineq)
3003 struct isl_context *context = (struct isl_context *)user;
3004 context_gbr_add_ineq(context, ineq, 0, 0);
3005 return context->op->is_ok(context) ? 0 : -1;
3008 static enum isl_tab_row_sign context_gbr_ineq_sign(struct isl_context *context,
3009 isl_int *ineq, int strict)
3011 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3012 return tab_ineq_sign(cgbr->tab, ineq, strict);
3015 /* Check whether "ineq" can be added to the tableau without rendering
3016 * it infeasible.
3018 static int context_gbr_test_ineq(struct isl_context *context, isl_int *ineq)
3020 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3021 struct isl_tab_undo *snap;
3022 struct isl_tab_undo *shifted_snap = NULL;
3023 struct isl_tab_undo *cone_snap = NULL;
3024 int feasible;
3026 if (!cgbr->tab)
3027 return -1;
3029 if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
3030 return -1;
3032 snap = isl_tab_snap(cgbr->tab);
3033 if (cgbr->shifted)
3034 shifted_snap = isl_tab_snap(cgbr->shifted);
3035 if (cgbr->cone)
3036 cone_snap = isl_tab_snap(cgbr->cone);
3037 add_gbr_ineq(cgbr, ineq);
3038 check_gbr_integer_feasible(cgbr);
3039 if (!cgbr->tab)
3040 return -1;
3041 feasible = !cgbr->tab->empty;
3042 if (isl_tab_rollback(cgbr->tab, snap) < 0)
3043 return -1;
3044 if (shifted_snap) {
3045 if (isl_tab_rollback(cgbr->shifted, shifted_snap))
3046 return -1;
3047 } else if (cgbr->shifted) {
3048 isl_tab_free(cgbr->shifted);
3049 cgbr->shifted = NULL;
3051 if (cone_snap) {
3052 if (isl_tab_rollback(cgbr->cone, cone_snap))
3053 return -1;
3054 } else if (cgbr->cone) {
3055 isl_tab_free(cgbr->cone);
3056 cgbr->cone = NULL;
3059 return feasible;
3062 /* Return the column of the last of the variables associated to
3063 * a column that has a non-zero coefficient.
3064 * This function is called in a context where only coefficients
3065 * of parameters or divs can be non-zero.
3067 static int last_non_zero_var_col(struct isl_tab *tab, isl_int *p)
3069 int i;
3070 int col;
3072 if (tab->n_var == 0)
3073 return -1;
3075 for (i = tab->n_var - 1; i >= 0; --i) {
3076 if (i >= tab->n_param && i < tab->n_var - tab->n_div)
3077 continue;
3078 if (tab->var[i].is_row)
3079 continue;
3080 col = tab->var[i].index;
3081 if (!isl_int_is_zero(p[col]))
3082 return col;
3085 return -1;
3088 /* Look through all the recently added equalities in the context
3089 * to see if we can propagate any of them to the main tableau.
3091 * The newly added equalities in the context are encoded as pairs
3092 * of inequalities starting at inequality "first".
3094 * We tentatively add each of these equalities to the main tableau
3095 * and if this happens to result in a row with a final coefficient
3096 * that is one or negative one, we use it to kill a column
3097 * in the main tableau. Otherwise, we discard the tentatively
3098 * added row.
3099 * This tentative addition of equality constraints turns
3100 * on the undo facility of the tableau. Turn it off again
3101 * at the end, assuming it was turned off to begin with.
3103 * Return 0 on success and -1 on failure.
3105 static int propagate_equalities(struct isl_context_gbr *cgbr,
3106 struct isl_tab *tab, unsigned first)
3108 int i;
3109 struct isl_vec *eq = NULL;
3110 isl_bool needs_undo;
3112 needs_undo = isl_tab_need_undo(tab);
3113 if (needs_undo < 0)
3114 goto error;
3115 eq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
3116 if (!eq)
3117 goto error;
3119 if (isl_tab_extend_cons(tab, (cgbr->tab->bmap->n_ineq - first)/2) < 0)
3120 goto error;
3122 isl_seq_clr(eq->el + 1 + tab->n_param,
3123 tab->n_var - tab->n_param - tab->n_div);
3124 for (i = first; i < cgbr->tab->bmap->n_ineq; i += 2) {
3125 int j;
3126 int r;
3127 struct isl_tab_undo *snap;
3128 snap = isl_tab_snap(tab);
3130 isl_seq_cpy(eq->el, cgbr->tab->bmap->ineq[i], 1 + tab->n_param);
3131 isl_seq_cpy(eq->el + 1 + tab->n_var - tab->n_div,
3132 cgbr->tab->bmap->ineq[i] + 1 + tab->n_param,
3133 tab->n_div);
3135 r = isl_tab_add_row(tab, eq->el);
3136 if (r < 0)
3137 goto error;
3138 r = tab->con[r].index;
3139 j = last_non_zero_var_col(tab, tab->mat->row[r] + 2 + tab->M);
3140 if (j < 0 || j < tab->n_dead ||
3141 !isl_int_is_one(tab->mat->row[r][0]) ||
3142 (!isl_int_is_one(tab->mat->row[r][2 + tab->M + j]) &&
3143 !isl_int_is_negone(tab->mat->row[r][2 + tab->M + j]))) {
3144 if (isl_tab_rollback(tab, snap) < 0)
3145 goto error;
3146 continue;
3148 if (isl_tab_pivot(tab, r, j) < 0)
3149 goto error;
3150 if (isl_tab_kill_col(tab, j) < 0)
3151 goto error;
3153 if (restore_lexmin(tab) < 0)
3154 goto error;
3157 if (!needs_undo)
3158 isl_tab_clear_undo(tab);
3159 isl_vec_free(eq);
3161 return 0;
3162 error:
3163 isl_vec_free(eq);
3164 isl_tab_free(cgbr->tab);
3165 cgbr->tab = NULL;
3166 return -1;
3169 static int context_gbr_detect_equalities(struct isl_context *context,
3170 struct isl_tab *tab)
3172 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3173 unsigned n_ineq;
3175 if (!cgbr->cone) {
3176 struct isl_basic_set *bset = isl_tab_peek_bset(cgbr->tab);
3177 cgbr->cone = isl_tab_from_recession_cone(bset, 0);
3178 if (!cgbr->cone)
3179 goto error;
3180 if (isl_tab_track_bset(cgbr->cone,
3181 isl_basic_set_copy(bset)) < 0)
3182 goto error;
3184 if (isl_tab_detect_implicit_equalities(cgbr->cone) < 0)
3185 goto error;
3187 n_ineq = cgbr->tab->bmap->n_ineq;
3188 cgbr->tab = isl_tab_detect_equalities(cgbr->tab, cgbr->cone);
3189 if (!cgbr->tab)
3190 return -1;
3191 if (cgbr->tab->bmap->n_ineq > n_ineq &&
3192 propagate_equalities(cgbr, tab, n_ineq) < 0)
3193 return -1;
3195 return 0;
3196 error:
3197 isl_tab_free(cgbr->tab);
3198 cgbr->tab = NULL;
3199 return -1;
3202 static int context_gbr_get_div(struct isl_context *context, struct isl_tab *tab,
3203 struct isl_vec *div)
3205 return get_div(tab, context, div);
3208 static isl_bool context_gbr_add_div(struct isl_context *context,
3209 __isl_keep isl_vec *div)
3211 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3212 if (cgbr->cone) {
3213 int r, n_div, o_div;
3215 n_div = isl_basic_map_dim(cgbr->cone->bmap, isl_dim_div);
3216 o_div = cgbr->cone->n_var - n_div;
3218 if (isl_tab_extend_cons(cgbr->cone, 3) < 0)
3219 return isl_bool_error;
3220 if (isl_tab_extend_vars(cgbr->cone, 1) < 0)
3221 return isl_bool_error;
3222 if ((r = isl_tab_allocate_var(cgbr->cone)) <0)
3223 return isl_bool_error;
3225 cgbr->cone->bmap = isl_basic_map_insert_div(cgbr->cone->bmap,
3226 r - o_div, div);
3227 if (!cgbr->cone->bmap)
3228 return isl_bool_error;
3229 if (isl_tab_push_var(cgbr->cone, isl_tab_undo_bmap_div,
3230 &cgbr->cone->var[r]) < 0)
3231 return isl_bool_error;
3233 return context_tab_add_div(cgbr->tab, div,
3234 context_gbr_add_ineq_wrap, context);
3237 static int context_gbr_best_split(struct isl_context *context,
3238 struct isl_tab *tab)
3240 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3241 struct isl_tab_undo *snap;
3242 int r;
3244 snap = isl_tab_snap(cgbr->tab);
3245 r = best_split(tab, cgbr->tab);
3247 if (r >= 0 && isl_tab_rollback(cgbr->tab, snap) < 0)
3248 return -1;
3250 return r;
3253 static int context_gbr_is_empty(struct isl_context *context)
3255 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3256 if (!cgbr->tab)
3257 return -1;
3258 return cgbr->tab->empty;
3261 struct isl_gbr_tab_undo {
3262 struct isl_tab_undo *tab_snap;
3263 struct isl_tab_undo *shifted_snap;
3264 struct isl_tab_undo *cone_snap;
3267 static void *context_gbr_save(struct isl_context *context)
3269 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3270 struct isl_gbr_tab_undo *snap;
3272 if (!cgbr->tab)
3273 return NULL;
3275 snap = isl_alloc_type(cgbr->tab->mat->ctx, struct isl_gbr_tab_undo);
3276 if (!snap)
3277 return NULL;
3279 snap->tab_snap = isl_tab_snap(cgbr->tab);
3280 if (isl_tab_save_samples(cgbr->tab) < 0)
3281 goto error;
3283 if (cgbr->shifted)
3284 snap->shifted_snap = isl_tab_snap(cgbr->shifted);
3285 else
3286 snap->shifted_snap = NULL;
3288 if (cgbr->cone)
3289 snap->cone_snap = isl_tab_snap(cgbr->cone);
3290 else
3291 snap->cone_snap = NULL;
3293 return snap;
3294 error:
3295 free(snap);
3296 return NULL;
3299 static void context_gbr_restore(struct isl_context *context, void *save)
3301 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3302 struct isl_gbr_tab_undo *snap = (struct isl_gbr_tab_undo *)save;
3303 if (!snap)
3304 goto error;
3305 if (isl_tab_rollback(cgbr->tab, snap->tab_snap) < 0)
3306 goto error;
3308 if (snap->shifted_snap) {
3309 if (isl_tab_rollback(cgbr->shifted, snap->shifted_snap) < 0)
3310 goto error;
3311 } else if (cgbr->shifted) {
3312 isl_tab_free(cgbr->shifted);
3313 cgbr->shifted = NULL;
3316 if (snap->cone_snap) {
3317 if (isl_tab_rollback(cgbr->cone, snap->cone_snap) < 0)
3318 goto error;
3319 } else if (cgbr->cone) {
3320 isl_tab_free(cgbr->cone);
3321 cgbr->cone = NULL;
3324 free(snap);
3326 return;
3327 error:
3328 free(snap);
3329 isl_tab_free(cgbr->tab);
3330 cgbr->tab = NULL;
3333 static void context_gbr_discard(void *save)
3335 struct isl_gbr_tab_undo *snap = (struct isl_gbr_tab_undo *)save;
3336 free(snap);
3339 static int context_gbr_is_ok(struct isl_context *context)
3341 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3342 return !!cgbr->tab;
3345 static void context_gbr_invalidate(struct isl_context *context)
3347 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3348 isl_tab_free(cgbr->tab);
3349 cgbr->tab = NULL;
3352 static __isl_null struct isl_context *context_gbr_free(
3353 struct isl_context *context)
3355 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3356 isl_tab_free(cgbr->tab);
3357 isl_tab_free(cgbr->shifted);
3358 isl_tab_free(cgbr->cone);
3359 free(cgbr);
3361 return NULL;
3364 struct isl_context_op isl_context_gbr_op = {
3365 context_gbr_detect_nonnegative_parameters,
3366 context_gbr_peek_basic_set,
3367 context_gbr_peek_tab,
3368 context_gbr_add_eq,
3369 context_gbr_add_ineq,
3370 context_gbr_ineq_sign,
3371 context_gbr_test_ineq,
3372 context_gbr_get_div,
3373 context_gbr_add_div,
3374 context_gbr_detect_equalities,
3375 context_gbr_best_split,
3376 context_gbr_is_empty,
3377 context_gbr_is_ok,
3378 context_gbr_save,
3379 context_gbr_restore,
3380 context_gbr_discard,
3381 context_gbr_invalidate,
3382 context_gbr_free,
3385 static struct isl_context *isl_context_gbr_alloc(__isl_keep isl_basic_set *dom)
3387 struct isl_context_gbr *cgbr;
3389 if (!dom)
3390 return NULL;
3392 cgbr = isl_calloc_type(dom->ctx, struct isl_context_gbr);
3393 if (!cgbr)
3394 return NULL;
3396 cgbr->context.op = &isl_context_gbr_op;
3398 cgbr->shifted = NULL;
3399 cgbr->cone = NULL;
3400 cgbr->tab = isl_tab_from_basic_set(dom, 1);
3401 cgbr->tab = isl_tab_init_samples(cgbr->tab);
3402 if (!cgbr->tab)
3403 goto error;
3404 check_gbr_integer_feasible(cgbr);
3406 return &cgbr->context;
3407 error:
3408 cgbr->context.op->free(&cgbr->context);
3409 return NULL;
3412 static struct isl_context *isl_context_alloc(__isl_keep isl_basic_set *dom)
3414 if (!dom)
3415 return NULL;
3417 if (dom->ctx->opt->context == ISL_CONTEXT_LEXMIN)
3418 return isl_context_lex_alloc(dom);
3419 else
3420 return isl_context_gbr_alloc(dom);
3423 /* Construct an isl_sol_map structure for accumulating the solution.
3424 * If track_empty is set, then we also keep track of the parts
3425 * of the context where there is no solution.
3426 * If max is set, then we are solving a maximization, rather than
3427 * a minimization problem, which means that the variables in the
3428 * tableau have value "M - x" rather than "M + x".
3430 static struct isl_sol *sol_map_init(struct isl_basic_map *bmap,
3431 struct isl_basic_set *dom, int track_empty, int max)
3433 struct isl_sol_map *sol_map = NULL;
3435 if (!bmap)
3436 goto error;
3438 sol_map = isl_calloc_type(bmap->ctx, struct isl_sol_map);
3439 if (!sol_map)
3440 goto error;
3442 sol_map->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
3443 sol_map->sol.dec_level.callback.run = &sol_dec_level_wrap;
3444 sol_map->sol.dec_level.sol = &sol_map->sol;
3445 sol_map->sol.max = max;
3446 sol_map->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
3447 sol_map->sol.add = &sol_map_add_wrap;
3448 sol_map->sol.add_empty = track_empty ? &sol_map_add_empty_wrap : NULL;
3449 sol_map->sol.free = &sol_map_free_wrap;
3450 sol_map->map = isl_map_alloc_space(isl_basic_map_get_space(bmap), 1,
3451 ISL_MAP_DISJOINT);
3452 if (!sol_map->map)
3453 goto error;
3455 sol_map->sol.context = isl_context_alloc(dom);
3456 if (!sol_map->sol.context)
3457 goto error;
3459 if (track_empty) {
3460 sol_map->empty = isl_set_alloc_space(isl_basic_set_get_space(dom),
3461 1, ISL_SET_DISJOINT);
3462 if (!sol_map->empty)
3463 goto error;
3466 isl_basic_set_free(dom);
3467 return &sol_map->sol;
3468 error:
3469 isl_basic_set_free(dom);
3470 sol_map_free(sol_map);
3471 return NULL;
3474 /* Check whether all coefficients of (non-parameter) variables
3475 * are non-positive, meaning that no pivots can be performed on the row.
3477 static int is_critical(struct isl_tab *tab, int row)
3479 int j;
3480 unsigned off = 2 + tab->M;
3482 for (j = tab->n_dead; j < tab->n_col; ++j) {
3483 if (tab->col_var[j] >= 0 &&
3484 (tab->col_var[j] < tab->n_param ||
3485 tab->col_var[j] >= tab->n_var - tab->n_div))
3486 continue;
3488 if (isl_int_is_pos(tab->mat->row[row][off + j]))
3489 return 0;
3492 return 1;
3495 /* Check whether the inequality represented by vec is strict over the integers,
3496 * i.e., there are no integer values satisfying the constraint with
3497 * equality. This happens if the gcd of the coefficients is not a divisor
3498 * of the constant term. If so, scale the constraint down by the gcd
3499 * of the coefficients.
3501 static int is_strict(struct isl_vec *vec)
3503 isl_int gcd;
3504 int strict = 0;
3506 isl_int_init(gcd);
3507 isl_seq_gcd(vec->el + 1, vec->size - 1, &gcd);
3508 if (!isl_int_is_one(gcd)) {
3509 strict = !isl_int_is_divisible_by(vec->el[0], gcd);
3510 isl_int_fdiv_q(vec->el[0], vec->el[0], gcd);
3511 isl_seq_scale_down(vec->el + 1, vec->el + 1, gcd, vec->size-1);
3513 isl_int_clear(gcd);
3515 return strict;
3518 /* Determine the sign of the given row of the main tableau.
3519 * The result is one of
3520 * isl_tab_row_pos: always non-negative; no pivot needed
3521 * isl_tab_row_neg: always non-positive; pivot
3522 * isl_tab_row_any: can be both positive and negative; split
3524 * We first handle some simple cases
3525 * - the row sign may be known already
3526 * - the row may be obviously non-negative
3527 * - the parametric constant may be equal to that of another row
3528 * for which we know the sign. This sign will be either "pos" or
3529 * "any". If it had been "neg" then we would have pivoted before.
3531 * If none of these cases hold, we check the value of the row for each
3532 * of the currently active samples. Based on the signs of these values
3533 * we make an initial determination of the sign of the row.
3535 * all zero -> unk(nown)
3536 * all non-negative -> pos
3537 * all non-positive -> neg
3538 * both negative and positive -> all
3540 * If we end up with "all", we are done.
3541 * Otherwise, we perform a check for positive and/or negative
3542 * values as follows.
3544 * samples neg unk pos
3545 * <0 ? Y N Y N
3546 * pos any pos
3547 * >0 ? Y N Y N
3548 * any neg any neg
3550 * There is no special sign for "zero", because we can usually treat zero
3551 * as either non-negative or non-positive, whatever works out best.
3552 * However, if the row is "critical", meaning that pivoting is impossible
3553 * then we don't want to limp zero with the non-positive case, because
3554 * then we we would lose the solution for those values of the parameters
3555 * where the value of the row is zero. Instead, we treat 0 as non-negative
3556 * ensuring a split if the row can attain both zero and negative values.
3557 * The same happens when the original constraint was one that could not
3558 * be satisfied with equality by any integer values of the parameters.
3559 * In this case, we normalize the constraint, but then a value of zero
3560 * for the normalized constraint is actually a positive value for the
3561 * original constraint, so again we need to treat zero as non-negative.
3562 * In both these cases, we have the following decision tree instead:
3564 * all non-negative -> pos
3565 * all negative -> neg
3566 * both negative and non-negative -> all
3568 * samples neg pos
3569 * <0 ? Y N
3570 * any pos
3571 * >=0 ? Y N
3572 * any neg
3574 static enum isl_tab_row_sign row_sign(struct isl_tab *tab,
3575 struct isl_sol *sol, int row)
3577 struct isl_vec *ineq = NULL;
3578 enum isl_tab_row_sign res = isl_tab_row_unknown;
3579 int critical;
3580 int strict;
3581 int row2;
3583 if (tab->row_sign[row] != isl_tab_row_unknown)
3584 return tab->row_sign[row];
3585 if (is_obviously_nonneg(tab, row))
3586 return isl_tab_row_pos;
3587 for (row2 = tab->n_redundant; row2 < tab->n_row; ++row2) {
3588 if (tab->row_sign[row2] == isl_tab_row_unknown)
3589 continue;
3590 if (identical_parameter_line(tab, row, row2))
3591 return tab->row_sign[row2];
3594 critical = is_critical(tab, row);
3596 ineq = get_row_parameter_ineq(tab, row);
3597 if (!ineq)
3598 goto error;
3600 strict = is_strict(ineq);
3602 res = sol->context->op->ineq_sign(sol->context, ineq->el,
3603 critical || strict);
3605 if (res == isl_tab_row_unknown || res == isl_tab_row_pos) {
3606 /* test for negative values */
3607 int feasible;
3608 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3609 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3611 feasible = sol->context->op->test_ineq(sol->context, ineq->el);
3612 if (feasible < 0)
3613 goto error;
3614 if (!feasible)
3615 res = isl_tab_row_pos;
3616 else
3617 res = (res == isl_tab_row_unknown) ? isl_tab_row_neg
3618 : isl_tab_row_any;
3619 if (res == isl_tab_row_neg) {
3620 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3621 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3625 if (res == isl_tab_row_neg) {
3626 /* test for positive values */
3627 int feasible;
3628 if (!critical && !strict)
3629 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3631 feasible = sol->context->op->test_ineq(sol->context, ineq->el);
3632 if (feasible < 0)
3633 goto error;
3634 if (feasible)
3635 res = isl_tab_row_any;
3638 isl_vec_free(ineq);
3639 return res;
3640 error:
3641 isl_vec_free(ineq);
3642 return isl_tab_row_unknown;
3645 static void find_solutions(struct isl_sol *sol, struct isl_tab *tab);
3647 /* Find solutions for values of the parameters that satisfy the given
3648 * inequality.
3650 * We currently take a snapshot of the context tableau that is reset
3651 * when we return from this function, while we make a copy of the main
3652 * tableau, leaving the original main tableau untouched.
3653 * These are fairly arbitrary choices. Making a copy also of the context
3654 * tableau would obviate the need to undo any changes made to it later,
3655 * while taking a snapshot of the main tableau could reduce memory usage.
3656 * If we were to switch to taking a snapshot of the main tableau,
3657 * we would have to keep in mind that we need to save the row signs
3658 * and that we need to do this before saving the current basis
3659 * such that the basis has been restore before we restore the row signs.
3661 static void find_in_pos(struct isl_sol *sol, struct isl_tab *tab, isl_int *ineq)
3663 void *saved;
3665 if (!sol->context)
3666 goto error;
3667 saved = sol->context->op->save(sol->context);
3669 tab = isl_tab_dup(tab);
3670 if (!tab)
3671 goto error;
3673 sol->context->op->add_ineq(sol->context, ineq, 0, 1);
3675 find_solutions(sol, tab);
3677 if (!sol->error)
3678 sol->context->op->restore(sol->context, saved);
3679 else
3680 sol->context->op->discard(saved);
3681 return;
3682 error:
3683 sol->error = 1;
3686 /* Record the absence of solutions for those values of the parameters
3687 * that do not satisfy the given inequality with equality.
3689 static void no_sol_in_strict(struct isl_sol *sol,
3690 struct isl_tab *tab, struct isl_vec *ineq)
3692 int empty;
3693 void *saved;
3695 if (!sol->context || sol->error)
3696 goto error;
3697 saved = sol->context->op->save(sol->context);
3699 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3701 sol->context->op->add_ineq(sol->context, ineq->el, 1, 0);
3702 if (!sol->context)
3703 goto error;
3705 empty = tab->empty;
3706 tab->empty = 1;
3707 sol_add(sol, tab);
3708 tab->empty = empty;
3710 isl_int_add_ui(ineq->el[0], ineq->el[0], 1);
3712 sol->context->op->restore(sol->context, saved);
3713 return;
3714 error:
3715 sol->error = 1;
3718 /* Reset all row variables that are marked to have a sign that may
3719 * be both positive and negative to have an unknown sign.
3721 static void reset_any_to_unknown(struct isl_tab *tab)
3723 int row;
3725 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3726 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
3727 continue;
3728 if (tab->row_sign[row] == isl_tab_row_any)
3729 tab->row_sign[row] = isl_tab_row_unknown;
3733 /* Compute the lexicographic minimum of the set represented by the main
3734 * tableau "tab" within the context "sol->context_tab".
3735 * On entry the sample value of the main tableau is lexicographically
3736 * less than or equal to this lexicographic minimum.
3737 * Pivots are performed until a feasible point is found, which is then
3738 * necessarily equal to the minimum, or until the tableau is found to
3739 * be infeasible. Some pivots may need to be performed for only some
3740 * feasible values of the context tableau. If so, the context tableau
3741 * is split into a part where the pivot is needed and a part where it is not.
3743 * Whenever we enter the main loop, the main tableau is such that no
3744 * "obvious" pivots need to be performed on it, where "obvious" means
3745 * that the given row can be seen to be negative without looking at
3746 * the context tableau. In particular, for non-parametric problems,
3747 * no pivots need to be performed on the main tableau.
3748 * The caller of find_solutions is responsible for making this property
3749 * hold prior to the first iteration of the loop, while restore_lexmin
3750 * is called before every other iteration.
3752 * Inside the main loop, we first examine the signs of the rows of
3753 * the main tableau within the context of the context tableau.
3754 * If we find a row that is always non-positive for all values of
3755 * the parameters satisfying the context tableau and negative for at
3756 * least one value of the parameters, we perform the appropriate pivot
3757 * and start over. An exception is the case where no pivot can be
3758 * performed on the row. In this case, we require that the sign of
3759 * the row is negative for all values of the parameters (rather than just
3760 * non-positive). This special case is handled inside row_sign, which
3761 * will say that the row can have any sign if it determines that it can
3762 * attain both negative and zero values.
3764 * If we can't find a row that always requires a pivot, but we can find
3765 * one or more rows that require a pivot for some values of the parameters
3766 * (i.e., the row can attain both positive and negative signs), then we split
3767 * the context tableau into two parts, one where we force the sign to be
3768 * non-negative and one where we force is to be negative.
3769 * The non-negative part is handled by a recursive call (through find_in_pos).
3770 * Upon returning from this call, we continue with the negative part and
3771 * perform the required pivot.
3773 * If no such rows can be found, all rows are non-negative and we have
3774 * found a (rational) feasible point. If we only wanted a rational point
3775 * then we are done.
3776 * Otherwise, we check if all values of the sample point of the tableau
3777 * are integral for the variables. If so, we have found the minimal
3778 * integral point and we are done.
3779 * If the sample point is not integral, then we need to make a distinction
3780 * based on whether the constant term is non-integral or the coefficients
3781 * of the parameters. Furthermore, in order to decide how to handle
3782 * the non-integrality, we also need to know whether the coefficients
3783 * of the other columns in the tableau are integral. This leads
3784 * to the following table. The first two rows do not correspond
3785 * to a non-integral sample point and are only mentioned for completeness.
3787 * constant parameters other
3789 * int int int |
3790 * int int rat | -> no problem
3792 * rat int int -> fail
3794 * rat int rat -> cut
3796 * int rat rat |
3797 * rat rat rat | -> parametric cut
3799 * int rat int |
3800 * rat rat int | -> split context
3802 * If the parametric constant is completely integral, then there is nothing
3803 * to be done. If the constant term is non-integral, but all the other
3804 * coefficient are integral, then there is nothing that can be done
3805 * and the tableau has no integral solution.
3806 * If, on the other hand, one or more of the other columns have rational
3807 * coefficients, but the parameter coefficients are all integral, then
3808 * we can perform a regular (non-parametric) cut.
3809 * Finally, if there is any parameter coefficient that is non-integral,
3810 * then we need to involve the context tableau. There are two cases here.
3811 * If at least one other column has a rational coefficient, then we
3812 * can perform a parametric cut in the main tableau by adding a new
3813 * integer division in the context tableau.
3814 * If all other columns have integral coefficients, then we need to
3815 * enforce that the rational combination of parameters (c + \sum a_i y_i)/m
3816 * is always integral. We do this by introducing an integer division
3817 * q = floor((c + \sum a_i y_i)/m) and stipulating that its argument should
3818 * always be integral in the context tableau, i.e., m q = c + \sum a_i y_i.
3819 * Since q is expressed in the tableau as
3820 * c + \sum a_i y_i - m q >= 0
3821 * -c - \sum a_i y_i + m q + m - 1 >= 0
3822 * it is sufficient to add the inequality
3823 * -c - \sum a_i y_i + m q >= 0
3824 * In the part of the context where this inequality does not hold, the
3825 * main tableau is marked as being empty.
3827 static void find_solutions(struct isl_sol *sol, struct isl_tab *tab)
3829 struct isl_context *context;
3830 int r;
3832 if (!tab || sol->error)
3833 goto error;
3835 context = sol->context;
3837 if (tab->empty)
3838 goto done;
3839 if (context->op->is_empty(context))
3840 goto done;
3842 for (r = 0; r >= 0 && tab && !tab->empty; r = restore_lexmin(tab)) {
3843 int flags;
3844 int row;
3845 enum isl_tab_row_sign sgn;
3846 int split = -1;
3847 int n_split = 0;
3849 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3850 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
3851 continue;
3852 sgn = row_sign(tab, sol, row);
3853 if (!sgn)
3854 goto error;
3855 tab->row_sign[row] = sgn;
3856 if (sgn == isl_tab_row_any)
3857 n_split++;
3858 if (sgn == isl_tab_row_any && split == -1)
3859 split = row;
3860 if (sgn == isl_tab_row_neg)
3861 break;
3863 if (row < tab->n_row)
3864 continue;
3865 if (split != -1) {
3866 struct isl_vec *ineq;
3867 if (n_split != 1)
3868 split = context->op->best_split(context, tab);
3869 if (split < 0)
3870 goto error;
3871 ineq = get_row_parameter_ineq(tab, split);
3872 if (!ineq)
3873 goto error;
3874 is_strict(ineq);
3875 reset_any_to_unknown(tab);
3876 tab->row_sign[split] = isl_tab_row_pos;
3877 sol_inc_level(sol);
3878 find_in_pos(sol, tab, ineq->el);
3879 tab->row_sign[split] = isl_tab_row_neg;
3880 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3881 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3882 if (!sol->error)
3883 context->op->add_ineq(context, ineq->el, 0, 1);
3884 isl_vec_free(ineq);
3885 if (sol->error)
3886 goto error;
3887 continue;
3889 if (tab->rational)
3890 break;
3891 row = first_non_integer_row(tab, &flags);
3892 if (row < 0)
3893 break;
3894 if (ISL_FL_ISSET(flags, I_PAR)) {
3895 if (ISL_FL_ISSET(flags, I_VAR)) {
3896 if (isl_tab_mark_empty(tab) < 0)
3897 goto error;
3898 break;
3900 row = add_cut(tab, row);
3901 } else if (ISL_FL_ISSET(flags, I_VAR)) {
3902 struct isl_vec *div;
3903 struct isl_vec *ineq;
3904 int d;
3905 div = get_row_split_div(tab, row);
3906 if (!div)
3907 goto error;
3908 d = context->op->get_div(context, tab, div);
3909 isl_vec_free(div);
3910 if (d < 0)
3911 goto error;
3912 ineq = ineq_for_div(context->op->peek_basic_set(context), d);
3913 if (!ineq)
3914 goto error;
3915 sol_inc_level(sol);
3916 no_sol_in_strict(sol, tab, ineq);
3917 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3918 context->op->add_ineq(context, ineq->el, 1, 1);
3919 isl_vec_free(ineq);
3920 if (sol->error || !context->op->is_ok(context))
3921 goto error;
3922 tab = set_row_cst_to_div(tab, row, d);
3923 if (context->op->is_empty(context))
3924 break;
3925 } else
3926 row = add_parametric_cut(tab, row, context);
3927 if (row < 0)
3928 goto error;
3930 if (r < 0)
3931 goto error;
3932 done:
3933 sol_add(sol, tab);
3934 isl_tab_free(tab);
3935 return;
3936 error:
3937 isl_tab_free(tab);
3938 sol->error = 1;
3941 /* Does "sol" contain a pair of partial solutions that could potentially
3942 * be merged?
3944 * We currently only check that "sol" is not in an error state
3945 * and that there are at least two partial solutions of which the final two
3946 * are defined at the same level.
3948 static int sol_has_mergeable_solutions(struct isl_sol *sol)
3950 if (sol->error)
3951 return 0;
3952 if (!sol->partial)
3953 return 0;
3954 if (!sol->partial->next)
3955 return 0;
3956 return sol->partial->level == sol->partial->next->level;
3959 /* Compute the lexicographic minimum of the set represented by the main
3960 * tableau "tab" within the context "sol->context_tab".
3962 * As a preprocessing step, we first transfer all the purely parametric
3963 * equalities from the main tableau to the context tableau, i.e.,
3964 * parameters that have been pivoted to a row.
3965 * These equalities are ignored by the main algorithm, because the
3966 * corresponding rows may not be marked as being non-negative.
3967 * In parts of the context where the added equality does not hold,
3968 * the main tableau is marked as being empty.
3970 * Before we embark on the actual computation, we save a copy
3971 * of the context. When we return, we check if there are any
3972 * partial solutions that can potentially be merged. If so,
3973 * we perform a rollback to the initial state of the context.
3974 * The merging of partial solutions happens inside calls to
3975 * sol_dec_level that are pushed onto the undo stack of the context.
3976 * If there are no partial solutions that can potentially be merged
3977 * then the rollback is skipped as it would just be wasted effort.
3979 static void find_solutions_main(struct isl_sol *sol, struct isl_tab *tab)
3981 int row;
3982 void *saved;
3984 if (!tab)
3985 goto error;
3987 sol->level = 0;
3989 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3990 int p;
3991 struct isl_vec *eq;
3993 if (tab->row_var[row] < 0)
3994 continue;
3995 if (tab->row_var[row] >= tab->n_param &&
3996 tab->row_var[row] < tab->n_var - tab->n_div)
3997 continue;
3998 if (tab->row_var[row] < tab->n_param)
3999 p = tab->row_var[row];
4000 else
4001 p = tab->row_var[row]
4002 + tab->n_param - (tab->n_var - tab->n_div);
4004 eq = isl_vec_alloc(tab->mat->ctx, 1+tab->n_param+tab->n_div);
4005 if (!eq)
4006 goto error;
4007 get_row_parameter_line(tab, row, eq->el);
4008 isl_int_neg(eq->el[1 + p], tab->mat->row[row][0]);
4009 eq = isl_vec_normalize(eq);
4011 sol_inc_level(sol);
4012 no_sol_in_strict(sol, tab, eq);
4014 isl_seq_neg(eq->el, eq->el, eq->size);
4015 sol_inc_level(sol);
4016 no_sol_in_strict(sol, tab, eq);
4017 isl_seq_neg(eq->el, eq->el, eq->size);
4019 sol->context->op->add_eq(sol->context, eq->el, 1, 1);
4021 isl_vec_free(eq);
4023 if (isl_tab_mark_redundant(tab, row) < 0)
4024 goto error;
4026 if (sol->context->op->is_empty(sol->context))
4027 break;
4029 row = tab->n_redundant - 1;
4032 saved = sol->context->op->save(sol->context);
4034 find_solutions(sol, tab);
4036 if (sol_has_mergeable_solutions(sol))
4037 sol->context->op->restore(sol->context, saved);
4038 else
4039 sol->context->op->discard(saved);
4041 sol->level = 0;
4042 sol_pop(sol);
4044 return;
4045 error:
4046 isl_tab_free(tab);
4047 sol->error = 1;
4050 /* Check if integer division "div" of "dom" also occurs in "bmap".
4051 * If so, return its position within the divs.
4052 * If not, return -1.
4054 static int find_context_div(struct isl_basic_map *bmap,
4055 struct isl_basic_set *dom, unsigned div)
4057 int i;
4058 unsigned b_dim = isl_space_dim(bmap->dim, isl_dim_all);
4059 unsigned d_dim = isl_space_dim(dom->dim, isl_dim_all);
4061 if (isl_int_is_zero(dom->div[div][0]))
4062 return -1;
4063 if (isl_seq_first_non_zero(dom->div[div] + 2 + d_dim, dom->n_div) != -1)
4064 return -1;
4066 for (i = 0; i < bmap->n_div; ++i) {
4067 if (isl_int_is_zero(bmap->div[i][0]))
4068 continue;
4069 if (isl_seq_first_non_zero(bmap->div[i] + 2 + d_dim,
4070 (b_dim - d_dim) + bmap->n_div) != -1)
4071 continue;
4072 if (isl_seq_eq(bmap->div[i], dom->div[div], 2 + d_dim))
4073 return i;
4075 return -1;
4078 /* The correspondence between the variables in the main tableau,
4079 * the context tableau, and the input map and domain is as follows.
4080 * The first n_param and the last n_div variables of the main tableau
4081 * form the variables of the context tableau.
4082 * In the basic map, these n_param variables correspond to the
4083 * parameters and the input dimensions. In the domain, they correspond
4084 * to the parameters and the set dimensions.
4085 * The n_div variables correspond to the integer divisions in the domain.
4086 * To ensure that everything lines up, we may need to copy some of the
4087 * integer divisions of the domain to the map. These have to be placed
4088 * in the same order as those in the context and they have to be placed
4089 * after any other integer divisions that the map may have.
4090 * This function performs the required reordering.
4092 static struct isl_basic_map *align_context_divs(struct isl_basic_map *bmap,
4093 struct isl_basic_set *dom)
4095 int i;
4096 int common = 0;
4097 int other;
4099 for (i = 0; i < dom->n_div; ++i)
4100 if (find_context_div(bmap, dom, i) != -1)
4101 common++;
4102 other = bmap->n_div - common;
4103 if (dom->n_div - common > 0) {
4104 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
4105 dom->n_div - common, 0, 0);
4106 if (!bmap)
4107 return NULL;
4109 for (i = 0; i < dom->n_div; ++i) {
4110 int pos = find_context_div(bmap, dom, i);
4111 if (pos < 0) {
4112 pos = isl_basic_map_alloc_div(bmap);
4113 if (pos < 0)
4114 goto error;
4115 isl_int_set_si(bmap->div[pos][0], 0);
4117 if (pos != other + i)
4118 isl_basic_map_swap_div(bmap, pos, other + i);
4120 return bmap;
4121 error:
4122 isl_basic_map_free(bmap);
4123 return NULL;
4126 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4127 * some obvious symmetries.
4129 * We make sure the divs in the domain are properly ordered,
4130 * because they will be added one by one in the given order
4131 * during the construction of the solution map.
4133 static struct isl_sol *basic_map_partial_lexopt_base_sol(
4134 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4135 __isl_give isl_set **empty, int max,
4136 struct isl_sol *(*init)(__isl_keep isl_basic_map *bmap,
4137 __isl_take isl_basic_set *dom, int track_empty, int max))
4139 struct isl_tab *tab;
4140 struct isl_sol *sol = NULL;
4141 struct isl_context *context;
4143 if (dom->n_div) {
4144 dom = isl_basic_set_order_divs(dom);
4145 bmap = align_context_divs(bmap, dom);
4147 sol = init(bmap, dom, !!empty, max);
4148 if (!sol)
4149 goto error;
4151 context = sol->context;
4152 if (isl_basic_set_plain_is_empty(context->op->peek_basic_set(context)))
4153 /* nothing */;
4154 else if (isl_basic_map_plain_is_empty(bmap)) {
4155 if (sol->add_empty)
4156 sol->add_empty(sol,
4157 isl_basic_set_copy(context->op->peek_basic_set(context)));
4158 } else {
4159 tab = tab_for_lexmin(bmap,
4160 context->op->peek_basic_set(context), 1, max);
4161 tab = context->op->detect_nonnegative_parameters(context, tab);
4162 find_solutions_main(sol, tab);
4164 if (sol->error)
4165 goto error;
4167 isl_basic_map_free(bmap);
4168 return sol;
4169 error:
4170 sol_free(sol);
4171 isl_basic_map_free(bmap);
4172 return NULL;
4175 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4176 * some obvious symmetries.
4178 * We call basic_map_partial_lexopt_base_sol and extract the results.
4180 static __isl_give isl_map *basic_map_partial_lexopt_base(
4181 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4182 __isl_give isl_set **empty, int max)
4184 isl_map *result = NULL;
4185 struct isl_sol *sol;
4186 struct isl_sol_map *sol_map;
4188 sol = basic_map_partial_lexopt_base_sol(bmap, dom, empty, max,
4189 &sol_map_init);
4190 if (!sol)
4191 return NULL;
4192 sol_map = (struct isl_sol_map *) sol;
4194 result = isl_map_copy(sol_map->map);
4195 if (empty)
4196 *empty = isl_set_copy(sol_map->empty);
4197 sol_free(&sol_map->sol);
4198 return result;
4201 /* Return a count of the number of occurrences of the "n" first
4202 * variables in the inequality constraints of "bmap".
4204 static __isl_give int *count_occurrences(__isl_keep isl_basic_map *bmap,
4205 int n)
4207 int i, j;
4208 isl_ctx *ctx;
4209 int *occurrences;
4211 if (!bmap)
4212 return NULL;
4213 ctx = isl_basic_map_get_ctx(bmap);
4214 occurrences = isl_calloc_array(ctx, int, n);
4215 if (!occurrences)
4216 return NULL;
4218 for (i = 0; i < bmap->n_ineq; ++i) {
4219 for (j = 0; j < n; ++j) {
4220 if (!isl_int_is_zero(bmap->ineq[i][1 + j]))
4221 occurrences[j]++;
4225 return occurrences;
4228 /* Do all of the "n" variables with non-zero coefficients in "c"
4229 * occur in exactly a single constraint.
4230 * "occurrences" is an array of length "n" containing the number
4231 * of occurrences of each of the variables in the inequality constraints.
4233 static int single_occurrence(int n, isl_int *c, int *occurrences)
4235 int i;
4237 for (i = 0; i < n; ++i) {
4238 if (isl_int_is_zero(c[i]))
4239 continue;
4240 if (occurrences[i] != 1)
4241 return 0;
4244 return 1;
4247 /* Do all of the "n" initial variables that occur in inequality constraint
4248 * "ineq" of "bmap" only occur in that constraint?
4250 static int all_single_occurrence(__isl_keep isl_basic_map *bmap, int ineq,
4251 int n)
4253 int i, j;
4255 for (i = 0; i < n; ++i) {
4256 if (isl_int_is_zero(bmap->ineq[ineq][1 + i]))
4257 continue;
4258 for (j = 0; j < bmap->n_ineq; ++j) {
4259 if (j == ineq)
4260 continue;
4261 if (!isl_int_is_zero(bmap->ineq[j][1 + i]))
4262 return 0;
4266 return 1;
4269 /* Structure used during detection of parallel constraints.
4270 * n_in: number of "input" variables: isl_dim_param + isl_dim_in
4271 * n_out: number of "output" variables: isl_dim_out + isl_dim_div
4272 * val: the coefficients of the output variables
4274 struct isl_constraint_equal_info {
4275 isl_basic_map *bmap;
4276 unsigned n_in;
4277 unsigned n_out;
4278 isl_int *val;
4281 /* Check whether the coefficients of the output variables
4282 * of the constraint in "entry" are equal to info->val.
4284 static int constraint_equal(const void *entry, const void *val)
4286 isl_int **row = (isl_int **)entry;
4287 const struct isl_constraint_equal_info *info = val;
4289 return isl_seq_eq((*row) + 1 + info->n_in, info->val, info->n_out);
4292 /* Check whether "bmap" has a pair of constraints that have
4293 * the same coefficients for the output variables.
4294 * Note that the coefficients of the existentially quantified
4295 * variables need to be zero since the existentially quantified
4296 * of the result are usually not the same as those of the input.
4297 * Furthermore, check that each of the input variables that occur
4298 * in those constraints does not occur in any other constraint.
4299 * If so, return 1 and return the row indices of the two constraints
4300 * in *first and *second.
4302 static int parallel_constraints(__isl_keep isl_basic_map *bmap,
4303 int *first, int *second)
4305 int i;
4306 isl_ctx *ctx;
4307 int *occurrences = NULL;
4308 struct isl_hash_table *table = NULL;
4309 struct isl_hash_table_entry *entry;
4310 struct isl_constraint_equal_info info;
4311 unsigned n_out;
4312 unsigned n_div;
4314 ctx = isl_basic_map_get_ctx(bmap);
4315 table = isl_hash_table_alloc(ctx, bmap->n_ineq);
4316 if (!table)
4317 goto error;
4319 info.n_in = isl_basic_map_dim(bmap, isl_dim_param) +
4320 isl_basic_map_dim(bmap, isl_dim_in);
4321 occurrences = count_occurrences(bmap, info.n_in);
4322 if (info.n_in && !occurrences)
4323 goto error;
4324 info.bmap = bmap;
4325 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4326 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4327 info.n_out = n_out + n_div;
4328 for (i = 0; i < bmap->n_ineq; ++i) {
4329 uint32_t hash;
4331 info.val = bmap->ineq[i] + 1 + info.n_in;
4332 if (isl_seq_first_non_zero(info.val, n_out) < 0)
4333 continue;
4334 if (isl_seq_first_non_zero(info.val + n_out, n_div) >= 0)
4335 continue;
4336 if (!single_occurrence(info.n_in, bmap->ineq[i] + 1,
4337 occurrences))
4338 continue;
4339 hash = isl_seq_get_hash(info.val, info.n_out);
4340 entry = isl_hash_table_find(ctx, table, hash,
4341 constraint_equal, &info, 1);
4342 if (!entry)
4343 goto error;
4344 if (entry->data)
4345 break;
4346 entry->data = &bmap->ineq[i];
4349 if (i < bmap->n_ineq) {
4350 *first = ((isl_int **)entry->data) - bmap->ineq;
4351 *second = i;
4354 isl_hash_table_free(ctx, table);
4355 free(occurrences);
4357 return i < bmap->n_ineq;
4358 error:
4359 isl_hash_table_free(ctx, table);
4360 free(occurrences);
4361 return -1;
4364 /* Given a set of upper bounds in "var", add constraints to "bset"
4365 * that make the i-th bound smallest.
4367 * In particular, if there are n bounds b_i, then add the constraints
4369 * b_i <= b_j for j > i
4370 * b_i < b_j for j < i
4372 static __isl_give isl_basic_set *select_minimum(__isl_take isl_basic_set *bset,
4373 __isl_keep isl_mat *var, int i)
4375 isl_ctx *ctx;
4376 int j, k;
4378 ctx = isl_mat_get_ctx(var);
4380 for (j = 0; j < var->n_row; ++j) {
4381 if (j == i)
4382 continue;
4383 k = isl_basic_set_alloc_inequality(bset);
4384 if (k < 0)
4385 goto error;
4386 isl_seq_combine(bset->ineq[k], ctx->one, var->row[j],
4387 ctx->negone, var->row[i], var->n_col);
4388 isl_int_set_si(bset->ineq[k][var->n_col], 0);
4389 if (j < i)
4390 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
4393 bset = isl_basic_set_finalize(bset);
4395 return bset;
4396 error:
4397 isl_basic_set_free(bset);
4398 return NULL;
4401 /* Given a set of upper bounds on the last "input" variable m,
4402 * construct a set that assigns the minimal upper bound to m, i.e.,
4403 * construct a set that divides the space into cells where one
4404 * of the upper bounds is smaller than all the others and assign
4405 * this upper bound to m.
4407 * In particular, if there are n bounds b_i, then the result
4408 * consists of n basic sets, each one of the form
4410 * m = b_i
4411 * b_i <= b_j for j > i
4412 * b_i < b_j for j < i
4414 static __isl_give isl_set *set_minimum(__isl_take isl_space *dim,
4415 __isl_take isl_mat *var)
4417 int i, k;
4418 isl_basic_set *bset = NULL;
4419 isl_set *set = NULL;
4421 if (!dim || !var)
4422 goto error;
4424 set = isl_set_alloc_space(isl_space_copy(dim),
4425 var->n_row, ISL_SET_DISJOINT);
4427 for (i = 0; i < var->n_row; ++i) {
4428 bset = isl_basic_set_alloc_space(isl_space_copy(dim), 0,
4429 1, var->n_row - 1);
4430 k = isl_basic_set_alloc_equality(bset);
4431 if (k < 0)
4432 goto error;
4433 isl_seq_cpy(bset->eq[k], var->row[i], var->n_col);
4434 isl_int_set_si(bset->eq[k][var->n_col], -1);
4435 bset = select_minimum(bset, var, i);
4436 set = isl_set_add_basic_set(set, bset);
4439 isl_space_free(dim);
4440 isl_mat_free(var);
4441 return set;
4442 error:
4443 isl_basic_set_free(bset);
4444 isl_set_free(set);
4445 isl_space_free(dim);
4446 isl_mat_free(var);
4447 return NULL;
4450 /* Given that the last input variable of "bmap" represents the minimum
4451 * of the bounds in "cst", check whether we need to split the domain
4452 * based on which bound attains the minimum.
4454 * A split is needed when the minimum appears in an integer division
4455 * or in an equality. Otherwise, it is only needed if it appears in
4456 * an upper bound that is different from the upper bounds on which it
4457 * is defined.
4459 static int need_split_basic_map(__isl_keep isl_basic_map *bmap,
4460 __isl_keep isl_mat *cst)
4462 int i, j;
4463 unsigned total;
4464 unsigned pos;
4466 pos = cst->n_col - 1;
4467 total = isl_basic_map_dim(bmap, isl_dim_all);
4469 for (i = 0; i < bmap->n_div; ++i)
4470 if (!isl_int_is_zero(bmap->div[i][2 + pos]))
4471 return 1;
4473 for (i = 0; i < bmap->n_eq; ++i)
4474 if (!isl_int_is_zero(bmap->eq[i][1 + pos]))
4475 return 1;
4477 for (i = 0; i < bmap->n_ineq; ++i) {
4478 if (isl_int_is_nonneg(bmap->ineq[i][1 + pos]))
4479 continue;
4480 if (!isl_int_is_negone(bmap->ineq[i][1 + pos]))
4481 return 1;
4482 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + pos + 1,
4483 total - pos - 1) >= 0)
4484 return 1;
4486 for (j = 0; j < cst->n_row; ++j)
4487 if (isl_seq_eq(bmap->ineq[i], cst->row[j], cst->n_col))
4488 break;
4489 if (j >= cst->n_row)
4490 return 1;
4493 return 0;
4496 /* Given that the last set variable of "bset" represents the minimum
4497 * of the bounds in "cst", check whether we need to split the domain
4498 * based on which bound attains the minimum.
4500 * We simply call need_split_basic_map here. This is safe because
4501 * the position of the minimum is computed from "cst" and not
4502 * from "bmap".
4504 static int need_split_basic_set(__isl_keep isl_basic_set *bset,
4505 __isl_keep isl_mat *cst)
4507 return need_split_basic_map((isl_basic_map *)bset, cst);
4510 /* Given that the last set variable of "set" represents the minimum
4511 * of the bounds in "cst", check whether we need to split the domain
4512 * based on which bound attains the minimum.
4514 static int need_split_set(__isl_keep isl_set *set, __isl_keep isl_mat *cst)
4516 int i;
4518 for (i = 0; i < set->n; ++i)
4519 if (need_split_basic_set(set->p[i], cst))
4520 return 1;
4522 return 0;
4525 /* Given a set of which the last set variable is the minimum
4526 * of the bounds in "cst", split each basic set in the set
4527 * in pieces where one of the bounds is (strictly) smaller than the others.
4528 * This subdivision is given in "min_expr".
4529 * The variable is subsequently projected out.
4531 * We only do the split when it is needed.
4532 * For example if the last input variable m = min(a,b) and the only
4533 * constraints in the given basic set are lower bounds on m,
4534 * i.e., l <= m = min(a,b), then we can simply project out m
4535 * to obtain l <= a and l <= b, without having to split on whether
4536 * m is equal to a or b.
4538 static __isl_give isl_set *split(__isl_take isl_set *empty,
4539 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
4541 int n_in;
4542 int i;
4543 isl_space *dim;
4544 isl_set *res;
4546 if (!empty || !min_expr || !cst)
4547 goto error;
4549 n_in = isl_set_dim(empty, isl_dim_set);
4550 dim = isl_set_get_space(empty);
4551 dim = isl_space_drop_dims(dim, isl_dim_set, n_in - 1, 1);
4552 res = isl_set_empty(dim);
4554 for (i = 0; i < empty->n; ++i) {
4555 isl_set *set;
4557 set = isl_set_from_basic_set(isl_basic_set_copy(empty->p[i]));
4558 if (need_split_basic_set(empty->p[i], cst))
4559 set = isl_set_intersect(set, isl_set_copy(min_expr));
4560 set = isl_set_remove_dims(set, isl_dim_set, n_in - 1, 1);
4562 res = isl_set_union_disjoint(res, set);
4565 isl_set_free(empty);
4566 isl_set_free(min_expr);
4567 isl_mat_free(cst);
4568 return res;
4569 error:
4570 isl_set_free(empty);
4571 isl_set_free(min_expr);
4572 isl_mat_free(cst);
4573 return NULL;
4576 /* Given a map of which the last input variable is the minimum
4577 * of the bounds in "cst", split each basic set in the set
4578 * in pieces where one of the bounds is (strictly) smaller than the others.
4579 * This subdivision is given in "min_expr".
4580 * The variable is subsequently projected out.
4582 * The implementation is essentially the same as that of "split".
4584 static __isl_give isl_map *split_domain(__isl_take isl_map *opt,
4585 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
4587 int n_in;
4588 int i;
4589 isl_space *dim;
4590 isl_map *res;
4592 if (!opt || !min_expr || !cst)
4593 goto error;
4595 n_in = isl_map_dim(opt, isl_dim_in);
4596 dim = isl_map_get_space(opt);
4597 dim = isl_space_drop_dims(dim, isl_dim_in, n_in - 1, 1);
4598 res = isl_map_empty(dim);
4600 for (i = 0; i < opt->n; ++i) {
4601 isl_map *map;
4603 map = isl_map_from_basic_map(isl_basic_map_copy(opt->p[i]));
4604 if (need_split_basic_map(opt->p[i], cst))
4605 map = isl_map_intersect_domain(map,
4606 isl_set_copy(min_expr));
4607 map = isl_map_remove_dims(map, isl_dim_in, n_in - 1, 1);
4609 res = isl_map_union_disjoint(res, map);
4612 isl_map_free(opt);
4613 isl_set_free(min_expr);
4614 isl_mat_free(cst);
4615 return res;
4616 error:
4617 isl_map_free(opt);
4618 isl_set_free(min_expr);
4619 isl_mat_free(cst);
4620 return NULL;
4623 static __isl_give isl_map *basic_map_partial_lexopt(
4624 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4625 __isl_give isl_set **empty, int max);
4627 /* This function is called from basic_map_partial_lexopt_symm.
4628 * The last variable of "bmap" and "dom" corresponds to the minimum
4629 * of the bounds in "cst". "map_space" is the space of the original
4630 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
4631 * is the space of the original domain.
4633 * We recursively call basic_map_partial_lexopt and then plug in
4634 * the definition of the minimum in the result.
4636 static __isl_give isl_map *basic_map_partial_lexopt_symm_core(
4637 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4638 __isl_give isl_set **empty, int max, __isl_take isl_mat *cst,
4639 __isl_take isl_space *map_space, __isl_take isl_space *set_space)
4641 isl_map *opt;
4642 isl_set *min_expr;
4644 min_expr = set_minimum(isl_basic_set_get_space(dom), isl_mat_copy(cst));
4646 opt = basic_map_partial_lexopt(bmap, dom, empty, max);
4648 if (empty) {
4649 *empty = split(*empty,
4650 isl_set_copy(min_expr), isl_mat_copy(cst));
4651 *empty = isl_set_reset_space(*empty, set_space);
4654 opt = split_domain(opt, min_expr, cst);
4655 opt = isl_map_reset_space(opt, map_space);
4657 return opt;
4660 /* Extract a domain from "bmap" for the purpose of computing
4661 * a lexicographic optimum.
4663 * This function is only called when the caller wants to compute a full
4664 * lexicographic optimum, i.e., without specifying a domain. In this case,
4665 * the caller is not interested in the part of the domain space where
4666 * there is no solution and the domain can be initialized to those constraints
4667 * of "bmap" that only involve the parameters and the input dimensions.
4668 * This relieves the parametric programming engine from detecting those
4669 * inequalities and transferring them to the context. More importantly,
4670 * it ensures that those inequalities are transferred first and not
4671 * intermixed with inequalities that actually split the domain.
4673 * If the caller does not require the absence of existentially quantified
4674 * variables in the result (i.e., if ISL_OPT_QE is not set in "flags"),
4675 * then the actual domain of "bmap" can be used. This ensures that
4676 * the domain does not need to be split at all just to separate out
4677 * pieces of the domain that do not have a solution from piece that do.
4678 * This domain cannot be used in general because it may involve
4679 * (unknown) existentially quantified variables which will then also
4680 * appear in the solution.
4682 static __isl_give isl_basic_set *extract_domain(__isl_keep isl_basic_map *bmap,
4683 unsigned flags)
4685 int n_div;
4686 int n_out;
4688 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4689 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4690 bmap = isl_basic_map_copy(bmap);
4691 if (ISL_FL_ISSET(flags, ISL_OPT_QE)) {
4692 bmap = isl_basic_map_drop_constraints_involving_dims(bmap,
4693 isl_dim_div, 0, n_div);
4694 bmap = isl_basic_map_drop_constraints_involving_dims(bmap,
4695 isl_dim_out, 0, n_out);
4697 return isl_basic_map_domain(bmap);
4700 #undef TYPE
4701 #define TYPE isl_map
4702 #undef SUFFIX
4703 #define SUFFIX
4704 #include "isl_tab_lexopt_templ.c"
4706 struct isl_sol_for {
4707 struct isl_sol sol;
4708 int (*fn)(__isl_take isl_basic_set *dom,
4709 __isl_take isl_aff_list *list, void *user);
4710 void *user;
4713 static void sol_for_free(struct isl_sol_for *sol_for)
4715 if (!sol_for)
4716 return;
4717 if (sol_for->sol.context)
4718 sol_for->sol.context->op->free(sol_for->sol.context);
4719 free(sol_for);
4722 static void sol_for_free_wrap(struct isl_sol *sol)
4724 sol_for_free((struct isl_sol_for *)sol);
4727 /* Add the solution identified by the tableau and the context tableau.
4729 * See documentation of sol_add for more details.
4731 * Instead of constructing a basic map, this function calls a user
4732 * defined function with the current context as a basic set and
4733 * a list of affine expressions representing the relation between
4734 * the input and output. The space over which the affine expressions
4735 * are defined is the same as that of the domain. The number of
4736 * affine expressions in the list is equal to the number of output variables.
4738 static void sol_for_add(struct isl_sol_for *sol,
4739 struct isl_basic_set *dom, struct isl_mat *M)
4741 int i;
4742 isl_ctx *ctx;
4743 isl_local_space *ls;
4744 isl_aff *aff;
4745 isl_aff_list *list;
4747 if (sol->sol.error || !dom || !M)
4748 goto error;
4750 ctx = isl_basic_set_get_ctx(dom);
4751 ls = isl_basic_set_get_local_space(dom);
4752 list = isl_aff_list_alloc(ctx, M->n_row - 1);
4753 for (i = 1; i < M->n_row; ++i) {
4754 aff = isl_aff_alloc(isl_local_space_copy(ls));
4755 if (aff) {
4756 isl_int_set(aff->v->el[0], M->row[0][0]);
4757 isl_seq_cpy(aff->v->el + 1, M->row[i], M->n_col);
4759 aff = isl_aff_normalize(aff);
4760 list = isl_aff_list_add(list, aff);
4762 isl_local_space_free(ls);
4764 dom = isl_basic_set_finalize(dom);
4766 if (sol->fn(isl_basic_set_copy(dom), list, sol->user) < 0)
4767 goto error;
4769 isl_basic_set_free(dom);
4770 isl_mat_free(M);
4771 return;
4772 error:
4773 isl_basic_set_free(dom);
4774 isl_mat_free(M);
4775 sol->sol.error = 1;
4778 static void sol_for_add_wrap(struct isl_sol *sol,
4779 struct isl_basic_set *dom, struct isl_mat *M)
4781 sol_for_add((struct isl_sol_for *)sol, dom, M);
4784 static struct isl_sol_for *sol_for_init(struct isl_basic_map *bmap, int max,
4785 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4786 void *user),
4787 void *user)
4789 struct isl_sol_for *sol_for = NULL;
4790 isl_space *dom_dim;
4791 struct isl_basic_set *dom = NULL;
4793 sol_for = isl_calloc_type(bmap->ctx, struct isl_sol_for);
4794 if (!sol_for)
4795 goto error;
4797 dom_dim = isl_space_domain(isl_space_copy(bmap->dim));
4798 dom = isl_basic_set_universe(dom_dim);
4800 sol_for->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
4801 sol_for->sol.dec_level.callback.run = &sol_dec_level_wrap;
4802 sol_for->sol.dec_level.sol = &sol_for->sol;
4803 sol_for->fn = fn;
4804 sol_for->user = user;
4805 sol_for->sol.max = max;
4806 sol_for->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
4807 sol_for->sol.add = &sol_for_add_wrap;
4808 sol_for->sol.add_empty = NULL;
4809 sol_for->sol.free = &sol_for_free_wrap;
4811 sol_for->sol.context = isl_context_alloc(dom);
4812 if (!sol_for->sol.context)
4813 goto error;
4815 isl_basic_set_free(dom);
4816 return sol_for;
4817 error:
4818 isl_basic_set_free(dom);
4819 sol_for_free(sol_for);
4820 return NULL;
4823 static void sol_for_find_solutions(struct isl_sol_for *sol_for,
4824 struct isl_tab *tab)
4826 find_solutions_main(&sol_for->sol, tab);
4829 int isl_basic_map_foreach_lexopt(__isl_keep isl_basic_map *bmap, int max,
4830 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4831 void *user),
4832 void *user)
4834 struct isl_sol_for *sol_for = NULL;
4836 bmap = isl_basic_map_copy(bmap);
4837 bmap = isl_basic_map_detect_equalities(bmap);
4838 if (!bmap)
4839 return -1;
4841 sol_for = sol_for_init(bmap, max, fn, user);
4842 if (!sol_for)
4843 goto error;
4845 if (isl_basic_map_plain_is_empty(bmap))
4846 /* nothing */;
4847 else {
4848 struct isl_tab *tab;
4849 struct isl_context *context = sol_for->sol.context;
4850 tab = tab_for_lexmin(bmap,
4851 context->op->peek_basic_set(context), 1, max);
4852 tab = context->op->detect_nonnegative_parameters(context, tab);
4853 sol_for_find_solutions(sol_for, tab);
4854 if (sol_for->sol.error)
4855 goto error;
4858 sol_free(&sol_for->sol);
4859 isl_basic_map_free(bmap);
4860 return 0;
4861 error:
4862 sol_free(&sol_for->sol);
4863 isl_basic_map_free(bmap);
4864 return -1;
4867 int isl_basic_set_foreach_lexopt(__isl_keep isl_basic_set *bset, int max,
4868 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4869 void *user),
4870 void *user)
4872 return isl_basic_map_foreach_lexopt(bset, max, fn, user);
4875 /* Check if the given sequence of len variables starting at pos
4876 * represents a trivial (i.e., zero) solution.
4877 * The variables are assumed to be non-negative and to come in pairs,
4878 * with each pair representing a variable of unrestricted sign.
4879 * The solution is trivial if each such pair in the sequence consists
4880 * of two identical values, meaning that the variable being represented
4881 * has value zero.
4883 static int region_is_trivial(struct isl_tab *tab, int pos, int len)
4885 int i;
4887 if (len == 0)
4888 return 0;
4890 for (i = 0; i < len; i += 2) {
4891 int neg_row;
4892 int pos_row;
4894 neg_row = tab->var[pos + i].is_row ?
4895 tab->var[pos + i].index : -1;
4896 pos_row = tab->var[pos + i + 1].is_row ?
4897 tab->var[pos + i + 1].index : -1;
4899 if ((neg_row < 0 ||
4900 isl_int_is_zero(tab->mat->row[neg_row][1])) &&
4901 (pos_row < 0 ||
4902 isl_int_is_zero(tab->mat->row[pos_row][1])))
4903 continue;
4905 if (neg_row < 0 || pos_row < 0)
4906 return 0;
4907 if (isl_int_ne(tab->mat->row[neg_row][1],
4908 tab->mat->row[pos_row][1]))
4909 return 0;
4912 return 1;
4915 /* Return the index of the first trivial region or -1 if all regions
4916 * are non-trivial.
4918 static int first_trivial_region(struct isl_tab *tab,
4919 int n_region, struct isl_region *region)
4921 int i;
4923 for (i = 0; i < n_region; ++i) {
4924 if (region_is_trivial(tab, region[i].pos, region[i].len))
4925 return i;
4928 return -1;
4931 /* Check if the solution is optimal, i.e., whether the first
4932 * n_op entries are zero.
4934 static int is_optimal(__isl_keep isl_vec *sol, int n_op)
4936 int i;
4938 for (i = 0; i < n_op; ++i)
4939 if (!isl_int_is_zero(sol->el[1 + i]))
4940 return 0;
4941 return 1;
4944 /* Add constraints to "tab" that ensure that any solution is significantly
4945 * better than that represented by "sol". That is, find the first
4946 * relevant (within first n_op) non-zero coefficient and force it (along
4947 * with all previous coefficients) to be zero.
4948 * If the solution is already optimal (all relevant coefficients are zero),
4949 * then just mark the table as empty.
4951 * This function assumes that at least 2 * n_op more rows and at least
4952 * 2 * n_op more elements in the constraint array are available in the tableau.
4954 static int force_better_solution(struct isl_tab *tab,
4955 __isl_keep isl_vec *sol, int n_op)
4957 int i;
4958 isl_ctx *ctx;
4959 isl_vec *v = NULL;
4961 if (!sol)
4962 return -1;
4964 for (i = 0; i < n_op; ++i)
4965 if (!isl_int_is_zero(sol->el[1 + i]))
4966 break;
4968 if (i == n_op) {
4969 if (isl_tab_mark_empty(tab) < 0)
4970 return -1;
4971 return 0;
4974 ctx = isl_vec_get_ctx(sol);
4975 v = isl_vec_alloc(ctx, 1 + tab->n_var);
4976 if (!v)
4977 return -1;
4979 for (; i >= 0; --i) {
4980 v = isl_vec_clr(v);
4981 isl_int_set_si(v->el[1 + i], -1);
4982 if (add_lexmin_eq(tab, v->el) < 0)
4983 goto error;
4986 isl_vec_free(v);
4987 return 0;
4988 error:
4989 isl_vec_free(v);
4990 return -1;
4993 struct isl_trivial {
4994 int update;
4995 int region;
4996 int side;
4997 struct isl_tab_undo *snap;
5000 /* Return the lexicographically smallest non-trivial solution of the
5001 * given ILP problem.
5003 * All variables are assumed to be non-negative.
5005 * n_op is the number of initial coordinates to optimize.
5006 * That is, once a solution has been found, we will only continue looking
5007 * for solution that result in significantly better values for those
5008 * initial coordinates. That is, we only continue looking for solutions
5009 * that increase the number of initial zeros in this sequence.
5011 * A solution is non-trivial, if it is non-trivial on each of the
5012 * specified regions. Each region represents a sequence of pairs
5013 * of variables. A solution is non-trivial on such a region if
5014 * at least one of these pairs consists of different values, i.e.,
5015 * such that the non-negative variable represented by the pair is non-zero.
5017 * Whenever a conflict is encountered, all constraints involved are
5018 * reported to the caller through a call to "conflict".
5020 * We perform a simple branch-and-bound backtracking search.
5021 * Each level in the search represents initially trivial region that is forced
5022 * to be non-trivial.
5023 * At each level we consider n cases, where n is the length of the region.
5024 * In terms of the n/2 variables of unrestricted signs being encoded by
5025 * the region, we consider the cases
5026 * x_0 >= 1
5027 * x_0 <= -1
5028 * x_0 = 0 and x_1 >= 1
5029 * x_0 = 0 and x_1 <= -1
5030 * x_0 = 0 and x_1 = 0 and x_2 >= 1
5031 * x_0 = 0 and x_1 = 0 and x_2 <= -1
5032 * ...
5033 * The cases are considered in this order, assuming that each pair
5034 * x_i_a x_i_b represents the value x_i_b - x_i_a.
5035 * That is, x_0 >= 1 is enforced by adding the constraint
5036 * x_0_b - x_0_a >= 1
5038 __isl_give isl_vec *isl_tab_basic_set_non_trivial_lexmin(
5039 __isl_take isl_basic_set *bset, int n_op, int n_region,
5040 struct isl_region *region,
5041 int (*conflict)(int con, void *user), void *user)
5043 int i, j;
5044 int r;
5045 isl_ctx *ctx;
5046 isl_vec *v = NULL;
5047 isl_vec *sol = NULL;
5048 struct isl_tab *tab;
5049 struct isl_trivial *triv = NULL;
5050 int level, init;
5052 if (!bset)
5053 return NULL;
5055 ctx = isl_basic_set_get_ctx(bset);
5056 sol = isl_vec_alloc(ctx, 0);
5058 tab = tab_for_lexmin(bset, NULL, 0, 0);
5059 if (!tab)
5060 goto error;
5061 tab->conflict = conflict;
5062 tab->conflict_user = user;
5064 v = isl_vec_alloc(ctx, 1 + tab->n_var);
5065 triv = isl_calloc_array(ctx, struct isl_trivial, n_region);
5066 if (!v || (n_region && !triv))
5067 goto error;
5069 level = 0;
5070 init = 1;
5072 while (level >= 0) {
5073 int side, base;
5075 if (init) {
5076 tab = cut_to_integer_lexmin(tab, CUT_ONE);
5077 if (!tab)
5078 goto error;
5079 if (tab->empty)
5080 goto backtrack;
5081 r = first_trivial_region(tab, n_region, region);
5082 if (r < 0) {
5083 for (i = 0; i < level; ++i)
5084 triv[i].update = 1;
5085 isl_vec_free(sol);
5086 sol = isl_tab_get_sample_value(tab);
5087 if (!sol)
5088 goto error;
5089 if (is_optimal(sol, n_op))
5090 break;
5091 goto backtrack;
5093 if (level >= n_region)
5094 isl_die(ctx, isl_error_internal,
5095 "nesting level too deep", goto error);
5096 if (isl_tab_extend_cons(tab,
5097 2 * region[r].len + 2 * n_op) < 0)
5098 goto error;
5099 triv[level].region = r;
5100 triv[level].side = 0;
5103 r = triv[level].region;
5104 side = triv[level].side;
5105 base = 2 * (side/2);
5107 if (side >= region[r].len) {
5108 backtrack:
5109 level--;
5110 init = 0;
5111 if (level >= 0)
5112 if (isl_tab_rollback(tab, triv[level].snap) < 0)
5113 goto error;
5114 continue;
5117 if (triv[level].update) {
5118 if (force_better_solution(tab, sol, n_op) < 0)
5119 goto error;
5120 triv[level].update = 0;
5123 if (side == base && base >= 2) {
5124 for (j = base - 2; j < base; ++j) {
5125 v = isl_vec_clr(v);
5126 isl_int_set_si(v->el[1 + region[r].pos + j], 1);
5127 if (add_lexmin_eq(tab, v->el) < 0)
5128 goto error;
5132 triv[level].snap = isl_tab_snap(tab);
5133 if (isl_tab_push_basis(tab) < 0)
5134 goto error;
5136 v = isl_vec_clr(v);
5137 isl_int_set_si(v->el[0], -1);
5138 isl_int_set_si(v->el[1 + region[r].pos + side], -1);
5139 isl_int_set_si(v->el[1 + region[r].pos + (side ^ 1)], 1);
5140 tab = add_lexmin_ineq(tab, v->el);
5142 triv[level].side++;
5143 level++;
5144 init = 1;
5147 free(triv);
5148 isl_vec_free(v);
5149 isl_tab_free(tab);
5150 isl_basic_set_free(bset);
5152 return sol;
5153 error:
5154 free(triv);
5155 isl_vec_free(v);
5156 isl_tab_free(tab);
5157 isl_basic_set_free(bset);
5158 isl_vec_free(sol);
5159 return NULL;
5162 /* Wrapper for a tableau that is used for computing
5163 * the lexicographically smallest rational point of a non-negative set.
5164 * This point is represented by the sample value of "tab",
5165 * unless "tab" is empty.
5167 struct isl_tab_lexmin {
5168 isl_ctx *ctx;
5169 struct isl_tab *tab;
5172 /* Free "tl" and return NULL.
5174 __isl_null isl_tab_lexmin *isl_tab_lexmin_free(__isl_take isl_tab_lexmin *tl)
5176 if (!tl)
5177 return NULL;
5178 isl_ctx_deref(tl->ctx);
5179 isl_tab_free(tl->tab);
5180 free(tl);
5182 return NULL;
5185 /* Construct an isl_tab_lexmin for computing
5186 * the lexicographically smallest rational point in "bset",
5187 * assuming that all variables are non-negative.
5189 __isl_give isl_tab_lexmin *isl_tab_lexmin_from_basic_set(
5190 __isl_take isl_basic_set *bset)
5192 isl_ctx *ctx;
5193 isl_tab_lexmin *tl;
5195 if (!bset)
5196 return NULL;
5198 ctx = isl_basic_set_get_ctx(bset);
5199 tl = isl_calloc_type(ctx, struct isl_tab_lexmin);
5200 if (!tl)
5201 goto error;
5202 tl->ctx = ctx;
5203 isl_ctx_ref(ctx);
5204 tl->tab = tab_for_lexmin(bset, NULL, 0, 0);
5205 isl_basic_set_free(bset);
5206 if (!tl->tab)
5207 return isl_tab_lexmin_free(tl);
5208 return tl;
5209 error:
5210 isl_basic_set_free(bset);
5211 isl_tab_lexmin_free(tl);
5212 return NULL;
5215 /* Return the dimension of the set represented by "tl".
5217 int isl_tab_lexmin_dim(__isl_keep isl_tab_lexmin *tl)
5219 return tl ? tl->tab->n_var : -1;
5222 /* Add the equality with coefficients "eq" to "tl", updating the optimal
5223 * solution if needed.
5224 * The equality is added as two opposite inequality constraints.
5226 __isl_give isl_tab_lexmin *isl_tab_lexmin_add_eq(__isl_take isl_tab_lexmin *tl,
5227 isl_int *eq)
5229 unsigned n_var;
5231 if (!tl || !eq)
5232 return isl_tab_lexmin_free(tl);
5234 if (isl_tab_extend_cons(tl->tab, 2) < 0)
5235 return isl_tab_lexmin_free(tl);
5236 n_var = tl->tab->n_var;
5237 isl_seq_neg(eq, eq, 1 + n_var);
5238 tl->tab = add_lexmin_ineq(tl->tab, eq);
5239 isl_seq_neg(eq, eq, 1 + n_var);
5240 tl->tab = add_lexmin_ineq(tl->tab, eq);
5242 if (!tl->tab)
5243 return isl_tab_lexmin_free(tl);
5245 return tl;
5248 /* Return the lexicographically smallest rational point in the basic set
5249 * from which "tl" was constructed.
5250 * If the original input was empty, then return a zero-length vector.
5252 __isl_give isl_vec *isl_tab_lexmin_get_solution(__isl_keep isl_tab_lexmin *tl)
5254 if (!tl)
5255 return NULL;
5256 if (tl->tab->empty)
5257 return isl_vec_alloc(tl->ctx, 0);
5258 else
5259 return isl_tab_get_sample_value(tl->tab);
5262 /* Return the lexicographically smallest rational point in "bset",
5263 * assuming that all variables are non-negative.
5264 * If "bset" is empty, then return a zero-length vector.
5266 __isl_give isl_vec *isl_tab_basic_set_non_neg_lexmin(
5267 __isl_take isl_basic_set *bset)
5269 isl_tab_lexmin *tl;
5270 isl_vec *sol;
5272 tl = isl_tab_lexmin_from_basic_set(bset);
5273 sol = isl_tab_lexmin_get_solution(tl);
5274 isl_tab_lexmin_free(tl);
5275 return sol;
5278 struct isl_sol_pma {
5279 struct isl_sol sol;
5280 isl_pw_multi_aff *pma;
5281 isl_set *empty;
5284 static void sol_pma_free(struct isl_sol_pma *sol_pma)
5286 if (!sol_pma)
5287 return;
5288 if (sol_pma->sol.context)
5289 sol_pma->sol.context->op->free(sol_pma->sol.context);
5290 isl_pw_multi_aff_free(sol_pma->pma);
5291 isl_set_free(sol_pma->empty);
5292 free(sol_pma);
5295 /* This function is called for parts of the context where there is
5296 * no solution, with "bset" corresponding to the context tableau.
5297 * Simply add the basic set to the set "empty".
5299 static void sol_pma_add_empty(struct isl_sol_pma *sol,
5300 __isl_take isl_basic_set *bset)
5302 if (!bset || !sol->empty)
5303 goto error;
5305 sol->empty = isl_set_grow(sol->empty, 1);
5306 bset = isl_basic_set_simplify(bset);
5307 bset = isl_basic_set_finalize(bset);
5308 sol->empty = isl_set_add_basic_set(sol->empty, bset);
5309 if (!sol->empty)
5310 sol->sol.error = 1;
5311 return;
5312 error:
5313 isl_basic_set_free(bset);
5314 sol->sol.error = 1;
5317 /* Return the equality constraint in "bset" that defines existentially
5318 * quantified variable "pos" in terms of earlier dimensions.
5319 * The equality constraint is guaranteed to exist by the caller.
5320 * If "c" is not NULL, then it is the result of a previous call
5321 * to this function for the same variable, so simply return the input "c"
5322 * in that case.
5324 static __isl_give isl_constraint *get_equality(__isl_keep isl_basic_set *bset,
5325 int pos, __isl_take isl_constraint *c)
5327 int r;
5329 if (c)
5330 return c;
5331 r = isl_basic_set_has_defining_equality(bset, isl_dim_div, pos, &c);
5332 if (r < 0)
5333 return NULL;
5334 if (!r)
5335 isl_die(isl_basic_set_get_ctx(bset), isl_error_internal,
5336 "unexpected missing equality", return NULL);
5337 return c;
5340 /* Given a set "dom", of which only the first "n_known" existentially
5341 * quantified variables have a known explicit representation, and
5342 * a matrix "M", the rows of which are defined in terms of the dimensions
5343 * of "dom", eliminate all references to the existentially quantified
5344 * variables without a known explicit representation from "M"
5345 * by exploiting the equality constraints of "dom".
5347 * In particular, for each of those existentially quantified variables,
5348 * if there are non-zero entries in the corresponding column of "M",
5349 * then look for an equality constraint of "dom" that defines that variable
5350 * in terms of earlier variables and use it to clear the entries.
5352 * In particular, if the equality is of the form
5354 * f() + a alpha = 0
5356 * while the matrix entry is b/d (with d the global denominator of "M"),
5357 * then first scale the matrix such that the entry becomes b'/d' with
5358 * b' a multiple of a. Do this by multiplying the entire matrix
5359 * by abs(a/gcd(a,b)). Then subtract the equality multiplied by b'/a
5360 * from the row of "M" to clear the entry.
5362 static __isl_give isl_mat *eliminate_unknown_divs(__isl_take isl_mat *M,
5363 __isl_keep isl_basic_set *dom, int n_known)
5365 int i, j, n_div, off;
5366 isl_int t;
5367 isl_constraint *c = NULL;
5369 if (!M)
5370 return NULL;
5372 n_div = isl_basic_set_dim(dom, isl_dim_div);
5373 off = M->n_col - n_div;
5375 isl_int_init(t);
5376 for (i = n_div - 1; i >= n_known; --i) {
5377 for (j = 1; j < M->n_row; ++j) {
5378 if (isl_int_is_zero(M->row[j][off + i]))
5379 continue;
5380 c = get_equality(dom, i, c);
5381 if (!c)
5382 goto error;
5383 isl_int_gcd(t, M->row[j][off + i], c->v->el[off + i]);
5384 isl_int_divexact(t, c->v->el[off + i], t);
5385 isl_int_abs(t, t);
5386 M = isl_mat_scale(M, t);
5387 M = isl_mat_cow(M);
5388 if (!M)
5389 goto error;
5390 isl_int_divexact(t,
5391 M->row[j][off + i], c->v->el[off + i]);
5392 isl_seq_submul(M->row[j], t, c->v->el, M->n_col);
5394 c = isl_constraint_free(c);
5396 isl_int_clear(t);
5398 return M;
5399 error:
5400 isl_int_clear(t);
5401 isl_constraint_free(c);
5402 isl_mat_free(M);
5403 return NULL;
5406 /* Return the index of the last known div of "bset" after "start" and
5407 * up to (but not including) "end".
5408 * Return "start" if there is no such known div.
5410 static int last_known_div_after(__isl_keep isl_basic_set *bset,
5411 int start, int end)
5413 for (end = end - 1; end > start; --end) {
5414 if (isl_basic_set_div_is_known(bset, end))
5415 return end;
5418 return start;
5421 /* Set the affine expressions in "ma" according to the rows in "M", which
5422 * are defined over the local space "ls".
5423 * The matrix "M" may have extra (zero) columns beyond the number
5424 * of variables in "ls".
5426 static __isl_give isl_multi_aff *set_from_affine_matrix(
5427 __isl_take isl_multi_aff *ma, __isl_take isl_local_space *ls,
5428 __isl_take isl_mat *M)
5430 int i, dim;
5431 isl_aff *aff;
5433 if (!ma || !ls || !M)
5434 goto error;
5436 dim = isl_local_space_dim(ls, isl_dim_all);
5437 for (i = 1; i < M->n_row; ++i) {
5438 aff = isl_aff_alloc(isl_local_space_copy(ls));
5439 if (aff) {
5440 isl_int_set(aff->v->el[0], M->row[0][0]);
5441 isl_seq_cpy(aff->v->el + 1, M->row[i], 1 + dim);
5443 aff = isl_aff_normalize(aff);
5444 ma = isl_multi_aff_set_aff(ma, i - 1, aff);
5446 isl_local_space_free(ls);
5447 isl_mat_free(M);
5449 return ma;
5450 error:
5451 isl_local_space_free(ls);
5452 isl_mat_free(M);
5453 isl_multi_aff_free(ma);
5454 return NULL;
5457 /* Given a basic map "dom" that represents the context and an affine
5458 * matrix "M" that maps the dimensions of the context to the
5459 * output variables, construct an isl_pw_multi_aff with a single
5460 * cell corresponding to "dom" and affine expressions copied from "M".
5462 * Note that the description of the initial context may have involved
5463 * existentially quantified variables, in which case they also appear
5464 * in "dom". These need to be removed before creating the affine
5465 * expression because an affine expression cannot be defined in terms
5466 * of existentially quantified variables without a known representation.
5467 * In particular, they are first moved to the end in both "dom" and "M" and
5468 * then ignored in "M". In principle, the final columns of "M"
5469 * (i.e., those that will be ignored) should be zero at this stage
5470 * because align_context_divs adds the existentially quantified
5471 * variables of the context to the main tableau without any constraints.
5472 * The computed minimal value can therefore not depend on these variables.
5473 * However, additional integer divisions that get added for parametric cuts
5474 * get added to the end and they may happen to be equal to some affine
5475 * expression involving the original existentially quantified variables.
5476 * These equality constraints are then propagated to the main tableau
5477 * such that the computed minimum can in fact depend on those existentially
5478 * quantified variables. This dependence can however be removed again
5479 * by exploiting the equality constraints in "dom".
5480 * eliminate_unknown_divs takes care of this.
5482 static void sol_pma_add(struct isl_sol_pma *sol,
5483 __isl_take isl_basic_set *dom, __isl_take isl_mat *M)
5485 isl_local_space *ls;
5486 isl_multi_aff *maff;
5487 isl_pw_multi_aff *pma;
5488 int n_div, n_known, end, off;
5490 n_div = isl_basic_set_dim(dom, isl_dim_div);
5491 off = M->n_col - n_div;
5492 end = n_div;
5493 for (n_known = 0; n_known < end; ++n_known) {
5494 if (isl_basic_set_div_is_known(dom, n_known))
5495 continue;
5496 end = last_known_div_after(dom, n_known, end);
5497 if (end == n_known)
5498 break;
5499 isl_basic_set_swap_div(dom, n_known, end);
5500 M = isl_mat_swap_cols(M, off + n_known, off + end);
5502 dom = isl_basic_set_gauss(dom, NULL);
5503 if (n_known < n_div)
5504 M = eliminate_unknown_divs(M, dom, n_known);
5506 maff = isl_multi_aff_alloc(isl_pw_multi_aff_get_space(sol->pma));
5507 ls = isl_basic_set_get_local_space(dom);
5508 ls = isl_local_space_drop_dims(ls, isl_dim_div,
5509 n_known, n_div - n_known);
5510 maff = set_from_affine_matrix(maff, ls, M);
5511 dom = isl_basic_set_simplify(dom);
5512 dom = isl_basic_set_finalize(dom);
5513 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(dom), maff);
5514 sol->pma = isl_pw_multi_aff_add_disjoint(sol->pma, pma);
5515 if (!sol->pma)
5516 sol->sol.error = 1;
5519 static void sol_pma_free_wrap(struct isl_sol *sol)
5521 sol_pma_free((struct isl_sol_pma *)sol);
5524 static void sol_pma_add_empty_wrap(struct isl_sol *sol,
5525 __isl_take isl_basic_set *bset)
5527 sol_pma_add_empty((struct isl_sol_pma *)sol, bset);
5530 static void sol_pma_add_wrap(struct isl_sol *sol,
5531 __isl_take isl_basic_set *dom, __isl_take isl_mat *M)
5533 sol_pma_add((struct isl_sol_pma *)sol, dom, M);
5536 /* Construct an isl_sol_pma structure for accumulating the solution.
5537 * If track_empty is set, then we also keep track of the parts
5538 * of the context where there is no solution.
5539 * If max is set, then we are solving a maximization, rather than
5540 * a minimization problem, which means that the variables in the
5541 * tableau have value "M - x" rather than "M + x".
5543 static struct isl_sol *sol_pma_init(__isl_keep isl_basic_map *bmap,
5544 __isl_take isl_basic_set *dom, int track_empty, int max)
5546 struct isl_sol_pma *sol_pma = NULL;
5548 if (!bmap)
5549 goto error;
5551 sol_pma = isl_calloc_type(bmap->ctx, struct isl_sol_pma);
5552 if (!sol_pma)
5553 goto error;
5555 sol_pma->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
5556 sol_pma->sol.dec_level.callback.run = &sol_dec_level_wrap;
5557 sol_pma->sol.dec_level.sol = &sol_pma->sol;
5558 sol_pma->sol.max = max;
5559 sol_pma->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
5560 sol_pma->sol.add = &sol_pma_add_wrap;
5561 sol_pma->sol.add_empty = track_empty ? &sol_pma_add_empty_wrap : NULL;
5562 sol_pma->sol.free = &sol_pma_free_wrap;
5563 sol_pma->pma = isl_pw_multi_aff_empty(isl_basic_map_get_space(bmap));
5564 if (!sol_pma->pma)
5565 goto error;
5567 sol_pma->sol.context = isl_context_alloc(dom);
5568 if (!sol_pma->sol.context)
5569 goto error;
5571 if (track_empty) {
5572 sol_pma->empty = isl_set_alloc_space(isl_basic_set_get_space(dom),
5573 1, ISL_SET_DISJOINT);
5574 if (!sol_pma->empty)
5575 goto error;
5578 isl_basic_set_free(dom);
5579 return &sol_pma->sol;
5580 error:
5581 isl_basic_set_free(dom);
5582 sol_pma_free(sol_pma);
5583 return NULL;
5586 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
5587 * some obvious symmetries.
5589 * We call basic_map_partial_lexopt_base_sol and extract the results.
5591 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_base_pw_multi_aff(
5592 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5593 __isl_give isl_set **empty, int max)
5595 isl_pw_multi_aff *result = NULL;
5596 struct isl_sol *sol;
5597 struct isl_sol_pma *sol_pma;
5599 sol = basic_map_partial_lexopt_base_sol(bmap, dom, empty, max,
5600 &sol_pma_init);
5601 if (!sol)
5602 return NULL;
5603 sol_pma = (struct isl_sol_pma *) sol;
5605 result = isl_pw_multi_aff_copy(sol_pma->pma);
5606 if (empty)
5607 *empty = isl_set_copy(sol_pma->empty);
5608 sol_free(&sol_pma->sol);
5609 return result;
5612 /* Given that the last input variable of "maff" represents the minimum
5613 * of some bounds, check whether we need to plug in the expression
5614 * of the minimum.
5616 * In particular, check if the last input variable appears in any
5617 * of the expressions in "maff".
5619 static int need_substitution(__isl_keep isl_multi_aff *maff)
5621 int i;
5622 unsigned pos;
5624 pos = isl_multi_aff_dim(maff, isl_dim_in) - 1;
5626 for (i = 0; i < maff->n; ++i)
5627 if (isl_aff_involves_dims(maff->p[i], isl_dim_in, pos, 1))
5628 return 1;
5630 return 0;
5633 /* Given a set of upper bounds on the last "input" variable m,
5634 * construct a piecewise affine expression that selects
5635 * the minimal upper bound to m, i.e.,
5636 * divide the space into cells where one
5637 * of the upper bounds is smaller than all the others and select
5638 * this upper bound on that cell.
5640 * In particular, if there are n bounds b_i, then the result
5641 * consists of n cell, each one of the form
5643 * b_i <= b_j for j > i
5644 * b_i < b_j for j < i
5646 * The affine expression on this cell is
5648 * b_i
5650 static __isl_give isl_pw_aff *set_minimum_pa(__isl_take isl_space *space,
5651 __isl_take isl_mat *var)
5653 int i;
5654 isl_aff *aff = NULL;
5655 isl_basic_set *bset = NULL;
5656 isl_pw_aff *paff = NULL;
5657 isl_space *pw_space;
5658 isl_local_space *ls = NULL;
5660 if (!space || !var)
5661 goto error;
5663 ls = isl_local_space_from_space(isl_space_copy(space));
5664 pw_space = isl_space_copy(space);
5665 pw_space = isl_space_from_domain(pw_space);
5666 pw_space = isl_space_add_dims(pw_space, isl_dim_out, 1);
5667 paff = isl_pw_aff_alloc_size(pw_space, var->n_row);
5669 for (i = 0; i < var->n_row; ++i) {
5670 isl_pw_aff *paff_i;
5672 aff = isl_aff_alloc(isl_local_space_copy(ls));
5673 bset = isl_basic_set_alloc_space(isl_space_copy(space), 0,
5674 0, var->n_row - 1);
5675 if (!aff || !bset)
5676 goto error;
5677 isl_int_set_si(aff->v->el[0], 1);
5678 isl_seq_cpy(aff->v->el + 1, var->row[i], var->n_col);
5679 isl_int_set_si(aff->v->el[1 + var->n_col], 0);
5680 bset = select_minimum(bset, var, i);
5681 paff_i = isl_pw_aff_alloc(isl_set_from_basic_set(bset), aff);
5682 paff = isl_pw_aff_add_disjoint(paff, paff_i);
5685 isl_local_space_free(ls);
5686 isl_space_free(space);
5687 isl_mat_free(var);
5688 return paff;
5689 error:
5690 isl_aff_free(aff);
5691 isl_basic_set_free(bset);
5692 isl_pw_aff_free(paff);
5693 isl_local_space_free(ls);
5694 isl_space_free(space);
5695 isl_mat_free(var);
5696 return NULL;
5699 /* Given a piecewise multi-affine expression of which the last input variable
5700 * is the minimum of the bounds in "cst", plug in the value of the minimum.
5701 * This minimum expression is given in "min_expr_pa".
5702 * The set "min_expr" contains the same information, but in the form of a set.
5703 * The variable is subsequently projected out.
5705 * The implementation is similar to those of "split" and "split_domain".
5706 * If the variable appears in a given expression, then minimum expression
5707 * is plugged in. Otherwise, if the variable appears in the constraints
5708 * and a split is required, then the domain is split. Otherwise, no split
5709 * is performed.
5711 static __isl_give isl_pw_multi_aff *split_domain_pma(
5712 __isl_take isl_pw_multi_aff *opt, __isl_take isl_pw_aff *min_expr_pa,
5713 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
5715 int n_in;
5716 int i;
5717 isl_space *space;
5718 isl_pw_multi_aff *res;
5720 if (!opt || !min_expr || !cst)
5721 goto error;
5723 n_in = isl_pw_multi_aff_dim(opt, isl_dim_in);
5724 space = isl_pw_multi_aff_get_space(opt);
5725 space = isl_space_drop_dims(space, isl_dim_in, n_in - 1, 1);
5726 res = isl_pw_multi_aff_empty(space);
5728 for (i = 0; i < opt->n; ++i) {
5729 isl_pw_multi_aff *pma;
5731 pma = isl_pw_multi_aff_alloc(isl_set_copy(opt->p[i].set),
5732 isl_multi_aff_copy(opt->p[i].maff));
5733 if (need_substitution(opt->p[i].maff))
5734 pma = isl_pw_multi_aff_substitute(pma,
5735 isl_dim_in, n_in - 1, min_expr_pa);
5736 else if (need_split_set(opt->p[i].set, cst))
5737 pma = isl_pw_multi_aff_intersect_domain(pma,
5738 isl_set_copy(min_expr));
5739 pma = isl_pw_multi_aff_project_out(pma,
5740 isl_dim_in, n_in - 1, 1);
5742 res = isl_pw_multi_aff_add_disjoint(res, pma);
5745 isl_pw_multi_aff_free(opt);
5746 isl_pw_aff_free(min_expr_pa);
5747 isl_set_free(min_expr);
5748 isl_mat_free(cst);
5749 return res;
5750 error:
5751 isl_pw_multi_aff_free(opt);
5752 isl_pw_aff_free(min_expr_pa);
5753 isl_set_free(min_expr);
5754 isl_mat_free(cst);
5755 return NULL;
5758 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_pw_multi_aff(
5759 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5760 __isl_give isl_set **empty, int max);
5762 /* This function is called from basic_map_partial_lexopt_symm.
5763 * The last variable of "bmap" and "dom" corresponds to the minimum
5764 * of the bounds in "cst". "map_space" is the space of the original
5765 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
5766 * is the space of the original domain.
5768 * We recursively call basic_map_partial_lexopt and then plug in
5769 * the definition of the minimum in the result.
5771 static __isl_give isl_pw_multi_aff *
5772 basic_map_partial_lexopt_symm_core_pw_multi_aff(
5773 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5774 __isl_give isl_set **empty, int max, __isl_take isl_mat *cst,
5775 __isl_take isl_space *map_space, __isl_take isl_space *set_space)
5777 isl_pw_multi_aff *opt;
5778 isl_pw_aff *min_expr_pa;
5779 isl_set *min_expr;
5781 min_expr = set_minimum(isl_basic_set_get_space(dom), isl_mat_copy(cst));
5782 min_expr_pa = set_minimum_pa(isl_basic_set_get_space(dom),
5783 isl_mat_copy(cst));
5785 opt = basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, max);
5787 if (empty) {
5788 *empty = split(*empty,
5789 isl_set_copy(min_expr), isl_mat_copy(cst));
5790 *empty = isl_set_reset_space(*empty, set_space);
5793 opt = split_domain_pma(opt, min_expr_pa, min_expr, cst);
5794 opt = isl_pw_multi_aff_reset_space(opt, map_space);
5796 return opt;
5799 #undef TYPE
5800 #define TYPE isl_pw_multi_aff
5801 #undef SUFFIX
5802 #define SUFFIX _pw_multi_aff
5803 #include "isl_tab_lexopt_templ.c"