isl_tab_pip.c: isl_context_op.add_div: add memory management annotation
[isl.git] / isl_tab_pip.c
blobc3849e3ae273cdc8b296b4056a3a11ee11d165da
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 int (*add_div)(struct isl_context *context, __isl_keep isl_vec *div);
96 int (*detect_equalities)(struct isl_context *context,
97 struct isl_tab *tab);
98 /* return row index of "best" split */
99 int (*best_split)(struct isl_context *context, struct isl_tab *tab);
100 /* check if context has already been determined to be empty */
101 int (*is_empty)(struct isl_context *context);
102 /* check if context is still usable */
103 int (*is_ok)(struct isl_context *context);
104 /* save a copy/snapshot of context */
105 void *(*save)(struct isl_context *context);
106 /* restore saved context */
107 void (*restore)(struct isl_context *context, void *);
108 /* discard saved context */
109 void (*discard)(void *);
110 /* invalidate context */
111 void (*invalidate)(struct isl_context *context);
112 /* free context */
113 void (*free)(struct isl_context *context);
116 struct isl_context {
117 struct isl_context_op *op;
120 struct isl_context_lex {
121 struct isl_context context;
122 struct isl_tab *tab;
125 /* A stack (linked list) of solutions of subtrees of the search space.
127 * "M" describes the solution in terms of the dimensions of "dom".
128 * The number of columns of "M" is one more than the total number
129 * of dimensions of "dom".
131 * If "M" is NULL, then there is no solution on "dom".
133 struct isl_partial_sol {
134 int level;
135 struct isl_basic_set *dom;
136 struct isl_mat *M;
138 struct isl_partial_sol *next;
141 struct isl_sol;
142 struct isl_sol_callback {
143 struct isl_tab_callback callback;
144 struct isl_sol *sol;
147 /* isl_sol is an interface for constructing a solution to
148 * a parametric integer linear programming problem.
149 * Every time the algorithm reaches a state where a solution
150 * can be read off from the tableau (including cases where the tableau
151 * is empty), the function "add" is called on the isl_sol passed
152 * to find_solutions_main.
154 * The context tableau is owned by isl_sol and is updated incrementally.
156 * There are currently two implementations of this interface,
157 * isl_sol_map, which simply collects the solutions in an isl_map
158 * and (optionally) the parts of the context where there is no solution
159 * in an isl_set, and
160 * isl_sol_for, which calls a user-defined function for each part of
161 * the solution.
163 struct isl_sol {
164 int error;
165 int rational;
166 int level;
167 int max;
168 int n_out;
169 struct isl_context *context;
170 struct isl_partial_sol *partial;
171 void (*add)(struct isl_sol *sol,
172 struct isl_basic_set *dom, struct isl_mat *M);
173 void (*add_empty)(struct isl_sol *sol, struct isl_basic_set *bset);
174 void (*free)(struct isl_sol *sol);
175 struct isl_sol_callback dec_level;
178 static void sol_free(struct isl_sol *sol)
180 struct isl_partial_sol *partial, *next;
181 if (!sol)
182 return;
183 for (partial = sol->partial; partial; partial = next) {
184 next = partial->next;
185 isl_basic_set_free(partial->dom);
186 isl_mat_free(partial->M);
187 free(partial);
189 sol->free(sol);
192 /* Push a partial solution represented by a domain and mapping M
193 * onto the stack of partial solutions.
195 static void sol_push_sol(struct isl_sol *sol,
196 struct isl_basic_set *dom, struct isl_mat *M)
198 struct isl_partial_sol *partial;
200 if (sol->error || !dom)
201 goto error;
203 partial = isl_alloc_type(dom->ctx, struct isl_partial_sol);
204 if (!partial)
205 goto error;
207 partial->level = sol->level;
208 partial->dom = dom;
209 partial->M = M;
210 partial->next = sol->partial;
212 sol->partial = partial;
214 return;
215 error:
216 isl_basic_set_free(dom);
217 isl_mat_free(M);
218 sol->error = 1;
221 /* Pop one partial solution from the partial solution stack and
222 * pass it on to sol->add or sol->add_empty.
224 static void sol_pop_one(struct isl_sol *sol)
226 struct isl_partial_sol *partial;
228 partial = sol->partial;
229 sol->partial = partial->next;
231 if (partial->M)
232 sol->add(sol, partial->dom, partial->M);
233 else
234 sol->add_empty(sol, partial->dom);
235 free(partial);
238 /* Return a fresh copy of the domain represented by the context tableau.
240 static struct isl_basic_set *sol_domain(struct isl_sol *sol)
242 struct isl_basic_set *bset;
244 if (sol->error)
245 return NULL;
247 bset = isl_basic_set_dup(sol->context->op->peek_basic_set(sol->context));
248 bset = isl_basic_set_update_from_tab(bset,
249 sol->context->op->peek_tab(sol->context));
251 return bset;
254 /* Check whether two partial solutions have the same mapping, where n_div
255 * is the number of divs that the two partial solutions have in common.
257 static int same_solution(struct isl_partial_sol *s1, struct isl_partial_sol *s2,
258 unsigned n_div)
260 int i;
261 unsigned dim;
263 if (!s1->M != !s2->M)
264 return 0;
265 if (!s1->M)
266 return 1;
268 dim = isl_basic_set_total_dim(s1->dom) - s1->dom->n_div;
270 for (i = 0; i < s1->M->n_row; ++i) {
271 if (isl_seq_first_non_zero(s1->M->row[i]+1+dim+n_div,
272 s1->M->n_col-1-dim-n_div) != -1)
273 return 0;
274 if (isl_seq_first_non_zero(s2->M->row[i]+1+dim+n_div,
275 s2->M->n_col-1-dim-n_div) != -1)
276 return 0;
277 if (!isl_seq_eq(s1->M->row[i], s2->M->row[i], 1+dim+n_div))
278 return 0;
280 return 1;
283 /* Pop all solutions from the partial solution stack that were pushed onto
284 * the stack at levels that are deeper than the current level.
285 * If the two topmost elements on the stack have the same level
286 * and represent the same solution, then their domains are combined.
287 * This combined domain is the same as the current context domain
288 * as sol_pop is called each time we move back to a higher level.
289 * If the outer level (0) has been reached, then all partial solutions
290 * at the current level are also popped off.
292 static void sol_pop(struct isl_sol *sol)
294 struct isl_partial_sol *partial;
295 unsigned n_div;
297 if (sol->error)
298 return;
300 partial = sol->partial;
301 if (!partial)
302 return;
304 if (partial->level == 0 && sol->level == 0) {
305 for (partial = sol->partial; partial; partial = sol->partial)
306 sol_pop_one(sol);
307 return;
310 if (partial->level <= sol->level)
311 return;
313 if (partial->next && partial->next->level == partial->level) {
314 n_div = isl_basic_set_dim(
315 sol->context->op->peek_basic_set(sol->context),
316 isl_dim_div);
318 if (!same_solution(partial, partial->next, n_div)) {
319 sol_pop_one(sol);
320 sol_pop_one(sol);
321 } else {
322 struct isl_basic_set *bset;
323 isl_mat *M;
324 unsigned n;
326 n = isl_basic_set_dim(partial->next->dom, isl_dim_div);
327 n -= n_div;
328 bset = sol_domain(sol);
329 isl_basic_set_free(partial->next->dom);
330 partial->next->dom = bset;
331 M = partial->next->M;
332 if (M) {
333 M = isl_mat_drop_cols(M, M->n_col - n, n);
334 partial->next->M = M;
335 if (!M)
336 goto error;
338 partial->next->level = sol->level;
340 if (!bset)
341 goto error;
343 sol->partial = partial->next;
344 isl_basic_set_free(partial->dom);
345 isl_mat_free(partial->M);
346 free(partial);
348 } else
349 sol_pop_one(sol);
351 if (sol->level == 0) {
352 for (partial = sol->partial; partial; partial = sol->partial)
353 sol_pop_one(sol);
354 return;
357 if (0)
358 error: sol->error = 1;
361 static void sol_dec_level(struct isl_sol *sol)
363 if (sol->error)
364 return;
366 sol->level--;
368 sol_pop(sol);
371 static int sol_dec_level_wrap(struct isl_tab_callback *cb)
373 struct isl_sol_callback *callback = (struct isl_sol_callback *)cb;
375 sol_dec_level(callback->sol);
377 return callback->sol->error ? -1 : 0;
380 /* Move down to next level and push callback onto context tableau
381 * to decrease the level again when it gets rolled back across
382 * the current state. That is, dec_level will be called with
383 * the context tableau in the same state as it is when inc_level
384 * is called.
386 static void sol_inc_level(struct isl_sol *sol)
388 struct isl_tab *tab;
390 if (sol->error)
391 return;
393 sol->level++;
394 tab = sol->context->op->peek_tab(sol->context);
395 if (isl_tab_push_callback(tab, &sol->dec_level.callback) < 0)
396 sol->error = 1;
399 static void scale_rows(struct isl_mat *mat, isl_int m, int n_row)
401 int i;
403 if (isl_int_is_one(m))
404 return;
406 for (i = 0; i < n_row; ++i)
407 isl_seq_scale(mat->row[i], mat->row[i], m, mat->n_col);
410 /* Add the solution identified by the tableau and the context tableau.
412 * The layout of the variables is as follows.
413 * tab->n_var is equal to the total number of variables in the input
414 * map (including divs that were copied from the context)
415 * + the number of extra divs constructed
416 * Of these, the first tab->n_param and the last tab->n_div variables
417 * correspond to the variables in the context, i.e.,
418 * tab->n_param + tab->n_div = context_tab->n_var
419 * tab->n_param is equal to the number of parameters and input
420 * dimensions in the input map
421 * tab->n_div is equal to the number of divs in the context
423 * If there is no solution, then call add_empty with a basic set
424 * that corresponds to the context tableau. (If add_empty is NULL,
425 * then do nothing).
427 * If there is a solution, then first construct a matrix that maps
428 * all dimensions of the context to the output variables, i.e.,
429 * the output dimensions in the input map.
430 * The divs in the input map (if any) that do not correspond to any
431 * div in the context do not appear in the solution.
432 * The algorithm will make sure that they have an integer value,
433 * but these values themselves are of no interest.
434 * We have to be careful not to drop or rearrange any divs in the
435 * context because that would change the meaning of the matrix.
437 * To extract the value of the output variables, it should be noted
438 * that we always use a big parameter M in the main tableau and so
439 * the variable stored in this tableau is not an output variable x itself, but
440 * x' = M + x (in case of minimization)
441 * or
442 * x' = M - x (in case of maximization)
443 * If x' appears in a column, then its optimal value is zero,
444 * which means that the optimal value of x is an unbounded number
445 * (-M for minimization and M for maximization).
446 * We currently assume that the output dimensions in the original map
447 * are bounded, so this cannot occur.
448 * Similarly, when x' appears in a row, then the coefficient of M in that
449 * row is necessarily 1.
450 * If the row in the tableau represents
451 * d x' = c + d M + e(y)
452 * then, in case of minimization, the corresponding row in the matrix
453 * will be
454 * a c + a e(y)
455 * with a d = m, the (updated) common denominator of the matrix.
456 * In case of maximization, the row will be
457 * -a c - a e(y)
459 static void sol_add(struct isl_sol *sol, struct isl_tab *tab)
461 struct isl_basic_set *bset = NULL;
462 struct isl_mat *mat = NULL;
463 unsigned off;
464 int row;
465 isl_int m;
467 if (sol->error || !tab)
468 goto error;
470 if (tab->empty && !sol->add_empty)
471 return;
472 if (sol->context->op->is_empty(sol->context))
473 return;
475 bset = sol_domain(sol);
477 if (tab->empty) {
478 sol_push_sol(sol, bset, NULL);
479 return;
482 off = 2 + tab->M;
484 mat = isl_mat_alloc(tab->mat->ctx, 1 + sol->n_out,
485 1 + tab->n_param + tab->n_div);
486 if (!mat)
487 goto error;
489 isl_int_init(m);
491 isl_seq_clr(mat->row[0] + 1, mat->n_col - 1);
492 isl_int_set_si(mat->row[0][0], 1);
493 for (row = 0; row < sol->n_out; ++row) {
494 int i = tab->n_param + row;
495 int r, j;
497 isl_seq_clr(mat->row[1 + row], mat->n_col);
498 if (!tab->var[i].is_row) {
499 if (tab->M)
500 isl_die(mat->ctx, isl_error_invalid,
501 "unbounded optimum", goto error2);
502 continue;
505 r = tab->var[i].index;
506 if (tab->M &&
507 isl_int_ne(tab->mat->row[r][2], tab->mat->row[r][0]))
508 isl_die(mat->ctx, isl_error_invalid,
509 "unbounded optimum", goto error2);
510 isl_int_gcd(m, mat->row[0][0], tab->mat->row[r][0]);
511 isl_int_divexact(m, tab->mat->row[r][0], m);
512 scale_rows(mat, m, 1 + row);
513 isl_int_divexact(m, mat->row[0][0], tab->mat->row[r][0]);
514 isl_int_mul(mat->row[1 + row][0], m, tab->mat->row[r][1]);
515 for (j = 0; j < tab->n_param; ++j) {
516 int col;
517 if (tab->var[j].is_row)
518 continue;
519 col = tab->var[j].index;
520 isl_int_mul(mat->row[1 + row][1 + j], m,
521 tab->mat->row[r][off + col]);
523 for (j = 0; j < tab->n_div; ++j) {
524 int col;
525 if (tab->var[tab->n_var - tab->n_div+j].is_row)
526 continue;
527 col = tab->var[tab->n_var - tab->n_div+j].index;
528 isl_int_mul(mat->row[1 + row][1 + tab->n_param + j], m,
529 tab->mat->row[r][off + col]);
531 if (sol->max)
532 isl_seq_neg(mat->row[1 + row], mat->row[1 + row],
533 mat->n_col);
536 isl_int_clear(m);
538 sol_push_sol(sol, bset, mat);
539 return;
540 error2:
541 isl_int_clear(m);
542 error:
543 isl_basic_set_free(bset);
544 isl_mat_free(mat);
545 sol->error = 1;
548 struct isl_sol_map {
549 struct isl_sol sol;
550 struct isl_map *map;
551 struct isl_set *empty;
554 static void sol_map_free(struct isl_sol_map *sol_map)
556 if (!sol_map)
557 return;
558 if (sol_map->sol.context)
559 sol_map->sol.context->op->free(sol_map->sol.context);
560 isl_map_free(sol_map->map);
561 isl_set_free(sol_map->empty);
562 free(sol_map);
565 static void sol_map_free_wrap(struct isl_sol *sol)
567 sol_map_free((struct isl_sol_map *)sol);
570 /* This function is called for parts of the context where there is
571 * no solution, with "bset" corresponding to the context tableau.
572 * Simply add the basic set to the set "empty".
574 static void sol_map_add_empty(struct isl_sol_map *sol,
575 struct isl_basic_set *bset)
577 if (!bset || !sol->empty)
578 goto error;
580 sol->empty = isl_set_grow(sol->empty, 1);
581 bset = isl_basic_set_simplify(bset);
582 bset = isl_basic_set_finalize(bset);
583 sol->empty = isl_set_add_basic_set(sol->empty, isl_basic_set_copy(bset));
584 if (!sol->empty)
585 goto error;
586 isl_basic_set_free(bset);
587 return;
588 error:
589 isl_basic_set_free(bset);
590 sol->sol.error = 1;
593 static void sol_map_add_empty_wrap(struct isl_sol *sol,
594 struct isl_basic_set *bset)
596 sol_map_add_empty((struct isl_sol_map *)sol, bset);
599 /* Given a basic map "dom" that represents the context and an affine
600 * matrix "M" that maps the dimensions of the context to the
601 * output variables, construct a basic map with the same parameters
602 * and divs as the context, the dimensions of the context as input
603 * dimensions and a number of output dimensions that is equal to
604 * the number of output dimensions in the input map.
606 * The constraints and divs of the context are simply copied
607 * from "dom". For each row
608 * x = c + e(y)
609 * an equality
610 * c + e(y) - d x = 0
611 * is added, with d the common denominator of M.
613 static void sol_map_add(struct isl_sol_map *sol,
614 struct isl_basic_set *dom, struct isl_mat *M)
616 int i;
617 struct isl_basic_map *bmap = NULL;
618 unsigned n_eq;
619 unsigned n_ineq;
620 unsigned nparam;
621 unsigned total;
622 unsigned n_div;
623 unsigned n_out;
625 if (sol->sol.error || !dom || !M)
626 goto error;
628 n_out = sol->sol.n_out;
629 n_eq = dom->n_eq + n_out;
630 n_ineq = dom->n_ineq;
631 n_div = dom->n_div;
632 nparam = isl_basic_set_total_dim(dom) - n_div;
633 total = isl_map_dim(sol->map, isl_dim_all);
634 bmap = isl_basic_map_alloc_space(isl_map_get_space(sol->map),
635 n_div, n_eq, 2 * n_div + n_ineq);
636 if (!bmap)
637 goto error;
638 if (sol->sol.rational)
639 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
640 for (i = 0; i < dom->n_div; ++i) {
641 int k = isl_basic_map_alloc_div(bmap);
642 if (k < 0)
643 goto error;
644 isl_seq_cpy(bmap->div[k], dom->div[i], 1 + 1 + nparam);
645 isl_seq_clr(bmap->div[k] + 1 + 1 + nparam, total - nparam);
646 isl_seq_cpy(bmap->div[k] + 1 + 1 + total,
647 dom->div[i] + 1 + 1 + nparam, i);
649 for (i = 0; i < dom->n_eq; ++i) {
650 int k = isl_basic_map_alloc_equality(bmap);
651 if (k < 0)
652 goto error;
653 isl_seq_cpy(bmap->eq[k], dom->eq[i], 1 + nparam);
654 isl_seq_clr(bmap->eq[k] + 1 + nparam, total - nparam);
655 isl_seq_cpy(bmap->eq[k] + 1 + total,
656 dom->eq[i] + 1 + nparam, n_div);
658 for (i = 0; i < dom->n_ineq; ++i) {
659 int k = isl_basic_map_alloc_inequality(bmap);
660 if (k < 0)
661 goto error;
662 isl_seq_cpy(bmap->ineq[k], dom->ineq[i], 1 + nparam);
663 isl_seq_clr(bmap->ineq[k] + 1 + nparam, total - nparam);
664 isl_seq_cpy(bmap->ineq[k] + 1 + total,
665 dom->ineq[i] + 1 + nparam, n_div);
667 for (i = 0; i < M->n_row - 1; ++i) {
668 int k = isl_basic_map_alloc_equality(bmap);
669 if (k < 0)
670 goto error;
671 isl_seq_cpy(bmap->eq[k], M->row[1 + i], 1 + nparam);
672 isl_seq_clr(bmap->eq[k] + 1 + nparam, n_out);
673 isl_int_neg(bmap->eq[k][1 + nparam + i], M->row[0][0]);
674 isl_seq_cpy(bmap->eq[k] + 1 + nparam + n_out,
675 M->row[1 + i] + 1 + nparam, n_div);
677 bmap = isl_basic_map_simplify(bmap);
678 bmap = isl_basic_map_finalize(bmap);
679 sol->map = isl_map_grow(sol->map, 1);
680 sol->map = isl_map_add_basic_map(sol->map, bmap);
681 isl_basic_set_free(dom);
682 isl_mat_free(M);
683 if (!sol->map)
684 sol->sol.error = 1;
685 return;
686 error:
687 isl_basic_set_free(dom);
688 isl_mat_free(M);
689 isl_basic_map_free(bmap);
690 sol->sol.error = 1;
693 static void sol_map_add_wrap(struct isl_sol *sol,
694 struct isl_basic_set *dom, struct isl_mat *M)
696 sol_map_add((struct isl_sol_map *)sol, dom, M);
700 /* Store the "parametric constant" of row "row" of tableau "tab" in "line",
701 * i.e., the constant term and the coefficients of all variables that
702 * appear in the context tableau.
703 * Note that the coefficient of the big parameter M is NOT copied.
704 * The context tableau may not have a big parameter and even when it
705 * does, it is a different big parameter.
707 static void get_row_parameter_line(struct isl_tab *tab, int row, isl_int *line)
709 int i;
710 unsigned off = 2 + tab->M;
712 isl_int_set(line[0], tab->mat->row[row][1]);
713 for (i = 0; i < tab->n_param; ++i) {
714 if (tab->var[i].is_row)
715 isl_int_set_si(line[1 + i], 0);
716 else {
717 int col = tab->var[i].index;
718 isl_int_set(line[1 + i], tab->mat->row[row][off + col]);
721 for (i = 0; i < tab->n_div; ++i) {
722 if (tab->var[tab->n_var - tab->n_div + i].is_row)
723 isl_int_set_si(line[1 + tab->n_param + i], 0);
724 else {
725 int col = tab->var[tab->n_var - tab->n_div + i].index;
726 isl_int_set(line[1 + tab->n_param + i],
727 tab->mat->row[row][off + col]);
732 /* Check if rows "row1" and "row2" have identical "parametric constants",
733 * as explained above.
734 * In this case, we also insist that the coefficients of the big parameter
735 * be the same as the values of the constants will only be the same
736 * if these coefficients are also the same.
738 static int identical_parameter_line(struct isl_tab *tab, int row1, int row2)
740 int i;
741 unsigned off = 2 + tab->M;
743 if (isl_int_ne(tab->mat->row[row1][1], tab->mat->row[row2][1]))
744 return 0;
746 if (tab->M && isl_int_ne(tab->mat->row[row1][2],
747 tab->mat->row[row2][2]))
748 return 0;
750 for (i = 0; i < tab->n_param + tab->n_div; ++i) {
751 int pos = i < tab->n_param ? i :
752 tab->n_var - tab->n_div + i - tab->n_param;
753 int col;
755 if (tab->var[pos].is_row)
756 continue;
757 col = tab->var[pos].index;
758 if (isl_int_ne(tab->mat->row[row1][off + col],
759 tab->mat->row[row2][off + col]))
760 return 0;
762 return 1;
765 /* Return an inequality that expresses that the "parametric constant"
766 * should be non-negative.
767 * This function is only called when the coefficient of the big parameter
768 * is equal to zero.
770 static struct isl_vec *get_row_parameter_ineq(struct isl_tab *tab, int row)
772 struct isl_vec *ineq;
774 ineq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_param + tab->n_div);
775 if (!ineq)
776 return NULL;
778 get_row_parameter_line(tab, row, ineq->el);
779 if (ineq)
780 ineq = isl_vec_normalize(ineq);
782 return ineq;
785 /* Normalize a div expression of the form
787 * [(g*f(x) + c)/(g * m)]
789 * with c the constant term and f(x) the remaining coefficients, to
791 * [(f(x) + [c/g])/m]
793 static void normalize_div(__isl_keep isl_vec *div)
795 isl_ctx *ctx = isl_vec_get_ctx(div);
796 int len = div->size - 2;
798 isl_seq_gcd(div->el + 2, len, &ctx->normalize_gcd);
799 isl_int_gcd(ctx->normalize_gcd, ctx->normalize_gcd, div->el[0]);
801 if (isl_int_is_one(ctx->normalize_gcd))
802 return;
804 isl_int_divexact(div->el[0], div->el[0], ctx->normalize_gcd);
805 isl_int_fdiv_q(div->el[1], div->el[1], ctx->normalize_gcd);
806 isl_seq_scale_down(div->el + 2, div->el + 2, ctx->normalize_gcd, len);
809 /* Return a integer division for use in a parametric cut based on the given row.
810 * In particular, let the parametric constant of the row be
812 * \sum_i a_i y_i
814 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
815 * The div returned is equal to
817 * floor(\sum_i {-a_i} y_i) = floor((\sum_i (-a_i mod d) y_i)/d)
819 static struct isl_vec *get_row_parameter_div(struct isl_tab *tab, int row)
821 struct isl_vec *div;
823 div = isl_vec_alloc(tab->mat->ctx, 1 + 1 + tab->n_param + tab->n_div);
824 if (!div)
825 return NULL;
827 isl_int_set(div->el[0], tab->mat->row[row][0]);
828 get_row_parameter_line(tab, row, div->el + 1);
829 isl_seq_neg(div->el + 1, div->el + 1, div->size - 1);
830 normalize_div(div);
831 isl_seq_fdiv_r(div->el + 1, div->el + 1, div->el[0], div->size - 1);
833 return div;
836 /* Return a integer division for use in transferring an integrality constraint
837 * to the context.
838 * In particular, let the parametric constant of the row be
840 * \sum_i a_i y_i
842 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
843 * The the returned div is equal to
845 * floor(\sum_i {a_i} y_i) = floor((\sum_i (a_i mod d) y_i)/d)
847 static struct isl_vec *get_row_split_div(struct isl_tab *tab, int row)
849 struct isl_vec *div;
851 div = isl_vec_alloc(tab->mat->ctx, 1 + 1 + tab->n_param + tab->n_div);
852 if (!div)
853 return NULL;
855 isl_int_set(div->el[0], tab->mat->row[row][0]);
856 get_row_parameter_line(tab, row, div->el + 1);
857 normalize_div(div);
858 isl_seq_fdiv_r(div->el + 1, div->el + 1, div->el[0], div->size - 1);
860 return div;
863 /* Construct and return an inequality that expresses an upper bound
864 * on the given div.
865 * In particular, if the div is given by
867 * d = floor(e/m)
869 * then the inequality expresses
871 * m d <= e
873 static struct isl_vec *ineq_for_div(struct isl_basic_set *bset, unsigned div)
875 unsigned total;
876 unsigned div_pos;
877 struct isl_vec *ineq;
879 if (!bset)
880 return NULL;
882 total = isl_basic_set_total_dim(bset);
883 div_pos = 1 + total - bset->n_div + div;
885 ineq = isl_vec_alloc(bset->ctx, 1 + total);
886 if (!ineq)
887 return NULL;
889 isl_seq_cpy(ineq->el, bset->div[div] + 1, 1 + total);
890 isl_int_neg(ineq->el[div_pos], bset->div[div][0]);
891 return ineq;
894 /* Given a row in the tableau and a div that was created
895 * using get_row_split_div and that has been constrained to equality, i.e.,
897 * d = floor(\sum_i {a_i} y_i) = \sum_i {a_i} y_i
899 * replace the expression "\sum_i {a_i} y_i" in the row by d,
900 * i.e., we subtract "\sum_i {a_i} y_i" and add 1 d.
901 * The coefficients of the non-parameters in the tableau have been
902 * verified to be integral. We can therefore simply replace coefficient b
903 * by floor(b). For the coefficients of the parameters we have
904 * floor(a_i) = a_i - {a_i}, while for the other coefficients, we have
905 * floor(b) = b.
907 static struct isl_tab *set_row_cst_to_div(struct isl_tab *tab, int row, int div)
909 isl_seq_fdiv_q(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
910 tab->mat->row[row][0], 1 + tab->M + tab->n_col);
912 isl_int_set_si(tab->mat->row[row][0], 1);
914 if (tab->var[tab->n_var - tab->n_div + div].is_row) {
915 int drow = tab->var[tab->n_var - tab->n_div + div].index;
917 isl_assert(tab->mat->ctx,
918 isl_int_is_one(tab->mat->row[drow][0]), goto error);
919 isl_seq_combine(tab->mat->row[row] + 1,
920 tab->mat->ctx->one, tab->mat->row[row] + 1,
921 tab->mat->ctx->one, tab->mat->row[drow] + 1,
922 1 + tab->M + tab->n_col);
923 } else {
924 int dcol = tab->var[tab->n_var - tab->n_div + div].index;
926 isl_int_add_ui(tab->mat->row[row][2 + tab->M + dcol],
927 tab->mat->row[row][2 + tab->M + dcol], 1);
930 return tab;
931 error:
932 isl_tab_free(tab);
933 return NULL;
936 /* Check if the (parametric) constant of the given row is obviously
937 * negative, meaning that we don't need to consult the context tableau.
938 * If there is a big parameter and its coefficient is non-zero,
939 * then this coefficient determines the outcome.
940 * Otherwise, we check whether the constant is negative and
941 * all non-zero coefficients of parameters are negative and
942 * belong to non-negative parameters.
944 static int is_obviously_neg(struct isl_tab *tab, int row)
946 int i;
947 int col;
948 unsigned off = 2 + tab->M;
950 if (tab->M) {
951 if (isl_int_is_pos(tab->mat->row[row][2]))
952 return 0;
953 if (isl_int_is_neg(tab->mat->row[row][2]))
954 return 1;
957 if (isl_int_is_nonneg(tab->mat->row[row][1]))
958 return 0;
959 for (i = 0; i < tab->n_param; ++i) {
960 /* Eliminated parameter */
961 if (tab->var[i].is_row)
962 continue;
963 col = tab->var[i].index;
964 if (isl_int_is_zero(tab->mat->row[row][off + col]))
965 continue;
966 if (!tab->var[i].is_nonneg)
967 return 0;
968 if (isl_int_is_pos(tab->mat->row[row][off + col]))
969 return 0;
971 for (i = 0; i < tab->n_div; ++i) {
972 if (tab->var[tab->n_var - tab->n_div + i].is_row)
973 continue;
974 col = tab->var[tab->n_var - tab->n_div + i].index;
975 if (isl_int_is_zero(tab->mat->row[row][off + col]))
976 continue;
977 if (!tab->var[tab->n_var - tab->n_div + i].is_nonneg)
978 return 0;
979 if (isl_int_is_pos(tab->mat->row[row][off + col]))
980 return 0;
982 return 1;
985 /* Check if the (parametric) constant of the given row is obviously
986 * non-negative, meaning that we don't need to consult the context tableau.
987 * If there is a big parameter and its coefficient is non-zero,
988 * then this coefficient determines the outcome.
989 * Otherwise, we check whether the constant is non-negative and
990 * all non-zero coefficients of parameters are positive and
991 * belong to non-negative parameters.
993 static int is_obviously_nonneg(struct isl_tab *tab, int row)
995 int i;
996 int col;
997 unsigned off = 2 + tab->M;
999 if (tab->M) {
1000 if (isl_int_is_pos(tab->mat->row[row][2]))
1001 return 1;
1002 if (isl_int_is_neg(tab->mat->row[row][2]))
1003 return 0;
1006 if (isl_int_is_neg(tab->mat->row[row][1]))
1007 return 0;
1008 for (i = 0; i < tab->n_param; ++i) {
1009 /* Eliminated parameter */
1010 if (tab->var[i].is_row)
1011 continue;
1012 col = tab->var[i].index;
1013 if (isl_int_is_zero(tab->mat->row[row][off + col]))
1014 continue;
1015 if (!tab->var[i].is_nonneg)
1016 return 0;
1017 if (isl_int_is_neg(tab->mat->row[row][off + col]))
1018 return 0;
1020 for (i = 0; i < tab->n_div; ++i) {
1021 if (tab->var[tab->n_var - tab->n_div + i].is_row)
1022 continue;
1023 col = tab->var[tab->n_var - tab->n_div + i].index;
1024 if (isl_int_is_zero(tab->mat->row[row][off + col]))
1025 continue;
1026 if (!tab->var[tab->n_var - tab->n_div + i].is_nonneg)
1027 return 0;
1028 if (isl_int_is_neg(tab->mat->row[row][off + col]))
1029 return 0;
1031 return 1;
1034 /* Given a row r and two columns, return the column that would
1035 * lead to the lexicographically smallest increment in the sample
1036 * solution when leaving the basis in favor of the row.
1037 * Pivoting with column c will increment the sample value by a non-negative
1038 * constant times a_{V,c}/a_{r,c}, with a_{V,c} the elements of column c
1039 * corresponding to the non-parametric variables.
1040 * If variable v appears in a column c_v, the a_{v,c} = 1 iff c = c_v,
1041 * with all other entries in this virtual row equal to zero.
1042 * If variable v appears in a row, then a_{v,c} is the element in column c
1043 * of that row.
1045 * Let v be the first variable with a_{v,c1}/a_{r,c1} != a_{v,c2}/a_{r,c2}.
1046 * Then if a_{v,c1}/a_{r,c1} < a_{v,c2}/a_{r,c2}, i.e.,
1047 * a_{v,c2} a_{r,c1} - a_{v,c1} a_{r,c2} > 0, c1 results in the minimal
1048 * increment. Otherwise, it's c2.
1050 static int lexmin_col_pair(struct isl_tab *tab,
1051 int row, int col1, int col2, isl_int tmp)
1053 int i;
1054 isl_int *tr;
1056 tr = tab->mat->row[row] + 2 + tab->M;
1058 for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
1059 int s1, s2;
1060 isl_int *r;
1062 if (!tab->var[i].is_row) {
1063 if (tab->var[i].index == col1)
1064 return col2;
1065 if (tab->var[i].index == col2)
1066 return col1;
1067 continue;
1070 if (tab->var[i].index == row)
1071 continue;
1073 r = tab->mat->row[tab->var[i].index] + 2 + tab->M;
1074 s1 = isl_int_sgn(r[col1]);
1075 s2 = isl_int_sgn(r[col2]);
1076 if (s1 == 0 && s2 == 0)
1077 continue;
1078 if (s1 < s2)
1079 return col1;
1080 if (s2 < s1)
1081 return col2;
1083 isl_int_mul(tmp, r[col2], tr[col1]);
1084 isl_int_submul(tmp, r[col1], tr[col2]);
1085 if (isl_int_is_pos(tmp))
1086 return col1;
1087 if (isl_int_is_neg(tmp))
1088 return col2;
1090 return -1;
1093 /* Given a row in the tableau, find and return the column that would
1094 * result in the lexicographically smallest, but positive, increment
1095 * in the sample point.
1096 * If there is no such column, then return tab->n_col.
1097 * If anything goes wrong, return -1.
1099 static int lexmin_pivot_col(struct isl_tab *tab, int row)
1101 int j;
1102 int col = tab->n_col;
1103 isl_int *tr;
1104 isl_int tmp;
1106 tr = tab->mat->row[row] + 2 + tab->M;
1108 isl_int_init(tmp);
1110 for (j = tab->n_dead; j < tab->n_col; ++j) {
1111 if (tab->col_var[j] >= 0 &&
1112 (tab->col_var[j] < tab->n_param ||
1113 tab->col_var[j] >= tab->n_var - tab->n_div))
1114 continue;
1116 if (!isl_int_is_pos(tr[j]))
1117 continue;
1119 if (col == tab->n_col)
1120 col = j;
1121 else
1122 col = lexmin_col_pair(tab, row, col, j, tmp);
1123 isl_assert(tab->mat->ctx, col >= 0, goto error);
1126 isl_int_clear(tmp);
1127 return col;
1128 error:
1129 isl_int_clear(tmp);
1130 return -1;
1133 /* Return the first known violated constraint, i.e., a non-negative
1134 * constraint that currently has an either obviously negative value
1135 * or a previously determined to be negative value.
1137 * If any constraint has a negative coefficient for the big parameter,
1138 * if any, then we return one of these first.
1140 static int first_neg(struct isl_tab *tab)
1142 int row;
1144 if (tab->M)
1145 for (row = tab->n_redundant; row < tab->n_row; ++row) {
1146 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
1147 continue;
1148 if (!isl_int_is_neg(tab->mat->row[row][2]))
1149 continue;
1150 if (tab->row_sign)
1151 tab->row_sign[row] = isl_tab_row_neg;
1152 return row;
1154 for (row = tab->n_redundant; row < tab->n_row; ++row) {
1155 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
1156 continue;
1157 if (tab->row_sign) {
1158 if (tab->row_sign[row] == 0 &&
1159 is_obviously_neg(tab, row))
1160 tab->row_sign[row] = isl_tab_row_neg;
1161 if (tab->row_sign[row] != isl_tab_row_neg)
1162 continue;
1163 } else if (!is_obviously_neg(tab, row))
1164 continue;
1165 return row;
1167 return -1;
1170 /* Check whether the invariant that all columns are lexico-positive
1171 * is satisfied. This function is not called from the current code
1172 * but is useful during debugging.
1174 static void check_lexpos(struct isl_tab *tab) __attribute__ ((unused));
1175 static void check_lexpos(struct isl_tab *tab)
1177 unsigned off = 2 + tab->M;
1178 int col;
1179 int var;
1180 int row;
1182 for (col = tab->n_dead; col < tab->n_col; ++col) {
1183 if (tab->col_var[col] >= 0 &&
1184 (tab->col_var[col] < tab->n_param ||
1185 tab->col_var[col] >= tab->n_var - tab->n_div))
1186 continue;
1187 for (var = tab->n_param; var < tab->n_var - tab->n_div; ++var) {
1188 if (!tab->var[var].is_row) {
1189 if (tab->var[var].index == col)
1190 break;
1191 else
1192 continue;
1194 row = tab->var[var].index;
1195 if (isl_int_is_zero(tab->mat->row[row][off + col]))
1196 continue;
1197 if (isl_int_is_pos(tab->mat->row[row][off + col]))
1198 break;
1199 fprintf(stderr, "lexneg column %d (row %d)\n",
1200 col, row);
1202 if (var >= tab->n_var - tab->n_div)
1203 fprintf(stderr, "zero column %d\n", col);
1207 /* Report to the caller that the given constraint is part of an encountered
1208 * conflict.
1210 static int report_conflicting_constraint(struct isl_tab *tab, int con)
1212 return tab->conflict(con, tab->conflict_user);
1215 /* Given a conflicting row in the tableau, report all constraints
1216 * involved in the row to the caller. That is, the row itself
1217 * (if it represents a constraint) and all constraint columns with
1218 * non-zero (and therefore negative) coefficients.
1220 static int report_conflict(struct isl_tab *tab, int row)
1222 int j;
1223 isl_int *tr;
1225 if (!tab->conflict)
1226 return 0;
1228 if (tab->row_var[row] < 0 &&
1229 report_conflicting_constraint(tab, ~tab->row_var[row]) < 0)
1230 return -1;
1232 tr = tab->mat->row[row] + 2 + tab->M;
1234 for (j = tab->n_dead; j < tab->n_col; ++j) {
1235 if (tab->col_var[j] >= 0 &&
1236 (tab->col_var[j] < tab->n_param ||
1237 tab->col_var[j] >= tab->n_var - tab->n_div))
1238 continue;
1240 if (!isl_int_is_neg(tr[j]))
1241 continue;
1243 if (tab->col_var[j] < 0 &&
1244 report_conflicting_constraint(tab, ~tab->col_var[j]) < 0)
1245 return -1;
1248 return 0;
1251 /* Resolve all known or obviously violated constraints through pivoting.
1252 * In particular, as long as we can find any violated constraint, we
1253 * look for a pivoting column that would result in the lexicographically
1254 * smallest increment in the sample point. If there is no such column
1255 * then the tableau is infeasible.
1257 static int restore_lexmin(struct isl_tab *tab) WARN_UNUSED;
1258 static int restore_lexmin(struct isl_tab *tab)
1260 int row, col;
1262 if (!tab)
1263 return -1;
1264 if (tab->empty)
1265 return 0;
1266 while ((row = first_neg(tab)) != -1) {
1267 col = lexmin_pivot_col(tab, row);
1268 if (col >= tab->n_col) {
1269 if (report_conflict(tab, row) < 0)
1270 return -1;
1271 if (isl_tab_mark_empty(tab) < 0)
1272 return -1;
1273 return 0;
1275 if (col < 0)
1276 return -1;
1277 if (isl_tab_pivot(tab, row, col) < 0)
1278 return -1;
1280 return 0;
1283 /* Given a row that represents an equality, look for an appropriate
1284 * pivoting column.
1285 * In particular, if there are any non-zero coefficients among
1286 * the non-parameter variables, then we take the last of these
1287 * variables. Eliminating this variable in terms of the other
1288 * variables and/or parameters does not influence the property
1289 * that all column in the initial tableau are lexicographically
1290 * positive. The row corresponding to the eliminated variable
1291 * will only have non-zero entries below the diagonal of the
1292 * initial tableau. That is, we transform
1294 * I I
1295 * 1 into a
1296 * I I
1298 * If there is no such non-parameter variable, then we are dealing with
1299 * pure parameter equality and we pick any parameter with coefficient 1 or -1
1300 * for elimination. This will ensure that the eliminated parameter
1301 * always has an integer value whenever all the other parameters are integral.
1302 * If there is no such parameter then we return -1.
1304 static int last_var_col_or_int_par_col(struct isl_tab *tab, int row)
1306 unsigned off = 2 + tab->M;
1307 int i;
1309 for (i = tab->n_var - tab->n_div - 1; i >= 0 && i >= tab->n_param; --i) {
1310 int col;
1311 if (tab->var[i].is_row)
1312 continue;
1313 col = tab->var[i].index;
1314 if (col <= tab->n_dead)
1315 continue;
1316 if (!isl_int_is_zero(tab->mat->row[row][off + col]))
1317 return col;
1319 for (i = tab->n_dead; i < tab->n_col; ++i) {
1320 if (isl_int_is_one(tab->mat->row[row][off + i]))
1321 return i;
1322 if (isl_int_is_negone(tab->mat->row[row][off + i]))
1323 return i;
1325 return -1;
1328 /* Add an equality that is known to be valid to the tableau.
1329 * We first check if we can eliminate a variable or a parameter.
1330 * If not, we add the equality as two inequalities.
1331 * In this case, the equality was a pure parameter equality and there
1332 * is no need to resolve any constraint violations.
1334 * This function assumes that at least two more rows and at least
1335 * two more elements in the constraint array are available in the tableau.
1337 static struct isl_tab *add_lexmin_valid_eq(struct isl_tab *tab, isl_int *eq)
1339 int i;
1340 int r;
1342 if (!tab)
1343 return NULL;
1344 r = isl_tab_add_row(tab, eq);
1345 if (r < 0)
1346 goto error;
1348 r = tab->con[r].index;
1349 i = last_var_col_or_int_par_col(tab, r);
1350 if (i < 0) {
1351 tab->con[r].is_nonneg = 1;
1352 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1353 goto error;
1354 isl_seq_neg(eq, eq, 1 + tab->n_var);
1355 r = isl_tab_add_row(tab, eq);
1356 if (r < 0)
1357 goto error;
1358 tab->con[r].is_nonneg = 1;
1359 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1360 goto error;
1361 } else {
1362 if (isl_tab_pivot(tab, r, i) < 0)
1363 goto error;
1364 if (isl_tab_kill_col(tab, i) < 0)
1365 goto error;
1366 tab->n_eq++;
1369 return tab;
1370 error:
1371 isl_tab_free(tab);
1372 return NULL;
1375 /* Check if the given row is a pure constant.
1377 static int is_constant(struct isl_tab *tab, int row)
1379 unsigned off = 2 + tab->M;
1381 return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1382 tab->n_col - tab->n_dead) == -1;
1385 /* Add an equality that may or may not be valid to the tableau.
1386 * If the resulting row is a pure constant, then it must be zero.
1387 * Otherwise, the resulting tableau is empty.
1389 * If the row is not a pure constant, then we add two inequalities,
1390 * each time checking that they can be satisfied.
1391 * In the end we try to use one of the two constraints to eliminate
1392 * a column.
1394 * This function assumes that at least two more rows and at least
1395 * two more elements in the constraint array are available in the tableau.
1397 static int add_lexmin_eq(struct isl_tab *tab, isl_int *eq) WARN_UNUSED;
1398 static int add_lexmin_eq(struct isl_tab *tab, isl_int *eq)
1400 int r1, r2;
1401 int row;
1402 struct isl_tab_undo *snap;
1404 if (!tab)
1405 return -1;
1406 snap = isl_tab_snap(tab);
1407 r1 = isl_tab_add_row(tab, eq);
1408 if (r1 < 0)
1409 return -1;
1410 tab->con[r1].is_nonneg = 1;
1411 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r1]) < 0)
1412 return -1;
1414 row = tab->con[r1].index;
1415 if (is_constant(tab, row)) {
1416 if (!isl_int_is_zero(tab->mat->row[row][1]) ||
1417 (tab->M && !isl_int_is_zero(tab->mat->row[row][2]))) {
1418 if (isl_tab_mark_empty(tab) < 0)
1419 return -1;
1420 return 0;
1422 if (isl_tab_rollback(tab, snap) < 0)
1423 return -1;
1424 return 0;
1427 if (restore_lexmin(tab) < 0)
1428 return -1;
1429 if (tab->empty)
1430 return 0;
1432 isl_seq_neg(eq, eq, 1 + tab->n_var);
1434 r2 = isl_tab_add_row(tab, eq);
1435 if (r2 < 0)
1436 return -1;
1437 tab->con[r2].is_nonneg = 1;
1438 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r2]) < 0)
1439 return -1;
1441 if (restore_lexmin(tab) < 0)
1442 return -1;
1443 if (tab->empty)
1444 return 0;
1446 if (!tab->con[r1].is_row) {
1447 if (isl_tab_kill_col(tab, tab->con[r1].index) < 0)
1448 return -1;
1449 } else if (!tab->con[r2].is_row) {
1450 if (isl_tab_kill_col(tab, tab->con[r2].index) < 0)
1451 return -1;
1454 if (tab->bmap) {
1455 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1456 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1457 return -1;
1458 isl_seq_neg(eq, eq, 1 + tab->n_var);
1459 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1460 isl_seq_neg(eq, eq, 1 + tab->n_var);
1461 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1462 return -1;
1463 if (!tab->bmap)
1464 return -1;
1467 return 0;
1470 /* Add an inequality to the tableau, resolving violations using
1471 * restore_lexmin.
1473 * This function assumes that at least one more row and at least
1474 * one more element in the constraint array are available in the tableau.
1476 static struct isl_tab *add_lexmin_ineq(struct isl_tab *tab, isl_int *ineq)
1478 int r;
1480 if (!tab)
1481 return NULL;
1482 if (tab->bmap) {
1483 tab->bmap = isl_basic_map_add_ineq(tab->bmap, ineq);
1484 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1485 goto error;
1486 if (!tab->bmap)
1487 goto error;
1489 r = isl_tab_add_row(tab, ineq);
1490 if (r < 0)
1491 goto error;
1492 tab->con[r].is_nonneg = 1;
1493 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1494 goto error;
1495 if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1496 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1497 goto error;
1498 return tab;
1501 if (restore_lexmin(tab) < 0)
1502 goto error;
1503 if (!tab->empty && tab->con[r].is_row &&
1504 isl_tab_row_is_redundant(tab, tab->con[r].index))
1505 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1506 goto error;
1507 return tab;
1508 error:
1509 isl_tab_free(tab);
1510 return NULL;
1513 /* Check if the coefficients of the parameters are all integral.
1515 static int integer_parameter(struct isl_tab *tab, int row)
1517 int i;
1518 int col;
1519 unsigned off = 2 + tab->M;
1521 for (i = 0; i < tab->n_param; ++i) {
1522 /* Eliminated parameter */
1523 if (tab->var[i].is_row)
1524 continue;
1525 col = tab->var[i].index;
1526 if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
1527 tab->mat->row[row][0]))
1528 return 0;
1530 for (i = 0; i < tab->n_div; ++i) {
1531 if (tab->var[tab->n_var - tab->n_div + i].is_row)
1532 continue;
1533 col = tab->var[tab->n_var - tab->n_div + i].index;
1534 if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
1535 tab->mat->row[row][0]))
1536 return 0;
1538 return 1;
1541 /* Check if the coefficients of the non-parameter variables are all integral.
1543 static int integer_variable(struct isl_tab *tab, int row)
1545 int i;
1546 unsigned off = 2 + tab->M;
1548 for (i = tab->n_dead; i < tab->n_col; ++i) {
1549 if (tab->col_var[i] >= 0 &&
1550 (tab->col_var[i] < tab->n_param ||
1551 tab->col_var[i] >= tab->n_var - tab->n_div))
1552 continue;
1553 if (!isl_int_is_divisible_by(tab->mat->row[row][off + i],
1554 tab->mat->row[row][0]))
1555 return 0;
1557 return 1;
1560 /* Check if the constant term is integral.
1562 static int integer_constant(struct isl_tab *tab, int row)
1564 return isl_int_is_divisible_by(tab->mat->row[row][1],
1565 tab->mat->row[row][0]);
1568 #define I_CST 1 << 0
1569 #define I_PAR 1 << 1
1570 #define I_VAR 1 << 2
1572 /* Check for next (non-parameter) variable after "var" (first if var == -1)
1573 * that is non-integer and therefore requires a cut and return
1574 * the index of the variable.
1575 * For parametric tableaus, there are three parts in a row,
1576 * the constant, the coefficients of the parameters and the rest.
1577 * For each part, we check whether the coefficients in that part
1578 * are all integral and if so, set the corresponding flag in *f.
1579 * If the constant and the parameter part are integral, then the
1580 * current sample value is integral and no cut is required
1581 * (irrespective of whether the variable part is integral).
1583 static int next_non_integer_var(struct isl_tab *tab, int var, int *f)
1585 var = var < 0 ? tab->n_param : var + 1;
1587 for (; var < tab->n_var - tab->n_div; ++var) {
1588 int flags = 0;
1589 int row;
1590 if (!tab->var[var].is_row)
1591 continue;
1592 row = tab->var[var].index;
1593 if (integer_constant(tab, row))
1594 ISL_FL_SET(flags, I_CST);
1595 if (integer_parameter(tab, row))
1596 ISL_FL_SET(flags, I_PAR);
1597 if (ISL_FL_ISSET(flags, I_CST) && ISL_FL_ISSET(flags, I_PAR))
1598 continue;
1599 if (integer_variable(tab, row))
1600 ISL_FL_SET(flags, I_VAR);
1601 *f = flags;
1602 return var;
1604 return -1;
1607 /* Check for first (non-parameter) variable that is non-integer and
1608 * therefore requires a cut and return the corresponding row.
1609 * For parametric tableaus, there are three parts in a row,
1610 * the constant, the coefficients of the parameters and the rest.
1611 * For each part, we check whether the coefficients in that part
1612 * are all integral and if so, set the corresponding flag in *f.
1613 * If the constant and the parameter part are integral, then the
1614 * current sample value is integral and no cut is required
1615 * (irrespective of whether the variable part is integral).
1617 static int first_non_integer_row(struct isl_tab *tab, int *f)
1619 int var = next_non_integer_var(tab, -1, f);
1621 return var < 0 ? -1 : tab->var[var].index;
1624 /* Add a (non-parametric) cut to cut away the non-integral sample
1625 * value of the given row.
1627 * If the row is given by
1629 * m r = f + \sum_i a_i y_i
1631 * then the cut is
1633 * c = - {-f/m} + \sum_i {a_i/m} y_i >= 0
1635 * The big parameter, if any, is ignored, since it is assumed to be big
1636 * enough to be divisible by any integer.
1637 * If the tableau is actually a parametric tableau, then this function
1638 * is only called when all coefficients of the parameters are integral.
1639 * The cut therefore has zero coefficients for the parameters.
1641 * The current value is known to be negative, so row_sign, if it
1642 * exists, is set accordingly.
1644 * Return the row of the cut or -1.
1646 static int add_cut(struct isl_tab *tab, int row)
1648 int i;
1649 int r;
1650 isl_int *r_row;
1651 unsigned off = 2 + tab->M;
1653 if (isl_tab_extend_cons(tab, 1) < 0)
1654 return -1;
1655 r = isl_tab_allocate_con(tab);
1656 if (r < 0)
1657 return -1;
1659 r_row = tab->mat->row[tab->con[r].index];
1660 isl_int_set(r_row[0], tab->mat->row[row][0]);
1661 isl_int_neg(r_row[1], tab->mat->row[row][1]);
1662 isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
1663 isl_int_neg(r_row[1], r_row[1]);
1664 if (tab->M)
1665 isl_int_set_si(r_row[2], 0);
1666 for (i = 0; i < tab->n_col; ++i)
1667 isl_int_fdiv_r(r_row[off + i],
1668 tab->mat->row[row][off + i], tab->mat->row[row][0]);
1670 tab->con[r].is_nonneg = 1;
1671 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1672 return -1;
1673 if (tab->row_sign)
1674 tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
1676 return tab->con[r].index;
1679 #define CUT_ALL 1
1680 #define CUT_ONE 0
1682 /* Given a non-parametric tableau, add cuts until an integer
1683 * sample point is obtained or until the tableau is determined
1684 * to be integer infeasible.
1685 * As long as there is any non-integer value in the sample point,
1686 * we add appropriate cuts, if possible, for each of these
1687 * non-integer values and then resolve the violated
1688 * cut constraints using restore_lexmin.
1689 * If one of the corresponding rows is equal to an integral
1690 * combination of variables/constraints plus a non-integral constant,
1691 * then there is no way to obtain an integer point and we return
1692 * a tableau that is marked empty.
1693 * The parameter cutting_strategy controls the strategy used when adding cuts
1694 * to remove non-integer points. CUT_ALL adds all possible cuts
1695 * before continuing the search. CUT_ONE adds only one cut at a time.
1697 static struct isl_tab *cut_to_integer_lexmin(struct isl_tab *tab,
1698 int cutting_strategy)
1700 int var;
1701 int row;
1702 int flags;
1704 if (!tab)
1705 return NULL;
1706 if (tab->empty)
1707 return tab;
1709 while ((var = next_non_integer_var(tab, -1, &flags)) != -1) {
1710 do {
1711 if (ISL_FL_ISSET(flags, I_VAR)) {
1712 if (isl_tab_mark_empty(tab) < 0)
1713 goto error;
1714 return tab;
1716 row = tab->var[var].index;
1717 row = add_cut(tab, row);
1718 if (row < 0)
1719 goto error;
1720 if (cutting_strategy == CUT_ONE)
1721 break;
1722 } while ((var = next_non_integer_var(tab, var, &flags)) != -1);
1723 if (restore_lexmin(tab) < 0)
1724 goto error;
1725 if (tab->empty)
1726 break;
1728 return tab;
1729 error:
1730 isl_tab_free(tab);
1731 return NULL;
1734 /* Check whether all the currently active samples also satisfy the inequality
1735 * "ineq" (treated as an equality if eq is set).
1736 * Remove those samples that do not.
1738 static struct isl_tab *check_samples(struct isl_tab *tab, isl_int *ineq, int eq)
1740 int i;
1741 isl_int v;
1743 if (!tab)
1744 return NULL;
1746 isl_assert(tab->mat->ctx, tab->bmap, goto error);
1747 isl_assert(tab->mat->ctx, tab->samples, goto error);
1748 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, goto error);
1750 isl_int_init(v);
1751 for (i = tab->n_outside; i < tab->n_sample; ++i) {
1752 int sgn;
1753 isl_seq_inner_product(ineq, tab->samples->row[i],
1754 1 + tab->n_var, &v);
1755 sgn = isl_int_sgn(v);
1756 if (eq ? (sgn == 0) : (sgn >= 0))
1757 continue;
1758 tab = isl_tab_drop_sample(tab, i);
1759 if (!tab)
1760 break;
1762 isl_int_clear(v);
1764 return tab;
1765 error:
1766 isl_tab_free(tab);
1767 return NULL;
1770 /* Check whether the sample value of the tableau is finite,
1771 * i.e., either the tableau does not use a big parameter, or
1772 * all values of the variables are equal to the big parameter plus
1773 * some constant. This constant is the actual sample value.
1775 static int sample_is_finite(struct isl_tab *tab)
1777 int i;
1779 if (!tab->M)
1780 return 1;
1782 for (i = 0; i < tab->n_var; ++i) {
1783 int row;
1784 if (!tab->var[i].is_row)
1785 return 0;
1786 row = tab->var[i].index;
1787 if (isl_int_ne(tab->mat->row[row][0], tab->mat->row[row][2]))
1788 return 0;
1790 return 1;
1793 /* Check if the context tableau of sol has any integer points.
1794 * Leave tab in empty state if no integer point can be found.
1795 * If an integer point can be found and if moreover it is finite,
1796 * then it is added to the list of sample values.
1798 * This function is only called when none of the currently active sample
1799 * values satisfies the most recently added constraint.
1801 static struct isl_tab *check_integer_feasible(struct isl_tab *tab)
1803 struct isl_tab_undo *snap;
1805 if (!tab)
1806 return NULL;
1808 snap = isl_tab_snap(tab);
1809 if (isl_tab_push_basis(tab) < 0)
1810 goto error;
1812 tab = cut_to_integer_lexmin(tab, CUT_ALL);
1813 if (!tab)
1814 goto error;
1816 if (!tab->empty && sample_is_finite(tab)) {
1817 struct isl_vec *sample;
1819 sample = isl_tab_get_sample_value(tab);
1821 if (isl_tab_add_sample(tab, sample) < 0)
1822 goto error;
1825 if (!tab->empty && isl_tab_rollback(tab, snap) < 0)
1826 goto error;
1828 return tab;
1829 error:
1830 isl_tab_free(tab);
1831 return NULL;
1834 /* Check if any of the currently active sample values satisfies
1835 * the inequality "ineq" (an equality if eq is set).
1837 static int tab_has_valid_sample(struct isl_tab *tab, isl_int *ineq, int eq)
1839 int i;
1840 isl_int v;
1842 if (!tab)
1843 return -1;
1845 isl_assert(tab->mat->ctx, tab->bmap, return -1);
1846 isl_assert(tab->mat->ctx, tab->samples, return -1);
1847 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, return -1);
1849 isl_int_init(v);
1850 for (i = tab->n_outside; i < tab->n_sample; ++i) {
1851 int sgn;
1852 isl_seq_inner_product(ineq, tab->samples->row[i],
1853 1 + tab->n_var, &v);
1854 sgn = isl_int_sgn(v);
1855 if (eq ? (sgn == 0) : (sgn >= 0))
1856 break;
1858 isl_int_clear(v);
1860 return i < tab->n_sample;
1863 /* Add a div specified by "div" to the tableau "tab" and return
1864 * 1 if the div is obviously non-negative.
1866 static int context_tab_add_div(struct isl_tab *tab, __isl_keep isl_vec *div,
1867 int (*add_ineq)(void *user, isl_int *), void *user)
1869 int i;
1870 int r;
1871 struct isl_mat *samples;
1872 int nonneg;
1874 r = isl_tab_add_div(tab, div, add_ineq, user);
1875 if (r < 0)
1876 return -1;
1877 nonneg = tab->var[r].is_nonneg;
1878 tab->var[r].frozen = 1;
1880 samples = isl_mat_extend(tab->samples,
1881 tab->n_sample, 1 + tab->n_var);
1882 tab->samples = samples;
1883 if (!samples)
1884 return -1;
1885 for (i = tab->n_outside; i < samples->n_row; ++i) {
1886 isl_seq_inner_product(div->el + 1, samples->row[i],
1887 div->size - 1, &samples->row[i][samples->n_col - 1]);
1888 isl_int_fdiv_q(samples->row[i][samples->n_col - 1],
1889 samples->row[i][samples->n_col - 1], div->el[0]);
1892 return nonneg;
1895 /* Add a div specified by "div" to both the main tableau and
1896 * the context tableau. In case of the main tableau, we only
1897 * need to add an extra div. In the context tableau, we also
1898 * need to express the meaning of the div.
1899 * Return the index of the div or -1 if anything went wrong.
1901 static int add_div(struct isl_tab *tab, struct isl_context *context,
1902 struct isl_vec *div)
1904 int r;
1905 int nonneg;
1907 if ((nonneg = context->op->add_div(context, div)) < 0)
1908 goto error;
1910 if (!context->op->is_ok(context))
1911 goto error;
1913 if (isl_tab_extend_vars(tab, 1) < 0)
1914 goto error;
1915 r = isl_tab_allocate_var(tab);
1916 if (r < 0)
1917 goto error;
1918 if (nonneg)
1919 tab->var[r].is_nonneg = 1;
1920 tab->var[r].frozen = 1;
1921 tab->n_div++;
1923 return tab->n_div - 1;
1924 error:
1925 context->op->invalidate(context);
1926 return -1;
1929 static int find_div(struct isl_tab *tab, isl_int *div, isl_int denom)
1931 int i;
1932 unsigned total = isl_basic_map_total_dim(tab->bmap);
1934 for (i = 0; i < tab->bmap->n_div; ++i) {
1935 if (isl_int_ne(tab->bmap->div[i][0], denom))
1936 continue;
1937 if (!isl_seq_eq(tab->bmap->div[i] + 1, div, 1 + total))
1938 continue;
1939 return i;
1941 return -1;
1944 /* Return the index of a div that corresponds to "div".
1945 * We first check if we already have such a div and if not, we create one.
1947 static int get_div(struct isl_tab *tab, struct isl_context *context,
1948 struct isl_vec *div)
1950 int d;
1951 struct isl_tab *context_tab = context->op->peek_tab(context);
1953 if (!context_tab)
1954 return -1;
1956 d = find_div(context_tab, div->el + 1, div->el[0]);
1957 if (d != -1)
1958 return d;
1960 return add_div(tab, context, div);
1963 /* Add a parametric cut to cut away the non-integral sample value
1964 * of the give row.
1965 * Let a_i be the coefficients of the constant term and the parameters
1966 * and let b_i be the coefficients of the variables or constraints
1967 * in basis of the tableau.
1968 * Let q be the div q = floor(\sum_i {-a_i} y_i).
1970 * The cut is expressed as
1972 * c = \sum_i -{-a_i} y_i + \sum_i {b_i} x_i + q >= 0
1974 * If q did not already exist in the context tableau, then it is added first.
1975 * If q is in a column of the main tableau then the "+ q" can be accomplished
1976 * by setting the corresponding entry to the denominator of the constraint.
1977 * If q happens to be in a row of the main tableau, then the corresponding
1978 * row needs to be added instead (taking care of the denominators).
1979 * Note that this is very unlikely, but perhaps not entirely impossible.
1981 * The current value of the cut is known to be negative (or at least
1982 * non-positive), so row_sign is set accordingly.
1984 * Return the row of the cut or -1.
1986 static int add_parametric_cut(struct isl_tab *tab, int row,
1987 struct isl_context *context)
1989 struct isl_vec *div;
1990 int d;
1991 int i;
1992 int r;
1993 isl_int *r_row;
1994 int col;
1995 int n;
1996 unsigned off = 2 + tab->M;
1998 if (!context)
1999 return -1;
2001 div = get_row_parameter_div(tab, row);
2002 if (!div)
2003 return -1;
2005 n = tab->n_div;
2006 d = context->op->get_div(context, tab, div);
2007 isl_vec_free(div);
2008 if (d < 0)
2009 return -1;
2011 if (isl_tab_extend_cons(tab, 1) < 0)
2012 return -1;
2013 r = isl_tab_allocate_con(tab);
2014 if (r < 0)
2015 return -1;
2017 r_row = tab->mat->row[tab->con[r].index];
2018 isl_int_set(r_row[0], tab->mat->row[row][0]);
2019 isl_int_neg(r_row[1], tab->mat->row[row][1]);
2020 isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
2021 isl_int_neg(r_row[1], r_row[1]);
2022 if (tab->M)
2023 isl_int_set_si(r_row[2], 0);
2024 for (i = 0; i < tab->n_param; ++i) {
2025 if (tab->var[i].is_row)
2026 continue;
2027 col = tab->var[i].index;
2028 isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
2029 isl_int_fdiv_r(r_row[off + col], r_row[off + col],
2030 tab->mat->row[row][0]);
2031 isl_int_neg(r_row[off + col], r_row[off + col]);
2033 for (i = 0; i < tab->n_div; ++i) {
2034 if (tab->var[tab->n_var - tab->n_div + i].is_row)
2035 continue;
2036 col = tab->var[tab->n_var - tab->n_div + i].index;
2037 isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
2038 isl_int_fdiv_r(r_row[off + col], r_row[off + col],
2039 tab->mat->row[row][0]);
2040 isl_int_neg(r_row[off + col], r_row[off + col]);
2042 for (i = 0; i < tab->n_col; ++i) {
2043 if (tab->col_var[i] >= 0 &&
2044 (tab->col_var[i] < tab->n_param ||
2045 tab->col_var[i] >= tab->n_var - tab->n_div))
2046 continue;
2047 isl_int_fdiv_r(r_row[off + i],
2048 tab->mat->row[row][off + i], tab->mat->row[row][0]);
2050 if (tab->var[tab->n_var - tab->n_div + d].is_row) {
2051 isl_int gcd;
2052 int d_row = tab->var[tab->n_var - tab->n_div + d].index;
2053 isl_int_init(gcd);
2054 isl_int_gcd(gcd, tab->mat->row[d_row][0], r_row[0]);
2055 isl_int_divexact(r_row[0], r_row[0], gcd);
2056 isl_int_divexact(gcd, tab->mat->row[d_row][0], gcd);
2057 isl_seq_combine(r_row + 1, gcd, r_row + 1,
2058 r_row[0], tab->mat->row[d_row] + 1,
2059 off - 1 + tab->n_col);
2060 isl_int_mul(r_row[0], r_row[0], tab->mat->row[d_row][0]);
2061 isl_int_clear(gcd);
2062 } else {
2063 col = tab->var[tab->n_var - tab->n_div + d].index;
2064 isl_int_set(r_row[off + col], tab->mat->row[row][0]);
2067 tab->con[r].is_nonneg = 1;
2068 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
2069 return -1;
2070 if (tab->row_sign)
2071 tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
2073 row = tab->con[r].index;
2075 if (d >= n && context->op->detect_equalities(context, tab) < 0)
2076 return -1;
2078 return row;
2081 /* Construct a tableau for bmap that can be used for computing
2082 * the lexicographic minimum (or maximum) of bmap.
2083 * If not NULL, then dom is the domain where the minimum
2084 * should be computed. In this case, we set up a parametric
2085 * tableau with row signs (initialized to "unknown").
2086 * If M is set, then the tableau will use a big parameter.
2087 * If max is set, then a maximum should be computed instead of a minimum.
2088 * This means that for each variable x, the tableau will contain the variable
2089 * x' = M - x, rather than x' = M + x. This in turn means that the coefficient
2090 * of the variables in all constraints are negated prior to adding them
2091 * to the tableau.
2093 static struct isl_tab *tab_for_lexmin(struct isl_basic_map *bmap,
2094 struct isl_basic_set *dom, unsigned M, int max)
2096 int i;
2097 struct isl_tab *tab;
2098 unsigned n_var;
2099 unsigned o_var;
2101 tab = isl_tab_alloc(bmap->ctx, 2 * bmap->n_eq + bmap->n_ineq + 1,
2102 isl_basic_map_total_dim(bmap), M);
2103 if (!tab)
2104 return NULL;
2106 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
2107 if (dom) {
2108 tab->n_param = isl_basic_set_total_dim(dom) - dom->n_div;
2109 tab->n_div = dom->n_div;
2110 tab->row_sign = isl_calloc_array(bmap->ctx,
2111 enum isl_tab_row_sign, tab->mat->n_row);
2112 if (tab->mat->n_row && !tab->row_sign)
2113 goto error;
2115 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
2116 if (isl_tab_mark_empty(tab) < 0)
2117 goto error;
2118 return tab;
2121 for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
2122 tab->var[i].is_nonneg = 1;
2123 tab->var[i].frozen = 1;
2125 o_var = 1 + tab->n_param;
2126 n_var = tab->n_var - tab->n_param - tab->n_div;
2127 for (i = 0; i < bmap->n_eq; ++i) {
2128 if (max)
2129 isl_seq_neg(bmap->eq[i] + o_var,
2130 bmap->eq[i] + o_var, n_var);
2131 tab = add_lexmin_valid_eq(tab, bmap->eq[i]);
2132 if (max)
2133 isl_seq_neg(bmap->eq[i] + o_var,
2134 bmap->eq[i] + o_var, n_var);
2135 if (!tab || tab->empty)
2136 return tab;
2138 if (bmap->n_eq && restore_lexmin(tab) < 0)
2139 goto error;
2140 for (i = 0; i < bmap->n_ineq; ++i) {
2141 if (max)
2142 isl_seq_neg(bmap->ineq[i] + o_var,
2143 bmap->ineq[i] + o_var, n_var);
2144 tab = add_lexmin_ineq(tab, bmap->ineq[i]);
2145 if (max)
2146 isl_seq_neg(bmap->ineq[i] + o_var,
2147 bmap->ineq[i] + o_var, n_var);
2148 if (!tab || tab->empty)
2149 return tab;
2151 return tab;
2152 error:
2153 isl_tab_free(tab);
2154 return NULL;
2157 /* Given a main tableau where more than one row requires a split,
2158 * determine and return the "best" row to split on.
2160 * Given two rows in the main tableau, if the inequality corresponding
2161 * to the first row is redundant with respect to that of the second row
2162 * in the current tableau, then it is better to split on the second row,
2163 * since in the positive part, both rows will be positive.
2164 * (In the negative part a pivot will have to be performed and just about
2165 * anything can happen to the sign of the other row.)
2167 * As a simple heuristic, we therefore select the row that makes the most
2168 * of the other rows redundant.
2170 * Perhaps it would also be useful to look at the number of constraints
2171 * that conflict with any given constraint.
2173 * best is the best row so far (-1 when we have not found any row yet).
2174 * best_r is the number of other rows made redundant by row best.
2175 * When best is still -1, bset_r is meaningless, but it is initialized
2176 * to some arbitrary value (0) anyway. Without this redundant initialization
2177 * valgrind may warn about uninitialized memory accesses when isl
2178 * is compiled with some versions of gcc.
2180 static int best_split(struct isl_tab *tab, struct isl_tab *context_tab)
2182 struct isl_tab_undo *snap;
2183 int split;
2184 int row;
2185 int best = -1;
2186 int best_r = 0;
2188 if (isl_tab_extend_cons(context_tab, 2) < 0)
2189 return -1;
2191 snap = isl_tab_snap(context_tab);
2193 for (split = tab->n_redundant; split < tab->n_row; ++split) {
2194 struct isl_tab_undo *snap2;
2195 struct isl_vec *ineq = NULL;
2196 int r = 0;
2197 int ok;
2199 if (!isl_tab_var_from_row(tab, split)->is_nonneg)
2200 continue;
2201 if (tab->row_sign[split] != isl_tab_row_any)
2202 continue;
2204 ineq = get_row_parameter_ineq(tab, split);
2205 if (!ineq)
2206 return -1;
2207 ok = isl_tab_add_ineq(context_tab, ineq->el) >= 0;
2208 isl_vec_free(ineq);
2209 if (!ok)
2210 return -1;
2212 snap2 = isl_tab_snap(context_tab);
2214 for (row = tab->n_redundant; row < tab->n_row; ++row) {
2215 struct isl_tab_var *var;
2217 if (row == split)
2218 continue;
2219 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
2220 continue;
2221 if (tab->row_sign[row] != isl_tab_row_any)
2222 continue;
2224 ineq = get_row_parameter_ineq(tab, row);
2225 if (!ineq)
2226 return -1;
2227 ok = isl_tab_add_ineq(context_tab, ineq->el) >= 0;
2228 isl_vec_free(ineq);
2229 if (!ok)
2230 return -1;
2231 var = &context_tab->con[context_tab->n_con - 1];
2232 if (!context_tab->empty &&
2233 !isl_tab_min_at_most_neg_one(context_tab, var))
2234 r++;
2235 if (isl_tab_rollback(context_tab, snap2) < 0)
2236 return -1;
2238 if (best == -1 || r > best_r) {
2239 best = split;
2240 best_r = r;
2242 if (isl_tab_rollback(context_tab, snap) < 0)
2243 return -1;
2246 return best;
2249 static struct isl_basic_set *context_lex_peek_basic_set(
2250 struct isl_context *context)
2252 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2253 if (!clex->tab)
2254 return NULL;
2255 return isl_tab_peek_bset(clex->tab);
2258 static struct isl_tab *context_lex_peek_tab(struct isl_context *context)
2260 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2261 return clex->tab;
2264 static void context_lex_add_eq(struct isl_context *context, isl_int *eq,
2265 int check, int update)
2267 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2268 if (isl_tab_extend_cons(clex->tab, 2) < 0)
2269 goto error;
2270 if (add_lexmin_eq(clex->tab, eq) < 0)
2271 goto error;
2272 if (check) {
2273 int v = tab_has_valid_sample(clex->tab, eq, 1);
2274 if (v < 0)
2275 goto error;
2276 if (!v)
2277 clex->tab = check_integer_feasible(clex->tab);
2279 if (update)
2280 clex->tab = check_samples(clex->tab, eq, 1);
2281 return;
2282 error:
2283 isl_tab_free(clex->tab);
2284 clex->tab = NULL;
2287 static void context_lex_add_ineq(struct isl_context *context, isl_int *ineq,
2288 int check, int update)
2290 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2291 if (isl_tab_extend_cons(clex->tab, 1) < 0)
2292 goto error;
2293 clex->tab = add_lexmin_ineq(clex->tab, ineq);
2294 if (check) {
2295 int v = tab_has_valid_sample(clex->tab, ineq, 0);
2296 if (v < 0)
2297 goto error;
2298 if (!v)
2299 clex->tab = check_integer_feasible(clex->tab);
2301 if (update)
2302 clex->tab = check_samples(clex->tab, ineq, 0);
2303 return;
2304 error:
2305 isl_tab_free(clex->tab);
2306 clex->tab = NULL;
2309 static int context_lex_add_ineq_wrap(void *user, isl_int *ineq)
2311 struct isl_context *context = (struct isl_context *)user;
2312 context_lex_add_ineq(context, ineq, 0, 0);
2313 return context->op->is_ok(context) ? 0 : -1;
2316 /* Check which signs can be obtained by "ineq" on all the currently
2317 * active sample values. See row_sign for more information.
2319 static enum isl_tab_row_sign tab_ineq_sign(struct isl_tab *tab, isl_int *ineq,
2320 int strict)
2322 int i;
2323 int sgn;
2324 isl_int tmp;
2325 enum isl_tab_row_sign res = isl_tab_row_unknown;
2327 isl_assert(tab->mat->ctx, tab->samples, return isl_tab_row_unknown);
2328 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var,
2329 return isl_tab_row_unknown);
2331 isl_int_init(tmp);
2332 for (i = tab->n_outside; i < tab->n_sample; ++i) {
2333 isl_seq_inner_product(tab->samples->row[i], ineq,
2334 1 + tab->n_var, &tmp);
2335 sgn = isl_int_sgn(tmp);
2336 if (sgn > 0 || (sgn == 0 && strict)) {
2337 if (res == isl_tab_row_unknown)
2338 res = isl_tab_row_pos;
2339 if (res == isl_tab_row_neg)
2340 res = isl_tab_row_any;
2342 if (sgn < 0) {
2343 if (res == isl_tab_row_unknown)
2344 res = isl_tab_row_neg;
2345 if (res == isl_tab_row_pos)
2346 res = isl_tab_row_any;
2348 if (res == isl_tab_row_any)
2349 break;
2351 isl_int_clear(tmp);
2353 return res;
2356 static enum isl_tab_row_sign context_lex_ineq_sign(struct isl_context *context,
2357 isl_int *ineq, int strict)
2359 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2360 return tab_ineq_sign(clex->tab, ineq, strict);
2363 /* Check whether "ineq" can be added to the tableau without rendering
2364 * it infeasible.
2366 static int context_lex_test_ineq(struct isl_context *context, isl_int *ineq)
2368 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2369 struct isl_tab_undo *snap;
2370 int feasible;
2372 if (!clex->tab)
2373 return -1;
2375 if (isl_tab_extend_cons(clex->tab, 1) < 0)
2376 return -1;
2378 snap = isl_tab_snap(clex->tab);
2379 if (isl_tab_push_basis(clex->tab) < 0)
2380 return -1;
2381 clex->tab = add_lexmin_ineq(clex->tab, ineq);
2382 clex->tab = check_integer_feasible(clex->tab);
2383 if (!clex->tab)
2384 return -1;
2385 feasible = !clex->tab->empty;
2386 if (isl_tab_rollback(clex->tab, snap) < 0)
2387 return -1;
2389 return feasible;
2392 static int context_lex_get_div(struct isl_context *context, struct isl_tab *tab,
2393 struct isl_vec *div)
2395 return get_div(tab, context, div);
2398 /* Add a div specified by "div" to the context tableau and return
2399 * 1 if the div is obviously non-negative.
2400 * context_tab_add_div will always return 1, because all variables
2401 * in a isl_context_lex tableau are non-negative.
2402 * However, if we are using a big parameter in the context, then this only
2403 * reflects the non-negativity of the variable used to _encode_ the
2404 * div, i.e., div' = M + div, so we can't draw any conclusions.
2406 static int context_lex_add_div(struct isl_context *context,
2407 __isl_keep isl_vec *div)
2409 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2410 int nonneg;
2411 nonneg = context_tab_add_div(clex->tab, div,
2412 context_lex_add_ineq_wrap, context);
2413 if (nonneg < 0)
2414 return -1;
2415 if (clex->tab->M)
2416 return 0;
2417 return nonneg;
2420 static int context_lex_detect_equalities(struct isl_context *context,
2421 struct isl_tab *tab)
2423 return 0;
2426 static int context_lex_best_split(struct isl_context *context,
2427 struct isl_tab *tab)
2429 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2430 struct isl_tab_undo *snap;
2431 int r;
2433 snap = isl_tab_snap(clex->tab);
2434 if (isl_tab_push_basis(clex->tab) < 0)
2435 return -1;
2436 r = best_split(tab, clex->tab);
2438 if (r >= 0 && isl_tab_rollback(clex->tab, snap) < 0)
2439 return -1;
2441 return r;
2444 static int context_lex_is_empty(struct isl_context *context)
2446 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2447 if (!clex->tab)
2448 return -1;
2449 return clex->tab->empty;
2452 static void *context_lex_save(struct isl_context *context)
2454 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2455 struct isl_tab_undo *snap;
2457 snap = isl_tab_snap(clex->tab);
2458 if (isl_tab_push_basis(clex->tab) < 0)
2459 return NULL;
2460 if (isl_tab_save_samples(clex->tab) < 0)
2461 return NULL;
2463 return snap;
2466 static void context_lex_restore(struct isl_context *context, void *save)
2468 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2469 if (isl_tab_rollback(clex->tab, (struct isl_tab_undo *)save) < 0) {
2470 isl_tab_free(clex->tab);
2471 clex->tab = NULL;
2475 static void context_lex_discard(void *save)
2479 static int context_lex_is_ok(struct isl_context *context)
2481 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2482 return !!clex->tab;
2485 /* For each variable in the context tableau, check if the variable can
2486 * only attain non-negative values. If so, mark the parameter as non-negative
2487 * in the main tableau. This allows for a more direct identification of some
2488 * cases of violated constraints.
2490 static struct isl_tab *tab_detect_nonnegative_parameters(struct isl_tab *tab,
2491 struct isl_tab *context_tab)
2493 int i;
2494 struct isl_tab_undo *snap;
2495 struct isl_vec *ineq = NULL;
2496 struct isl_tab_var *var;
2497 int n;
2499 if (context_tab->n_var == 0)
2500 return tab;
2502 ineq = isl_vec_alloc(tab->mat->ctx, 1 + context_tab->n_var);
2503 if (!ineq)
2504 goto error;
2506 if (isl_tab_extend_cons(context_tab, 1) < 0)
2507 goto error;
2509 snap = isl_tab_snap(context_tab);
2511 n = 0;
2512 isl_seq_clr(ineq->el, ineq->size);
2513 for (i = 0; i < context_tab->n_var; ++i) {
2514 isl_int_set_si(ineq->el[1 + i], 1);
2515 if (isl_tab_add_ineq(context_tab, ineq->el) < 0)
2516 goto error;
2517 var = &context_tab->con[context_tab->n_con - 1];
2518 if (!context_tab->empty &&
2519 !isl_tab_min_at_most_neg_one(context_tab, var)) {
2520 int j = i;
2521 if (i >= tab->n_param)
2522 j = i - tab->n_param + tab->n_var - tab->n_div;
2523 tab->var[j].is_nonneg = 1;
2524 n++;
2526 isl_int_set_si(ineq->el[1 + i], 0);
2527 if (isl_tab_rollback(context_tab, snap) < 0)
2528 goto error;
2531 if (context_tab->M && n == context_tab->n_var) {
2532 context_tab->mat = isl_mat_drop_cols(context_tab->mat, 2, 1);
2533 context_tab->M = 0;
2536 isl_vec_free(ineq);
2537 return tab;
2538 error:
2539 isl_vec_free(ineq);
2540 isl_tab_free(tab);
2541 return NULL;
2544 static struct isl_tab *context_lex_detect_nonnegative_parameters(
2545 struct isl_context *context, struct isl_tab *tab)
2547 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2548 struct isl_tab_undo *snap;
2550 if (!tab)
2551 return NULL;
2553 snap = isl_tab_snap(clex->tab);
2554 if (isl_tab_push_basis(clex->tab) < 0)
2555 goto error;
2557 tab = tab_detect_nonnegative_parameters(tab, clex->tab);
2559 if (isl_tab_rollback(clex->tab, snap) < 0)
2560 goto error;
2562 return tab;
2563 error:
2564 isl_tab_free(tab);
2565 return NULL;
2568 static void context_lex_invalidate(struct isl_context *context)
2570 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2571 isl_tab_free(clex->tab);
2572 clex->tab = NULL;
2575 static void context_lex_free(struct isl_context *context)
2577 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2578 isl_tab_free(clex->tab);
2579 free(clex);
2582 struct isl_context_op isl_context_lex_op = {
2583 context_lex_detect_nonnegative_parameters,
2584 context_lex_peek_basic_set,
2585 context_lex_peek_tab,
2586 context_lex_add_eq,
2587 context_lex_add_ineq,
2588 context_lex_ineq_sign,
2589 context_lex_test_ineq,
2590 context_lex_get_div,
2591 context_lex_add_div,
2592 context_lex_detect_equalities,
2593 context_lex_best_split,
2594 context_lex_is_empty,
2595 context_lex_is_ok,
2596 context_lex_save,
2597 context_lex_restore,
2598 context_lex_discard,
2599 context_lex_invalidate,
2600 context_lex_free,
2603 static struct isl_tab *context_tab_for_lexmin(struct isl_basic_set *bset)
2605 struct isl_tab *tab;
2607 if (!bset)
2608 return NULL;
2609 tab = tab_for_lexmin((struct isl_basic_map *)bset, NULL, 1, 0);
2610 if (!tab)
2611 goto error;
2612 if (isl_tab_track_bset(tab, bset) < 0)
2613 goto error;
2614 tab = isl_tab_init_samples(tab);
2615 return tab;
2616 error:
2617 isl_basic_set_free(bset);
2618 return NULL;
2621 static struct isl_context *isl_context_lex_alloc(struct isl_basic_set *dom)
2623 struct isl_context_lex *clex;
2625 if (!dom)
2626 return NULL;
2628 clex = isl_alloc_type(dom->ctx, struct isl_context_lex);
2629 if (!clex)
2630 return NULL;
2632 clex->context.op = &isl_context_lex_op;
2634 clex->tab = context_tab_for_lexmin(isl_basic_set_copy(dom));
2635 if (restore_lexmin(clex->tab) < 0)
2636 goto error;
2637 clex->tab = check_integer_feasible(clex->tab);
2638 if (!clex->tab)
2639 goto error;
2641 return &clex->context;
2642 error:
2643 clex->context.op->free(&clex->context);
2644 return NULL;
2647 /* Representation of the context when using generalized basis reduction.
2649 * "shifted" contains the offsets of the unit hypercubes that lie inside the
2650 * context. Any rational point in "shifted" can therefore be rounded
2651 * up to an integer point in the context.
2652 * If the context is constrained by any equality, then "shifted" is not used
2653 * as it would be empty.
2655 struct isl_context_gbr {
2656 struct isl_context context;
2657 struct isl_tab *tab;
2658 struct isl_tab *shifted;
2659 struct isl_tab *cone;
2662 static struct isl_tab *context_gbr_detect_nonnegative_parameters(
2663 struct isl_context *context, struct isl_tab *tab)
2665 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2666 if (!tab)
2667 return NULL;
2668 return tab_detect_nonnegative_parameters(tab, cgbr->tab);
2671 static struct isl_basic_set *context_gbr_peek_basic_set(
2672 struct isl_context *context)
2674 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2675 if (!cgbr->tab)
2676 return NULL;
2677 return isl_tab_peek_bset(cgbr->tab);
2680 static struct isl_tab *context_gbr_peek_tab(struct isl_context *context)
2682 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2683 return cgbr->tab;
2686 /* Initialize the "shifted" tableau of the context, which
2687 * contains the constraints of the original tableau shifted
2688 * by the sum of all negative coefficients. This ensures
2689 * that any rational point in the shifted tableau can
2690 * be rounded up to yield an integer point in the original tableau.
2692 static void gbr_init_shifted(struct isl_context_gbr *cgbr)
2694 int i, j;
2695 struct isl_vec *cst;
2696 struct isl_basic_set *bset = isl_tab_peek_bset(cgbr->tab);
2697 unsigned dim = isl_basic_set_total_dim(bset);
2699 cst = isl_vec_alloc(cgbr->tab->mat->ctx, bset->n_ineq);
2700 if (!cst)
2701 return;
2703 for (i = 0; i < bset->n_ineq; ++i) {
2704 isl_int_set(cst->el[i], bset->ineq[i][0]);
2705 for (j = 0; j < dim; ++j) {
2706 if (!isl_int_is_neg(bset->ineq[i][1 + j]))
2707 continue;
2708 isl_int_add(bset->ineq[i][0], bset->ineq[i][0],
2709 bset->ineq[i][1 + j]);
2713 cgbr->shifted = isl_tab_from_basic_set(bset, 0);
2715 for (i = 0; i < bset->n_ineq; ++i)
2716 isl_int_set(bset->ineq[i][0], cst->el[i]);
2718 isl_vec_free(cst);
2721 /* Check if the shifted tableau is non-empty, and if so
2722 * use the sample point to construct an integer point
2723 * of the context tableau.
2725 static struct isl_vec *gbr_get_shifted_sample(struct isl_context_gbr *cgbr)
2727 struct isl_vec *sample;
2729 if (!cgbr->shifted)
2730 gbr_init_shifted(cgbr);
2731 if (!cgbr->shifted)
2732 return NULL;
2733 if (cgbr->shifted->empty)
2734 return isl_vec_alloc(cgbr->tab->mat->ctx, 0);
2736 sample = isl_tab_get_sample_value(cgbr->shifted);
2737 sample = isl_vec_ceil(sample);
2739 return sample;
2742 static struct isl_basic_set *drop_constant_terms(struct isl_basic_set *bset)
2744 int i;
2746 if (!bset)
2747 return NULL;
2749 for (i = 0; i < bset->n_eq; ++i)
2750 isl_int_set_si(bset->eq[i][0], 0);
2752 for (i = 0; i < bset->n_ineq; ++i)
2753 isl_int_set_si(bset->ineq[i][0], 0);
2755 return bset;
2758 static int use_shifted(struct isl_context_gbr *cgbr)
2760 if (!cgbr->tab)
2761 return 0;
2762 return cgbr->tab->bmap->n_eq == 0 && cgbr->tab->bmap->n_div == 0;
2765 static struct isl_vec *gbr_get_sample(struct isl_context_gbr *cgbr)
2767 struct isl_basic_set *bset;
2768 struct isl_basic_set *cone;
2770 if (isl_tab_sample_is_integer(cgbr->tab))
2771 return isl_tab_get_sample_value(cgbr->tab);
2773 if (use_shifted(cgbr)) {
2774 struct isl_vec *sample;
2776 sample = gbr_get_shifted_sample(cgbr);
2777 if (!sample || sample->size > 0)
2778 return sample;
2780 isl_vec_free(sample);
2783 if (!cgbr->cone) {
2784 bset = isl_tab_peek_bset(cgbr->tab);
2785 cgbr->cone = isl_tab_from_recession_cone(bset, 0);
2786 if (!cgbr->cone)
2787 return NULL;
2788 if (isl_tab_track_bset(cgbr->cone,
2789 isl_basic_set_copy(bset)) < 0)
2790 return NULL;
2792 if (isl_tab_detect_implicit_equalities(cgbr->cone) < 0)
2793 return NULL;
2795 if (cgbr->cone->n_dead == cgbr->cone->n_col) {
2796 struct isl_vec *sample;
2797 struct isl_tab_undo *snap;
2799 if (cgbr->tab->basis) {
2800 if (cgbr->tab->basis->n_col != 1 + cgbr->tab->n_var) {
2801 isl_mat_free(cgbr->tab->basis);
2802 cgbr->tab->basis = NULL;
2804 cgbr->tab->n_zero = 0;
2805 cgbr->tab->n_unbounded = 0;
2808 snap = isl_tab_snap(cgbr->tab);
2810 sample = isl_tab_sample(cgbr->tab);
2812 if (!sample || isl_tab_rollback(cgbr->tab, snap) < 0) {
2813 isl_vec_free(sample);
2814 return NULL;
2817 return sample;
2820 cone = isl_basic_set_dup(isl_tab_peek_bset(cgbr->cone));
2821 cone = drop_constant_terms(cone);
2822 cone = isl_basic_set_update_from_tab(cone, cgbr->cone);
2823 cone = isl_basic_set_underlying_set(cone);
2824 cone = isl_basic_set_gauss(cone, NULL);
2826 bset = isl_basic_set_dup(isl_tab_peek_bset(cgbr->tab));
2827 bset = isl_basic_set_update_from_tab(bset, cgbr->tab);
2828 bset = isl_basic_set_underlying_set(bset);
2829 bset = isl_basic_set_gauss(bset, NULL);
2831 return isl_basic_set_sample_with_cone(bset, cone);
2834 static void check_gbr_integer_feasible(struct isl_context_gbr *cgbr)
2836 struct isl_vec *sample;
2838 if (!cgbr->tab)
2839 return;
2841 if (cgbr->tab->empty)
2842 return;
2844 sample = gbr_get_sample(cgbr);
2845 if (!sample)
2846 goto error;
2848 if (sample->size == 0) {
2849 isl_vec_free(sample);
2850 if (isl_tab_mark_empty(cgbr->tab) < 0)
2851 goto error;
2852 return;
2855 if (isl_tab_add_sample(cgbr->tab, sample) < 0)
2856 goto error;
2858 return;
2859 error:
2860 isl_tab_free(cgbr->tab);
2861 cgbr->tab = NULL;
2864 static struct isl_tab *add_gbr_eq(struct isl_tab *tab, isl_int *eq)
2866 if (!tab)
2867 return NULL;
2869 if (isl_tab_extend_cons(tab, 2) < 0)
2870 goto error;
2872 if (isl_tab_add_eq(tab, eq) < 0)
2873 goto error;
2875 return tab;
2876 error:
2877 isl_tab_free(tab);
2878 return NULL;
2881 /* Add the equality described by "eq" to the context.
2882 * If "check" is set, then we check if the context is empty after
2883 * adding the equality.
2884 * If "update" is set, then we check if the samples are still valid.
2886 * We do not explicitly add shifted copies of the equality to
2887 * cgbr->shifted since they would conflict with each other.
2888 * Instead, we directly mark cgbr->shifted empty.
2890 static void context_gbr_add_eq(struct isl_context *context, isl_int *eq,
2891 int check, int update)
2893 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2895 cgbr->tab = add_gbr_eq(cgbr->tab, eq);
2897 if (cgbr->shifted && !cgbr->shifted->empty && use_shifted(cgbr)) {
2898 if (isl_tab_mark_empty(cgbr->shifted) < 0)
2899 goto error;
2902 if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
2903 if (isl_tab_extend_cons(cgbr->cone, 2) < 0)
2904 goto error;
2905 if (isl_tab_add_eq(cgbr->cone, eq) < 0)
2906 goto error;
2909 if (check) {
2910 int v = tab_has_valid_sample(cgbr->tab, eq, 1);
2911 if (v < 0)
2912 goto error;
2913 if (!v)
2914 check_gbr_integer_feasible(cgbr);
2916 if (update)
2917 cgbr->tab = check_samples(cgbr->tab, eq, 1);
2918 return;
2919 error:
2920 isl_tab_free(cgbr->tab);
2921 cgbr->tab = NULL;
2924 static void add_gbr_ineq(struct isl_context_gbr *cgbr, isl_int *ineq)
2926 if (!cgbr->tab)
2927 return;
2929 if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
2930 goto error;
2932 if (isl_tab_add_ineq(cgbr->tab, ineq) < 0)
2933 goto error;
2935 if (cgbr->shifted && !cgbr->shifted->empty && use_shifted(cgbr)) {
2936 int i;
2937 unsigned dim;
2938 dim = isl_basic_map_total_dim(cgbr->tab->bmap);
2940 if (isl_tab_extend_cons(cgbr->shifted, 1) < 0)
2941 goto error;
2943 for (i = 0; i < dim; ++i) {
2944 if (!isl_int_is_neg(ineq[1 + i]))
2945 continue;
2946 isl_int_add(ineq[0], ineq[0], ineq[1 + i]);
2949 if (isl_tab_add_ineq(cgbr->shifted, ineq) < 0)
2950 goto error;
2952 for (i = 0; i < dim; ++i) {
2953 if (!isl_int_is_neg(ineq[1 + i]))
2954 continue;
2955 isl_int_sub(ineq[0], ineq[0], ineq[1 + i]);
2959 if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
2960 if (isl_tab_extend_cons(cgbr->cone, 1) < 0)
2961 goto error;
2962 if (isl_tab_add_ineq(cgbr->cone, ineq) < 0)
2963 goto error;
2966 return;
2967 error:
2968 isl_tab_free(cgbr->tab);
2969 cgbr->tab = NULL;
2972 static void context_gbr_add_ineq(struct isl_context *context, isl_int *ineq,
2973 int check, int update)
2975 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2977 add_gbr_ineq(cgbr, ineq);
2978 if (!cgbr->tab)
2979 return;
2981 if (check) {
2982 int v = tab_has_valid_sample(cgbr->tab, ineq, 0);
2983 if (v < 0)
2984 goto error;
2985 if (!v)
2986 check_gbr_integer_feasible(cgbr);
2988 if (update)
2989 cgbr->tab = check_samples(cgbr->tab, ineq, 0);
2990 return;
2991 error:
2992 isl_tab_free(cgbr->tab);
2993 cgbr->tab = NULL;
2996 static int context_gbr_add_ineq_wrap(void *user, isl_int *ineq)
2998 struct isl_context *context = (struct isl_context *)user;
2999 context_gbr_add_ineq(context, ineq, 0, 0);
3000 return context->op->is_ok(context) ? 0 : -1;
3003 static enum isl_tab_row_sign context_gbr_ineq_sign(struct isl_context *context,
3004 isl_int *ineq, int strict)
3006 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3007 return tab_ineq_sign(cgbr->tab, ineq, strict);
3010 /* Check whether "ineq" can be added to the tableau without rendering
3011 * it infeasible.
3013 static int context_gbr_test_ineq(struct isl_context *context, isl_int *ineq)
3015 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3016 struct isl_tab_undo *snap;
3017 struct isl_tab_undo *shifted_snap = NULL;
3018 struct isl_tab_undo *cone_snap = NULL;
3019 int feasible;
3021 if (!cgbr->tab)
3022 return -1;
3024 if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
3025 return -1;
3027 snap = isl_tab_snap(cgbr->tab);
3028 if (cgbr->shifted)
3029 shifted_snap = isl_tab_snap(cgbr->shifted);
3030 if (cgbr->cone)
3031 cone_snap = isl_tab_snap(cgbr->cone);
3032 add_gbr_ineq(cgbr, ineq);
3033 check_gbr_integer_feasible(cgbr);
3034 if (!cgbr->tab)
3035 return -1;
3036 feasible = !cgbr->tab->empty;
3037 if (isl_tab_rollback(cgbr->tab, snap) < 0)
3038 return -1;
3039 if (shifted_snap) {
3040 if (isl_tab_rollback(cgbr->shifted, shifted_snap))
3041 return -1;
3042 } else if (cgbr->shifted) {
3043 isl_tab_free(cgbr->shifted);
3044 cgbr->shifted = NULL;
3046 if (cone_snap) {
3047 if (isl_tab_rollback(cgbr->cone, cone_snap))
3048 return -1;
3049 } else if (cgbr->cone) {
3050 isl_tab_free(cgbr->cone);
3051 cgbr->cone = NULL;
3054 return feasible;
3057 /* Return the column of the last of the variables associated to
3058 * a column that has a non-zero coefficient.
3059 * This function is called in a context where only coefficients
3060 * of parameters or divs can be non-zero.
3062 static int last_non_zero_var_col(struct isl_tab *tab, isl_int *p)
3064 int i;
3065 int col;
3067 if (tab->n_var == 0)
3068 return -1;
3070 for (i = tab->n_var - 1; i >= 0; --i) {
3071 if (i >= tab->n_param && i < tab->n_var - tab->n_div)
3072 continue;
3073 if (tab->var[i].is_row)
3074 continue;
3075 col = tab->var[i].index;
3076 if (!isl_int_is_zero(p[col]))
3077 return col;
3080 return -1;
3083 /* Look through all the recently added equalities in the context
3084 * to see if we can propagate any of them to the main tableau.
3086 * The newly added equalities in the context are encoded as pairs
3087 * of inequalities starting at inequality "first".
3089 * We tentatively add each of these equalities to the main tableau
3090 * and if this happens to result in a row with a final coefficient
3091 * that is one or negative one, we use it to kill a column
3092 * in the main tableau. Otherwise, we discard the tentatively
3093 * added row.
3094 * This tentative addition of equality constraints turns
3095 * on the undo facility of the tableau. Turn it off again
3096 * at the end, assuming it was turned off to begin with.
3098 * Return 0 on success and -1 on failure.
3100 static int propagate_equalities(struct isl_context_gbr *cgbr,
3101 struct isl_tab *tab, unsigned first)
3103 int i;
3104 struct isl_vec *eq = NULL;
3105 isl_bool needs_undo;
3107 needs_undo = isl_tab_need_undo(tab);
3108 if (needs_undo < 0)
3109 goto error;
3110 eq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
3111 if (!eq)
3112 goto error;
3114 if (isl_tab_extend_cons(tab, (cgbr->tab->bmap->n_ineq - first)/2) < 0)
3115 goto error;
3117 isl_seq_clr(eq->el + 1 + tab->n_param,
3118 tab->n_var - tab->n_param - tab->n_div);
3119 for (i = first; i < cgbr->tab->bmap->n_ineq; i += 2) {
3120 int j;
3121 int r;
3122 struct isl_tab_undo *snap;
3123 snap = isl_tab_snap(tab);
3125 isl_seq_cpy(eq->el, cgbr->tab->bmap->ineq[i], 1 + tab->n_param);
3126 isl_seq_cpy(eq->el + 1 + tab->n_var - tab->n_div,
3127 cgbr->tab->bmap->ineq[i] + 1 + tab->n_param,
3128 tab->n_div);
3130 r = isl_tab_add_row(tab, eq->el);
3131 if (r < 0)
3132 goto error;
3133 r = tab->con[r].index;
3134 j = last_non_zero_var_col(tab, tab->mat->row[r] + 2 + tab->M);
3135 if (j < 0 || j < tab->n_dead ||
3136 !isl_int_is_one(tab->mat->row[r][0]) ||
3137 (!isl_int_is_one(tab->mat->row[r][2 + tab->M + j]) &&
3138 !isl_int_is_negone(tab->mat->row[r][2 + tab->M + j]))) {
3139 if (isl_tab_rollback(tab, snap) < 0)
3140 goto error;
3141 continue;
3143 if (isl_tab_pivot(tab, r, j) < 0)
3144 goto error;
3145 if (isl_tab_kill_col(tab, j) < 0)
3146 goto error;
3148 if (restore_lexmin(tab) < 0)
3149 goto error;
3152 if (!needs_undo)
3153 isl_tab_clear_undo(tab);
3154 isl_vec_free(eq);
3156 return 0;
3157 error:
3158 isl_vec_free(eq);
3159 isl_tab_free(cgbr->tab);
3160 cgbr->tab = NULL;
3161 return -1;
3164 static int context_gbr_detect_equalities(struct isl_context *context,
3165 struct isl_tab *tab)
3167 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3168 unsigned n_ineq;
3170 if (!cgbr->cone) {
3171 struct isl_basic_set *bset = isl_tab_peek_bset(cgbr->tab);
3172 cgbr->cone = isl_tab_from_recession_cone(bset, 0);
3173 if (!cgbr->cone)
3174 goto error;
3175 if (isl_tab_track_bset(cgbr->cone,
3176 isl_basic_set_copy(bset)) < 0)
3177 goto error;
3179 if (isl_tab_detect_implicit_equalities(cgbr->cone) < 0)
3180 goto error;
3182 n_ineq = cgbr->tab->bmap->n_ineq;
3183 cgbr->tab = isl_tab_detect_equalities(cgbr->tab, cgbr->cone);
3184 if (!cgbr->tab)
3185 return -1;
3186 if (cgbr->tab->bmap->n_ineq > n_ineq &&
3187 propagate_equalities(cgbr, tab, n_ineq) < 0)
3188 return -1;
3190 return 0;
3191 error:
3192 isl_tab_free(cgbr->tab);
3193 cgbr->tab = NULL;
3194 return -1;
3197 static int context_gbr_get_div(struct isl_context *context, struct isl_tab *tab,
3198 struct isl_vec *div)
3200 return get_div(tab, context, div);
3203 static int context_gbr_add_div(struct isl_context *context,
3204 __isl_keep isl_vec *div)
3206 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3207 if (cgbr->cone) {
3208 int k;
3210 if (isl_tab_extend_cons(cgbr->cone, 3) < 0)
3211 return -1;
3212 if (isl_tab_extend_vars(cgbr->cone, 1) < 0)
3213 return -1;
3214 if (isl_tab_allocate_var(cgbr->cone) <0)
3215 return -1;
3217 cgbr->cone->bmap = isl_basic_map_extend_space(cgbr->cone->bmap,
3218 isl_basic_map_get_space(cgbr->cone->bmap), 1, 0, 2);
3219 k = isl_basic_map_alloc_div(cgbr->cone->bmap);
3220 if (k < 0)
3221 return -1;
3222 isl_seq_cpy(cgbr->cone->bmap->div[k], div->el, div->size);
3223 if (isl_tab_push(cgbr->cone, isl_tab_undo_bmap_div) < 0)
3224 return -1;
3226 return context_tab_add_div(cgbr->tab, div,
3227 context_gbr_add_ineq_wrap, context);
3230 static int context_gbr_best_split(struct isl_context *context,
3231 struct isl_tab *tab)
3233 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3234 struct isl_tab_undo *snap;
3235 int r;
3237 snap = isl_tab_snap(cgbr->tab);
3238 r = best_split(tab, cgbr->tab);
3240 if (r >= 0 && isl_tab_rollback(cgbr->tab, snap) < 0)
3241 return -1;
3243 return r;
3246 static int context_gbr_is_empty(struct isl_context *context)
3248 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3249 if (!cgbr->tab)
3250 return -1;
3251 return cgbr->tab->empty;
3254 struct isl_gbr_tab_undo {
3255 struct isl_tab_undo *tab_snap;
3256 struct isl_tab_undo *shifted_snap;
3257 struct isl_tab_undo *cone_snap;
3260 static void *context_gbr_save(struct isl_context *context)
3262 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3263 struct isl_gbr_tab_undo *snap;
3265 if (!cgbr->tab)
3266 return NULL;
3268 snap = isl_alloc_type(cgbr->tab->mat->ctx, struct isl_gbr_tab_undo);
3269 if (!snap)
3270 return NULL;
3272 snap->tab_snap = isl_tab_snap(cgbr->tab);
3273 if (isl_tab_save_samples(cgbr->tab) < 0)
3274 goto error;
3276 if (cgbr->shifted)
3277 snap->shifted_snap = isl_tab_snap(cgbr->shifted);
3278 else
3279 snap->shifted_snap = NULL;
3281 if (cgbr->cone)
3282 snap->cone_snap = isl_tab_snap(cgbr->cone);
3283 else
3284 snap->cone_snap = NULL;
3286 return snap;
3287 error:
3288 free(snap);
3289 return NULL;
3292 static void context_gbr_restore(struct isl_context *context, void *save)
3294 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3295 struct isl_gbr_tab_undo *snap = (struct isl_gbr_tab_undo *)save;
3296 if (!snap)
3297 goto error;
3298 if (isl_tab_rollback(cgbr->tab, snap->tab_snap) < 0)
3299 goto error;
3301 if (snap->shifted_snap) {
3302 if (isl_tab_rollback(cgbr->shifted, snap->shifted_snap) < 0)
3303 goto error;
3304 } else if (cgbr->shifted) {
3305 isl_tab_free(cgbr->shifted);
3306 cgbr->shifted = NULL;
3309 if (snap->cone_snap) {
3310 if (isl_tab_rollback(cgbr->cone, snap->cone_snap) < 0)
3311 goto error;
3312 } else if (cgbr->cone) {
3313 isl_tab_free(cgbr->cone);
3314 cgbr->cone = NULL;
3317 free(snap);
3319 return;
3320 error:
3321 free(snap);
3322 isl_tab_free(cgbr->tab);
3323 cgbr->tab = NULL;
3326 static void context_gbr_discard(void *save)
3328 struct isl_gbr_tab_undo *snap = (struct isl_gbr_tab_undo *)save;
3329 free(snap);
3332 static int context_gbr_is_ok(struct isl_context *context)
3334 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3335 return !!cgbr->tab;
3338 static void context_gbr_invalidate(struct isl_context *context)
3340 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3341 isl_tab_free(cgbr->tab);
3342 cgbr->tab = NULL;
3345 static void context_gbr_free(struct isl_context *context)
3347 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3348 isl_tab_free(cgbr->tab);
3349 isl_tab_free(cgbr->shifted);
3350 isl_tab_free(cgbr->cone);
3351 free(cgbr);
3354 struct isl_context_op isl_context_gbr_op = {
3355 context_gbr_detect_nonnegative_parameters,
3356 context_gbr_peek_basic_set,
3357 context_gbr_peek_tab,
3358 context_gbr_add_eq,
3359 context_gbr_add_ineq,
3360 context_gbr_ineq_sign,
3361 context_gbr_test_ineq,
3362 context_gbr_get_div,
3363 context_gbr_add_div,
3364 context_gbr_detect_equalities,
3365 context_gbr_best_split,
3366 context_gbr_is_empty,
3367 context_gbr_is_ok,
3368 context_gbr_save,
3369 context_gbr_restore,
3370 context_gbr_discard,
3371 context_gbr_invalidate,
3372 context_gbr_free,
3375 static struct isl_context *isl_context_gbr_alloc(__isl_keep isl_basic_set *dom)
3377 struct isl_context_gbr *cgbr;
3379 if (!dom)
3380 return NULL;
3382 cgbr = isl_calloc_type(dom->ctx, struct isl_context_gbr);
3383 if (!cgbr)
3384 return NULL;
3386 cgbr->context.op = &isl_context_gbr_op;
3388 cgbr->shifted = NULL;
3389 cgbr->cone = NULL;
3390 cgbr->tab = isl_tab_from_basic_set(dom, 1);
3391 cgbr->tab = isl_tab_init_samples(cgbr->tab);
3392 if (!cgbr->tab)
3393 goto error;
3394 check_gbr_integer_feasible(cgbr);
3396 return &cgbr->context;
3397 error:
3398 cgbr->context.op->free(&cgbr->context);
3399 return NULL;
3402 static struct isl_context *isl_context_alloc(__isl_keep isl_basic_set *dom)
3404 if (!dom)
3405 return NULL;
3407 if (dom->ctx->opt->context == ISL_CONTEXT_LEXMIN)
3408 return isl_context_lex_alloc(dom);
3409 else
3410 return isl_context_gbr_alloc(dom);
3413 /* Construct an isl_sol_map structure for accumulating the solution.
3414 * If track_empty is set, then we also keep track of the parts
3415 * of the context where there is no solution.
3416 * If max is set, then we are solving a maximization, rather than
3417 * a minimization problem, which means that the variables in the
3418 * tableau have value "M - x" rather than "M + x".
3420 static struct isl_sol *sol_map_init(struct isl_basic_map *bmap,
3421 struct isl_basic_set *dom, int track_empty, int max)
3423 struct isl_sol_map *sol_map = NULL;
3425 if (!bmap)
3426 goto error;
3428 sol_map = isl_calloc_type(bmap->ctx, struct isl_sol_map);
3429 if (!sol_map)
3430 goto error;
3432 sol_map->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
3433 sol_map->sol.dec_level.callback.run = &sol_dec_level_wrap;
3434 sol_map->sol.dec_level.sol = &sol_map->sol;
3435 sol_map->sol.max = max;
3436 sol_map->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
3437 sol_map->sol.add = &sol_map_add_wrap;
3438 sol_map->sol.add_empty = track_empty ? &sol_map_add_empty_wrap : NULL;
3439 sol_map->sol.free = &sol_map_free_wrap;
3440 sol_map->map = isl_map_alloc_space(isl_basic_map_get_space(bmap), 1,
3441 ISL_MAP_DISJOINT);
3442 if (!sol_map->map)
3443 goto error;
3445 sol_map->sol.context = isl_context_alloc(dom);
3446 if (!sol_map->sol.context)
3447 goto error;
3449 if (track_empty) {
3450 sol_map->empty = isl_set_alloc_space(isl_basic_set_get_space(dom),
3451 1, ISL_SET_DISJOINT);
3452 if (!sol_map->empty)
3453 goto error;
3456 isl_basic_set_free(dom);
3457 return &sol_map->sol;
3458 error:
3459 isl_basic_set_free(dom);
3460 sol_map_free(sol_map);
3461 return NULL;
3464 /* Check whether all coefficients of (non-parameter) variables
3465 * are non-positive, meaning that no pivots can be performed on the row.
3467 static int is_critical(struct isl_tab *tab, int row)
3469 int j;
3470 unsigned off = 2 + tab->M;
3472 for (j = tab->n_dead; j < tab->n_col; ++j) {
3473 if (tab->col_var[j] >= 0 &&
3474 (tab->col_var[j] < tab->n_param ||
3475 tab->col_var[j] >= tab->n_var - tab->n_div))
3476 continue;
3478 if (isl_int_is_pos(tab->mat->row[row][off + j]))
3479 return 0;
3482 return 1;
3485 /* Check whether the inequality represented by vec is strict over the integers,
3486 * i.e., there are no integer values satisfying the constraint with
3487 * equality. This happens if the gcd of the coefficients is not a divisor
3488 * of the constant term. If so, scale the constraint down by the gcd
3489 * of the coefficients.
3491 static int is_strict(struct isl_vec *vec)
3493 isl_int gcd;
3494 int strict = 0;
3496 isl_int_init(gcd);
3497 isl_seq_gcd(vec->el + 1, vec->size - 1, &gcd);
3498 if (!isl_int_is_one(gcd)) {
3499 strict = !isl_int_is_divisible_by(vec->el[0], gcd);
3500 isl_int_fdiv_q(vec->el[0], vec->el[0], gcd);
3501 isl_seq_scale_down(vec->el + 1, vec->el + 1, gcd, vec->size-1);
3503 isl_int_clear(gcd);
3505 return strict;
3508 /* Determine the sign of the given row of the main tableau.
3509 * The result is one of
3510 * isl_tab_row_pos: always non-negative; no pivot needed
3511 * isl_tab_row_neg: always non-positive; pivot
3512 * isl_tab_row_any: can be both positive and negative; split
3514 * We first handle some simple cases
3515 * - the row sign may be known already
3516 * - the row may be obviously non-negative
3517 * - the parametric constant may be equal to that of another row
3518 * for which we know the sign. This sign will be either "pos" or
3519 * "any". If it had been "neg" then we would have pivoted before.
3521 * If none of these cases hold, we check the value of the row for each
3522 * of the currently active samples. Based on the signs of these values
3523 * we make an initial determination of the sign of the row.
3525 * all zero -> unk(nown)
3526 * all non-negative -> pos
3527 * all non-positive -> neg
3528 * both negative and positive -> all
3530 * If we end up with "all", we are done.
3531 * Otherwise, we perform a check for positive and/or negative
3532 * values as follows.
3534 * samples neg unk pos
3535 * <0 ? Y N Y N
3536 * pos any pos
3537 * >0 ? Y N Y N
3538 * any neg any neg
3540 * There is no special sign for "zero", because we can usually treat zero
3541 * as either non-negative or non-positive, whatever works out best.
3542 * However, if the row is "critical", meaning that pivoting is impossible
3543 * then we don't want to limp zero with the non-positive case, because
3544 * then we we would lose the solution for those values of the parameters
3545 * where the value of the row is zero. Instead, we treat 0 as non-negative
3546 * ensuring a split if the row can attain both zero and negative values.
3547 * The same happens when the original constraint was one that could not
3548 * be satisfied with equality by any integer values of the parameters.
3549 * In this case, we normalize the constraint, but then a value of zero
3550 * for the normalized constraint is actually a positive value for the
3551 * original constraint, so again we need to treat zero as non-negative.
3552 * In both these cases, we have the following decision tree instead:
3554 * all non-negative -> pos
3555 * all negative -> neg
3556 * both negative and non-negative -> all
3558 * samples neg pos
3559 * <0 ? Y N
3560 * any pos
3561 * >=0 ? Y N
3562 * any neg
3564 static enum isl_tab_row_sign row_sign(struct isl_tab *tab,
3565 struct isl_sol *sol, int row)
3567 struct isl_vec *ineq = NULL;
3568 enum isl_tab_row_sign res = isl_tab_row_unknown;
3569 int critical;
3570 int strict;
3571 int row2;
3573 if (tab->row_sign[row] != isl_tab_row_unknown)
3574 return tab->row_sign[row];
3575 if (is_obviously_nonneg(tab, row))
3576 return isl_tab_row_pos;
3577 for (row2 = tab->n_redundant; row2 < tab->n_row; ++row2) {
3578 if (tab->row_sign[row2] == isl_tab_row_unknown)
3579 continue;
3580 if (identical_parameter_line(tab, row, row2))
3581 return tab->row_sign[row2];
3584 critical = is_critical(tab, row);
3586 ineq = get_row_parameter_ineq(tab, row);
3587 if (!ineq)
3588 goto error;
3590 strict = is_strict(ineq);
3592 res = sol->context->op->ineq_sign(sol->context, ineq->el,
3593 critical || strict);
3595 if (res == isl_tab_row_unknown || res == isl_tab_row_pos) {
3596 /* test for negative values */
3597 int feasible;
3598 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3599 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3601 feasible = sol->context->op->test_ineq(sol->context, ineq->el);
3602 if (feasible < 0)
3603 goto error;
3604 if (!feasible)
3605 res = isl_tab_row_pos;
3606 else
3607 res = (res == isl_tab_row_unknown) ? isl_tab_row_neg
3608 : isl_tab_row_any;
3609 if (res == isl_tab_row_neg) {
3610 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3611 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3615 if (res == isl_tab_row_neg) {
3616 /* test for positive values */
3617 int feasible;
3618 if (!critical && !strict)
3619 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3621 feasible = sol->context->op->test_ineq(sol->context, ineq->el);
3622 if (feasible < 0)
3623 goto error;
3624 if (feasible)
3625 res = isl_tab_row_any;
3628 isl_vec_free(ineq);
3629 return res;
3630 error:
3631 isl_vec_free(ineq);
3632 return isl_tab_row_unknown;
3635 static void find_solutions(struct isl_sol *sol, struct isl_tab *tab);
3637 /* Find solutions for values of the parameters that satisfy the given
3638 * inequality.
3640 * We currently take a snapshot of the context tableau that is reset
3641 * when we return from this function, while we make a copy of the main
3642 * tableau, leaving the original main tableau untouched.
3643 * These are fairly arbitrary choices. Making a copy also of the context
3644 * tableau would obviate the need to undo any changes made to it later,
3645 * while taking a snapshot of the main tableau could reduce memory usage.
3646 * If we were to switch to taking a snapshot of the main tableau,
3647 * we would have to keep in mind that we need to save the row signs
3648 * and that we need to do this before saving the current basis
3649 * such that the basis has been restore before we restore the row signs.
3651 static void find_in_pos(struct isl_sol *sol, struct isl_tab *tab, isl_int *ineq)
3653 void *saved;
3655 if (!sol->context)
3656 goto error;
3657 saved = sol->context->op->save(sol->context);
3659 tab = isl_tab_dup(tab);
3660 if (!tab)
3661 goto error;
3663 sol->context->op->add_ineq(sol->context, ineq, 0, 1);
3665 find_solutions(sol, tab);
3667 if (!sol->error)
3668 sol->context->op->restore(sol->context, saved);
3669 else
3670 sol->context->op->discard(saved);
3671 return;
3672 error:
3673 sol->error = 1;
3676 /* Record the absence of solutions for those values of the parameters
3677 * that do not satisfy the given inequality with equality.
3679 static void no_sol_in_strict(struct isl_sol *sol,
3680 struct isl_tab *tab, struct isl_vec *ineq)
3682 int empty;
3683 void *saved;
3685 if (!sol->context || sol->error)
3686 goto error;
3687 saved = sol->context->op->save(sol->context);
3689 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3691 sol->context->op->add_ineq(sol->context, ineq->el, 1, 0);
3692 if (!sol->context)
3693 goto error;
3695 empty = tab->empty;
3696 tab->empty = 1;
3697 sol_add(sol, tab);
3698 tab->empty = empty;
3700 isl_int_add_ui(ineq->el[0], ineq->el[0], 1);
3702 sol->context->op->restore(sol->context, saved);
3703 return;
3704 error:
3705 sol->error = 1;
3708 /* Reset all row variables that are marked to have a sign that may
3709 * be both positive and negative to have an unknown sign.
3711 static void reset_any_to_unknown(struct isl_tab *tab)
3713 int row;
3715 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3716 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
3717 continue;
3718 if (tab->row_sign[row] == isl_tab_row_any)
3719 tab->row_sign[row] = isl_tab_row_unknown;
3723 /* Compute the lexicographic minimum of the set represented by the main
3724 * tableau "tab" within the context "sol->context_tab".
3725 * On entry the sample value of the main tableau is lexicographically
3726 * less than or equal to this lexicographic minimum.
3727 * Pivots are performed until a feasible point is found, which is then
3728 * necessarily equal to the minimum, or until the tableau is found to
3729 * be infeasible. Some pivots may need to be performed for only some
3730 * feasible values of the context tableau. If so, the context tableau
3731 * is split into a part where the pivot is needed and a part where it is not.
3733 * Whenever we enter the main loop, the main tableau is such that no
3734 * "obvious" pivots need to be performed on it, where "obvious" means
3735 * that the given row can be seen to be negative without looking at
3736 * the context tableau. In particular, for non-parametric problems,
3737 * no pivots need to be performed on the main tableau.
3738 * The caller of find_solutions is responsible for making this property
3739 * hold prior to the first iteration of the loop, while restore_lexmin
3740 * is called before every other iteration.
3742 * Inside the main loop, we first examine the signs of the rows of
3743 * the main tableau within the context of the context tableau.
3744 * If we find a row that is always non-positive for all values of
3745 * the parameters satisfying the context tableau and negative for at
3746 * least one value of the parameters, we perform the appropriate pivot
3747 * and start over. An exception is the case where no pivot can be
3748 * performed on the row. In this case, we require that the sign of
3749 * the row is negative for all values of the parameters (rather than just
3750 * non-positive). This special case is handled inside row_sign, which
3751 * will say that the row can have any sign if it determines that it can
3752 * attain both negative and zero values.
3754 * If we can't find a row that always requires a pivot, but we can find
3755 * one or more rows that require a pivot for some values of the parameters
3756 * (i.e., the row can attain both positive and negative signs), then we split
3757 * the context tableau into two parts, one where we force the sign to be
3758 * non-negative and one where we force is to be negative.
3759 * The non-negative part is handled by a recursive call (through find_in_pos).
3760 * Upon returning from this call, we continue with the negative part and
3761 * perform the required pivot.
3763 * If no such rows can be found, all rows are non-negative and we have
3764 * found a (rational) feasible point. If we only wanted a rational point
3765 * then we are done.
3766 * Otherwise, we check if all values of the sample point of the tableau
3767 * are integral for the variables. If so, we have found the minimal
3768 * integral point and we are done.
3769 * If the sample point is not integral, then we need to make a distinction
3770 * based on whether the constant term is non-integral or the coefficients
3771 * of the parameters. Furthermore, in order to decide how to handle
3772 * the non-integrality, we also need to know whether the coefficients
3773 * of the other columns in the tableau are integral. This leads
3774 * to the following table. The first two rows do not correspond
3775 * to a non-integral sample point and are only mentioned for completeness.
3777 * constant parameters other
3779 * int int int |
3780 * int int rat | -> no problem
3782 * rat int int -> fail
3784 * rat int rat -> cut
3786 * int rat rat |
3787 * rat rat rat | -> parametric cut
3789 * int rat int |
3790 * rat rat int | -> split context
3792 * If the parametric constant is completely integral, then there is nothing
3793 * to be done. If the constant term is non-integral, but all the other
3794 * coefficient are integral, then there is nothing that can be done
3795 * and the tableau has no integral solution.
3796 * If, on the other hand, one or more of the other columns have rational
3797 * coefficients, but the parameter coefficients are all integral, then
3798 * we can perform a regular (non-parametric) cut.
3799 * Finally, if there is any parameter coefficient that is non-integral,
3800 * then we need to involve the context tableau. There are two cases here.
3801 * If at least one other column has a rational coefficient, then we
3802 * can perform a parametric cut in the main tableau by adding a new
3803 * integer division in the context tableau.
3804 * If all other columns have integral coefficients, then we need to
3805 * enforce that the rational combination of parameters (c + \sum a_i y_i)/m
3806 * is always integral. We do this by introducing an integer division
3807 * q = floor((c + \sum a_i y_i)/m) and stipulating that its argument should
3808 * always be integral in the context tableau, i.e., m q = c + \sum a_i y_i.
3809 * Since q is expressed in the tableau as
3810 * c + \sum a_i y_i - m q >= 0
3811 * -c - \sum a_i y_i + m q + m - 1 >= 0
3812 * it is sufficient to add the inequality
3813 * -c - \sum a_i y_i + m q >= 0
3814 * In the part of the context where this inequality does not hold, the
3815 * main tableau is marked as being empty.
3817 static void find_solutions(struct isl_sol *sol, struct isl_tab *tab)
3819 struct isl_context *context;
3820 int r;
3822 if (!tab || sol->error)
3823 goto error;
3825 context = sol->context;
3827 if (tab->empty)
3828 goto done;
3829 if (context->op->is_empty(context))
3830 goto done;
3832 for (r = 0; r >= 0 && tab && !tab->empty; r = restore_lexmin(tab)) {
3833 int flags;
3834 int row;
3835 enum isl_tab_row_sign sgn;
3836 int split = -1;
3837 int n_split = 0;
3839 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3840 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
3841 continue;
3842 sgn = row_sign(tab, sol, row);
3843 if (!sgn)
3844 goto error;
3845 tab->row_sign[row] = sgn;
3846 if (sgn == isl_tab_row_any)
3847 n_split++;
3848 if (sgn == isl_tab_row_any && split == -1)
3849 split = row;
3850 if (sgn == isl_tab_row_neg)
3851 break;
3853 if (row < tab->n_row)
3854 continue;
3855 if (split != -1) {
3856 struct isl_vec *ineq;
3857 if (n_split != 1)
3858 split = context->op->best_split(context, tab);
3859 if (split < 0)
3860 goto error;
3861 ineq = get_row_parameter_ineq(tab, split);
3862 if (!ineq)
3863 goto error;
3864 is_strict(ineq);
3865 reset_any_to_unknown(tab);
3866 tab->row_sign[split] = isl_tab_row_pos;
3867 sol_inc_level(sol);
3868 find_in_pos(sol, tab, ineq->el);
3869 tab->row_sign[split] = isl_tab_row_neg;
3870 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3871 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3872 if (!sol->error)
3873 context->op->add_ineq(context, ineq->el, 0, 1);
3874 isl_vec_free(ineq);
3875 if (sol->error)
3876 goto error;
3877 continue;
3879 if (tab->rational)
3880 break;
3881 row = first_non_integer_row(tab, &flags);
3882 if (row < 0)
3883 break;
3884 if (ISL_FL_ISSET(flags, I_PAR)) {
3885 if (ISL_FL_ISSET(flags, I_VAR)) {
3886 if (isl_tab_mark_empty(tab) < 0)
3887 goto error;
3888 break;
3890 row = add_cut(tab, row);
3891 } else if (ISL_FL_ISSET(flags, I_VAR)) {
3892 struct isl_vec *div;
3893 struct isl_vec *ineq;
3894 int d;
3895 div = get_row_split_div(tab, row);
3896 if (!div)
3897 goto error;
3898 d = context->op->get_div(context, tab, div);
3899 isl_vec_free(div);
3900 if (d < 0)
3901 goto error;
3902 ineq = ineq_for_div(context->op->peek_basic_set(context), d);
3903 if (!ineq)
3904 goto error;
3905 sol_inc_level(sol);
3906 no_sol_in_strict(sol, tab, ineq);
3907 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3908 context->op->add_ineq(context, ineq->el, 1, 1);
3909 isl_vec_free(ineq);
3910 if (sol->error || !context->op->is_ok(context))
3911 goto error;
3912 tab = set_row_cst_to_div(tab, row, d);
3913 if (context->op->is_empty(context))
3914 break;
3915 } else
3916 row = add_parametric_cut(tab, row, context);
3917 if (row < 0)
3918 goto error;
3920 if (r < 0)
3921 goto error;
3922 done:
3923 sol_add(sol, tab);
3924 isl_tab_free(tab);
3925 return;
3926 error:
3927 isl_tab_free(tab);
3928 sol->error = 1;
3931 /* Does "sol" contain a pair of partial solutions that could potentially
3932 * be merged?
3934 * We currently only check that "sol" is not in an error state
3935 * and that there are at least two partial solutions of which the final two
3936 * are defined at the same level.
3938 static int sol_has_mergeable_solutions(struct isl_sol *sol)
3940 if (sol->error)
3941 return 0;
3942 if (!sol->partial)
3943 return 0;
3944 if (!sol->partial->next)
3945 return 0;
3946 return sol->partial->level == sol->partial->next->level;
3949 /* Compute the lexicographic minimum of the set represented by the main
3950 * tableau "tab" within the context "sol->context_tab".
3952 * As a preprocessing step, we first transfer all the purely parametric
3953 * equalities from the main tableau to the context tableau, i.e.,
3954 * parameters that have been pivoted to a row.
3955 * These equalities are ignored by the main algorithm, because the
3956 * corresponding rows may not be marked as being non-negative.
3957 * In parts of the context where the added equality does not hold,
3958 * the main tableau is marked as being empty.
3960 * Before we embark on the actual computation, we save a copy
3961 * of the context. When we return, we check if there are any
3962 * partial solutions that can potentially be merged. If so,
3963 * we perform a rollback to the initial state of the context.
3964 * The merging of partial solutions happens inside calls to
3965 * sol_dec_level that are pushed onto the undo stack of the context.
3966 * If there are no partial solutions that can potentially be merged
3967 * then the rollback is skipped as it would just be wasted effort.
3969 static void find_solutions_main(struct isl_sol *sol, struct isl_tab *tab)
3971 int row;
3972 void *saved;
3974 if (!tab)
3975 goto error;
3977 sol->level = 0;
3979 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3980 int p;
3981 struct isl_vec *eq;
3983 if (tab->row_var[row] < 0)
3984 continue;
3985 if (tab->row_var[row] >= tab->n_param &&
3986 tab->row_var[row] < tab->n_var - tab->n_div)
3987 continue;
3988 if (tab->row_var[row] < tab->n_param)
3989 p = tab->row_var[row];
3990 else
3991 p = tab->row_var[row]
3992 + tab->n_param - (tab->n_var - tab->n_div);
3994 eq = isl_vec_alloc(tab->mat->ctx, 1+tab->n_param+tab->n_div);
3995 if (!eq)
3996 goto error;
3997 get_row_parameter_line(tab, row, eq->el);
3998 isl_int_neg(eq->el[1 + p], tab->mat->row[row][0]);
3999 eq = isl_vec_normalize(eq);
4001 sol_inc_level(sol);
4002 no_sol_in_strict(sol, tab, eq);
4004 isl_seq_neg(eq->el, eq->el, eq->size);
4005 sol_inc_level(sol);
4006 no_sol_in_strict(sol, tab, eq);
4007 isl_seq_neg(eq->el, eq->el, eq->size);
4009 sol->context->op->add_eq(sol->context, eq->el, 1, 1);
4011 isl_vec_free(eq);
4013 if (isl_tab_mark_redundant(tab, row) < 0)
4014 goto error;
4016 if (sol->context->op->is_empty(sol->context))
4017 break;
4019 row = tab->n_redundant - 1;
4022 saved = sol->context->op->save(sol->context);
4024 find_solutions(sol, tab);
4026 if (sol_has_mergeable_solutions(sol))
4027 sol->context->op->restore(sol->context, saved);
4028 else
4029 sol->context->op->discard(saved);
4031 sol->level = 0;
4032 sol_pop(sol);
4034 return;
4035 error:
4036 isl_tab_free(tab);
4037 sol->error = 1;
4040 /* Check if integer division "div" of "dom" also occurs in "bmap".
4041 * If so, return its position within the divs.
4042 * If not, return -1.
4044 static int find_context_div(struct isl_basic_map *bmap,
4045 struct isl_basic_set *dom, unsigned div)
4047 int i;
4048 unsigned b_dim = isl_space_dim(bmap->dim, isl_dim_all);
4049 unsigned d_dim = isl_space_dim(dom->dim, isl_dim_all);
4051 if (isl_int_is_zero(dom->div[div][0]))
4052 return -1;
4053 if (isl_seq_first_non_zero(dom->div[div] + 2 + d_dim, dom->n_div) != -1)
4054 return -1;
4056 for (i = 0; i < bmap->n_div; ++i) {
4057 if (isl_int_is_zero(bmap->div[i][0]))
4058 continue;
4059 if (isl_seq_first_non_zero(bmap->div[i] + 2 + d_dim,
4060 (b_dim - d_dim) + bmap->n_div) != -1)
4061 continue;
4062 if (isl_seq_eq(bmap->div[i], dom->div[div], 2 + d_dim))
4063 return i;
4065 return -1;
4068 /* The correspondence between the variables in the main tableau,
4069 * the context tableau, and the input map and domain is as follows.
4070 * The first n_param and the last n_div variables of the main tableau
4071 * form the variables of the context tableau.
4072 * In the basic map, these n_param variables correspond to the
4073 * parameters and the input dimensions. In the domain, they correspond
4074 * to the parameters and the set dimensions.
4075 * The n_div variables correspond to the integer divisions in the domain.
4076 * To ensure that everything lines up, we may need to copy some of the
4077 * integer divisions of the domain to the map. These have to be placed
4078 * in the same order as those in the context and they have to be placed
4079 * after any other integer divisions that the map may have.
4080 * This function performs the required reordering.
4082 static struct isl_basic_map *align_context_divs(struct isl_basic_map *bmap,
4083 struct isl_basic_set *dom)
4085 int i;
4086 int common = 0;
4087 int other;
4089 for (i = 0; i < dom->n_div; ++i)
4090 if (find_context_div(bmap, dom, i) != -1)
4091 common++;
4092 other = bmap->n_div - common;
4093 if (dom->n_div - common > 0) {
4094 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
4095 dom->n_div - common, 0, 0);
4096 if (!bmap)
4097 return NULL;
4099 for (i = 0; i < dom->n_div; ++i) {
4100 int pos = find_context_div(bmap, dom, i);
4101 if (pos < 0) {
4102 pos = isl_basic_map_alloc_div(bmap);
4103 if (pos < 0)
4104 goto error;
4105 isl_int_set_si(bmap->div[pos][0], 0);
4107 if (pos != other + i)
4108 isl_basic_map_swap_div(bmap, pos, other + i);
4110 return bmap;
4111 error:
4112 isl_basic_map_free(bmap);
4113 return NULL;
4116 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4117 * some obvious symmetries.
4119 * We make sure the divs in the domain are properly ordered,
4120 * because they will be added one by one in the given order
4121 * during the construction of the solution map.
4123 static struct isl_sol *basic_map_partial_lexopt_base_sol(
4124 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4125 __isl_give isl_set **empty, int max,
4126 struct isl_sol *(*init)(__isl_keep isl_basic_map *bmap,
4127 __isl_take isl_basic_set *dom, int track_empty, int max))
4129 struct isl_tab *tab;
4130 struct isl_sol *sol = NULL;
4131 struct isl_context *context;
4133 if (dom->n_div) {
4134 dom = isl_basic_set_order_divs(dom);
4135 bmap = align_context_divs(bmap, dom);
4137 sol = init(bmap, dom, !!empty, max);
4138 if (!sol)
4139 goto error;
4141 context = sol->context;
4142 if (isl_basic_set_plain_is_empty(context->op->peek_basic_set(context)))
4143 /* nothing */;
4144 else if (isl_basic_map_plain_is_empty(bmap)) {
4145 if (sol->add_empty)
4146 sol->add_empty(sol,
4147 isl_basic_set_copy(context->op->peek_basic_set(context)));
4148 } else {
4149 tab = tab_for_lexmin(bmap,
4150 context->op->peek_basic_set(context), 1, max);
4151 tab = context->op->detect_nonnegative_parameters(context, tab);
4152 find_solutions_main(sol, tab);
4154 if (sol->error)
4155 goto error;
4157 isl_basic_map_free(bmap);
4158 return sol;
4159 error:
4160 sol_free(sol);
4161 isl_basic_map_free(bmap);
4162 return NULL;
4165 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4166 * some obvious symmetries.
4168 * We call basic_map_partial_lexopt_base_sol and extract the results.
4170 static __isl_give isl_map *basic_map_partial_lexopt_base(
4171 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4172 __isl_give isl_set **empty, int max)
4174 isl_map *result = NULL;
4175 struct isl_sol *sol;
4176 struct isl_sol_map *sol_map;
4178 sol = basic_map_partial_lexopt_base_sol(bmap, dom, empty, max,
4179 &sol_map_init);
4180 if (!sol)
4181 return NULL;
4182 sol_map = (struct isl_sol_map *) sol;
4184 result = isl_map_copy(sol_map->map);
4185 if (empty)
4186 *empty = isl_set_copy(sol_map->empty);
4187 sol_free(&sol_map->sol);
4188 return result;
4191 /* Return a count of the number of occurrences of the "n" first
4192 * variables in the inequality constraints of "bmap".
4194 static __isl_give int *count_occurrences(__isl_keep isl_basic_map *bmap,
4195 int n)
4197 int i, j;
4198 isl_ctx *ctx;
4199 int *occurrences;
4201 if (!bmap)
4202 return NULL;
4203 ctx = isl_basic_map_get_ctx(bmap);
4204 occurrences = isl_calloc_array(ctx, int, n);
4205 if (!occurrences)
4206 return NULL;
4208 for (i = 0; i < bmap->n_ineq; ++i) {
4209 for (j = 0; j < n; ++j) {
4210 if (!isl_int_is_zero(bmap->ineq[i][1 + j]))
4211 occurrences[j]++;
4215 return occurrences;
4218 /* Do all of the "n" variables with non-zero coefficients in "c"
4219 * occur in exactly a single constraint.
4220 * "occurrences" is an array of length "n" containing the number
4221 * of occurrences of each of the variables in the inequality constraints.
4223 static int single_occurrence(int n, isl_int *c, int *occurrences)
4225 int i;
4227 for (i = 0; i < n; ++i) {
4228 if (isl_int_is_zero(c[i]))
4229 continue;
4230 if (occurrences[i] != 1)
4231 return 0;
4234 return 1;
4237 /* Do all of the "n" initial variables that occur in inequality constraint
4238 * "ineq" of "bmap" only occur in that constraint?
4240 static int all_single_occurrence(__isl_keep isl_basic_map *bmap, int ineq,
4241 int n)
4243 int i, j;
4245 for (i = 0; i < n; ++i) {
4246 if (isl_int_is_zero(bmap->ineq[ineq][1 + i]))
4247 continue;
4248 for (j = 0; j < bmap->n_ineq; ++j) {
4249 if (j == ineq)
4250 continue;
4251 if (!isl_int_is_zero(bmap->ineq[j][1 + i]))
4252 return 0;
4256 return 1;
4259 /* Structure used during detection of parallel constraints.
4260 * n_in: number of "input" variables: isl_dim_param + isl_dim_in
4261 * n_out: number of "output" variables: isl_dim_out + isl_dim_div
4262 * val: the coefficients of the output variables
4264 struct isl_constraint_equal_info {
4265 isl_basic_map *bmap;
4266 unsigned n_in;
4267 unsigned n_out;
4268 isl_int *val;
4271 /* Check whether the coefficients of the output variables
4272 * of the constraint in "entry" are equal to info->val.
4274 static int constraint_equal(const void *entry, const void *val)
4276 isl_int **row = (isl_int **)entry;
4277 const struct isl_constraint_equal_info *info = val;
4279 return isl_seq_eq((*row) + 1 + info->n_in, info->val, info->n_out);
4282 /* Check whether "bmap" has a pair of constraints that have
4283 * the same coefficients for the output variables.
4284 * Note that the coefficients of the existentially quantified
4285 * variables need to be zero since the existentially quantified
4286 * of the result are usually not the same as those of the input.
4287 * Furthermore, check that each of the input variables that occur
4288 * in those constraints does not occur in any other constraint.
4289 * If so, return 1 and return the row indices of the two constraints
4290 * in *first and *second.
4292 static int parallel_constraints(__isl_keep isl_basic_map *bmap,
4293 int *first, int *second)
4295 int i;
4296 isl_ctx *ctx;
4297 int *occurrences = NULL;
4298 struct isl_hash_table *table = NULL;
4299 struct isl_hash_table_entry *entry;
4300 struct isl_constraint_equal_info info;
4301 unsigned n_out;
4302 unsigned n_div;
4304 ctx = isl_basic_map_get_ctx(bmap);
4305 table = isl_hash_table_alloc(ctx, bmap->n_ineq);
4306 if (!table)
4307 goto error;
4309 info.n_in = isl_basic_map_dim(bmap, isl_dim_param) +
4310 isl_basic_map_dim(bmap, isl_dim_in);
4311 occurrences = count_occurrences(bmap, info.n_in);
4312 if (info.n_in && !occurrences)
4313 goto error;
4314 info.bmap = bmap;
4315 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4316 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4317 info.n_out = n_out + n_div;
4318 for (i = 0; i < bmap->n_ineq; ++i) {
4319 uint32_t hash;
4321 info.val = bmap->ineq[i] + 1 + info.n_in;
4322 if (isl_seq_first_non_zero(info.val, n_out) < 0)
4323 continue;
4324 if (isl_seq_first_non_zero(info.val + n_out, n_div) >= 0)
4325 continue;
4326 if (!single_occurrence(info.n_in, bmap->ineq[i] + 1,
4327 occurrences))
4328 continue;
4329 hash = isl_seq_get_hash(info.val, info.n_out);
4330 entry = isl_hash_table_find(ctx, table, hash,
4331 constraint_equal, &info, 1);
4332 if (!entry)
4333 goto error;
4334 if (entry->data)
4335 break;
4336 entry->data = &bmap->ineq[i];
4339 if (i < bmap->n_ineq) {
4340 *first = ((isl_int **)entry->data) - bmap->ineq;
4341 *second = i;
4344 isl_hash_table_free(ctx, table);
4345 free(occurrences);
4347 return i < bmap->n_ineq;
4348 error:
4349 isl_hash_table_free(ctx, table);
4350 free(occurrences);
4351 return -1;
4354 /* Given a set of upper bounds in "var", add constraints to "bset"
4355 * that make the i-th bound smallest.
4357 * In particular, if there are n bounds b_i, then add the constraints
4359 * b_i <= b_j for j > i
4360 * b_i < b_j for j < i
4362 static __isl_give isl_basic_set *select_minimum(__isl_take isl_basic_set *bset,
4363 __isl_keep isl_mat *var, int i)
4365 isl_ctx *ctx;
4366 int j, k;
4368 ctx = isl_mat_get_ctx(var);
4370 for (j = 0; j < var->n_row; ++j) {
4371 if (j == i)
4372 continue;
4373 k = isl_basic_set_alloc_inequality(bset);
4374 if (k < 0)
4375 goto error;
4376 isl_seq_combine(bset->ineq[k], ctx->one, var->row[j],
4377 ctx->negone, var->row[i], var->n_col);
4378 isl_int_set_si(bset->ineq[k][var->n_col], 0);
4379 if (j < i)
4380 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
4383 bset = isl_basic_set_finalize(bset);
4385 return bset;
4386 error:
4387 isl_basic_set_free(bset);
4388 return NULL;
4391 /* Given a set of upper bounds on the last "input" variable m,
4392 * construct a set that assigns the minimal upper bound to m, i.e.,
4393 * construct a set that divides the space into cells where one
4394 * of the upper bounds is smaller than all the others and assign
4395 * this upper bound to m.
4397 * In particular, if there are n bounds b_i, then the result
4398 * consists of n basic sets, each one of the form
4400 * m = b_i
4401 * b_i <= b_j for j > i
4402 * b_i < b_j for j < i
4404 static __isl_give isl_set *set_minimum(__isl_take isl_space *dim,
4405 __isl_take isl_mat *var)
4407 int i, k;
4408 isl_basic_set *bset = NULL;
4409 isl_set *set = NULL;
4411 if (!dim || !var)
4412 goto error;
4414 set = isl_set_alloc_space(isl_space_copy(dim),
4415 var->n_row, ISL_SET_DISJOINT);
4417 for (i = 0; i < var->n_row; ++i) {
4418 bset = isl_basic_set_alloc_space(isl_space_copy(dim), 0,
4419 1, var->n_row - 1);
4420 k = isl_basic_set_alloc_equality(bset);
4421 if (k < 0)
4422 goto error;
4423 isl_seq_cpy(bset->eq[k], var->row[i], var->n_col);
4424 isl_int_set_si(bset->eq[k][var->n_col], -1);
4425 bset = select_minimum(bset, var, i);
4426 set = isl_set_add_basic_set(set, bset);
4429 isl_space_free(dim);
4430 isl_mat_free(var);
4431 return set;
4432 error:
4433 isl_basic_set_free(bset);
4434 isl_set_free(set);
4435 isl_space_free(dim);
4436 isl_mat_free(var);
4437 return NULL;
4440 /* Given that the last input variable of "bmap" represents the minimum
4441 * of the bounds in "cst", check whether we need to split the domain
4442 * based on which bound attains the minimum.
4444 * A split is needed when the minimum appears in an integer division
4445 * or in an equality. Otherwise, it is only needed if it appears in
4446 * an upper bound that is different from the upper bounds on which it
4447 * is defined.
4449 static int need_split_basic_map(__isl_keep isl_basic_map *bmap,
4450 __isl_keep isl_mat *cst)
4452 int i, j;
4453 unsigned total;
4454 unsigned pos;
4456 pos = cst->n_col - 1;
4457 total = isl_basic_map_dim(bmap, isl_dim_all);
4459 for (i = 0; i < bmap->n_div; ++i)
4460 if (!isl_int_is_zero(bmap->div[i][2 + pos]))
4461 return 1;
4463 for (i = 0; i < bmap->n_eq; ++i)
4464 if (!isl_int_is_zero(bmap->eq[i][1 + pos]))
4465 return 1;
4467 for (i = 0; i < bmap->n_ineq; ++i) {
4468 if (isl_int_is_nonneg(bmap->ineq[i][1 + pos]))
4469 continue;
4470 if (!isl_int_is_negone(bmap->ineq[i][1 + pos]))
4471 return 1;
4472 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + pos + 1,
4473 total - pos - 1) >= 0)
4474 return 1;
4476 for (j = 0; j < cst->n_row; ++j)
4477 if (isl_seq_eq(bmap->ineq[i], cst->row[j], cst->n_col))
4478 break;
4479 if (j >= cst->n_row)
4480 return 1;
4483 return 0;
4486 /* Given that the last set variable of "bset" represents the minimum
4487 * of the bounds in "cst", check whether we need to split the domain
4488 * based on which bound attains the minimum.
4490 * We simply call need_split_basic_map here. This is safe because
4491 * the position of the minimum is computed from "cst" and not
4492 * from "bmap".
4494 static int need_split_basic_set(__isl_keep isl_basic_set *bset,
4495 __isl_keep isl_mat *cst)
4497 return need_split_basic_map((isl_basic_map *)bset, cst);
4500 /* Given that the last set variable of "set" represents the minimum
4501 * of the bounds in "cst", check whether we need to split the domain
4502 * based on which bound attains the minimum.
4504 static int need_split_set(__isl_keep isl_set *set, __isl_keep isl_mat *cst)
4506 int i;
4508 for (i = 0; i < set->n; ++i)
4509 if (need_split_basic_set(set->p[i], cst))
4510 return 1;
4512 return 0;
4515 /* Given a set of which the last set variable is the minimum
4516 * of the bounds in "cst", split each basic set in the set
4517 * in pieces where one of the bounds is (strictly) smaller than the others.
4518 * This subdivision is given in "min_expr".
4519 * The variable is subsequently projected out.
4521 * We only do the split when it is needed.
4522 * For example if the last input variable m = min(a,b) and the only
4523 * constraints in the given basic set are lower bounds on m,
4524 * i.e., l <= m = min(a,b), then we can simply project out m
4525 * to obtain l <= a and l <= b, without having to split on whether
4526 * m is equal to a or b.
4528 static __isl_give isl_set *split(__isl_take isl_set *empty,
4529 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
4531 int n_in;
4532 int i;
4533 isl_space *dim;
4534 isl_set *res;
4536 if (!empty || !min_expr || !cst)
4537 goto error;
4539 n_in = isl_set_dim(empty, isl_dim_set);
4540 dim = isl_set_get_space(empty);
4541 dim = isl_space_drop_dims(dim, isl_dim_set, n_in - 1, 1);
4542 res = isl_set_empty(dim);
4544 for (i = 0; i < empty->n; ++i) {
4545 isl_set *set;
4547 set = isl_set_from_basic_set(isl_basic_set_copy(empty->p[i]));
4548 if (need_split_basic_set(empty->p[i], cst))
4549 set = isl_set_intersect(set, isl_set_copy(min_expr));
4550 set = isl_set_remove_dims(set, isl_dim_set, n_in - 1, 1);
4552 res = isl_set_union_disjoint(res, set);
4555 isl_set_free(empty);
4556 isl_set_free(min_expr);
4557 isl_mat_free(cst);
4558 return res;
4559 error:
4560 isl_set_free(empty);
4561 isl_set_free(min_expr);
4562 isl_mat_free(cst);
4563 return NULL;
4566 /* Given a map of which the last input variable is the minimum
4567 * of the bounds in "cst", split each basic set in the set
4568 * in pieces where one of the bounds is (strictly) smaller than the others.
4569 * This subdivision is given in "min_expr".
4570 * The variable is subsequently projected out.
4572 * The implementation is essentially the same as that of "split".
4574 static __isl_give isl_map *split_domain(__isl_take isl_map *opt,
4575 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
4577 int n_in;
4578 int i;
4579 isl_space *dim;
4580 isl_map *res;
4582 if (!opt || !min_expr || !cst)
4583 goto error;
4585 n_in = isl_map_dim(opt, isl_dim_in);
4586 dim = isl_map_get_space(opt);
4587 dim = isl_space_drop_dims(dim, isl_dim_in, n_in - 1, 1);
4588 res = isl_map_empty(dim);
4590 for (i = 0; i < opt->n; ++i) {
4591 isl_map *map;
4593 map = isl_map_from_basic_map(isl_basic_map_copy(opt->p[i]));
4594 if (need_split_basic_map(opt->p[i], cst))
4595 map = isl_map_intersect_domain(map,
4596 isl_set_copy(min_expr));
4597 map = isl_map_remove_dims(map, isl_dim_in, n_in - 1, 1);
4599 res = isl_map_union_disjoint(res, map);
4602 isl_map_free(opt);
4603 isl_set_free(min_expr);
4604 isl_mat_free(cst);
4605 return res;
4606 error:
4607 isl_map_free(opt);
4608 isl_set_free(min_expr);
4609 isl_mat_free(cst);
4610 return NULL;
4613 static __isl_give isl_map *basic_map_partial_lexopt(
4614 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4615 __isl_give isl_set **empty, int max);
4617 /* This function is called from basic_map_partial_lexopt_symm.
4618 * The last variable of "bmap" and "dom" corresponds to the minimum
4619 * of the bounds in "cst". "map_space" is the space of the original
4620 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
4621 * is the space of the original domain.
4623 * We recursively call basic_map_partial_lexopt and then plug in
4624 * the definition of the minimum in the result.
4626 static __isl_give isl_map *basic_map_partial_lexopt_symm_core(
4627 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4628 __isl_give isl_set **empty, int max, __isl_take isl_mat *cst,
4629 __isl_take isl_space *map_space, __isl_take isl_space *set_space)
4631 isl_map *opt;
4632 isl_set *min_expr;
4634 min_expr = set_minimum(isl_basic_set_get_space(dom), isl_mat_copy(cst));
4636 opt = basic_map_partial_lexopt(bmap, dom, empty, max);
4638 if (empty) {
4639 *empty = split(*empty,
4640 isl_set_copy(min_expr), isl_mat_copy(cst));
4641 *empty = isl_set_reset_space(*empty, set_space);
4644 opt = split_domain(opt, min_expr, cst);
4645 opt = isl_map_reset_space(opt, map_space);
4647 return opt;
4650 /* Extract a domain from "bmap" for the purpose of computing
4651 * a lexicographic optimum.
4653 * This function is only called when the caller wants to compute a full
4654 * lexicographic optimum, i.e., without specifying a domain. In this case,
4655 * the caller is not interested in the part of the domain space where
4656 * there is no solution and the domain can be initialized to those constraints
4657 * of "bmap" that only involve the parameters and the input dimensions.
4658 * This relieves the parametric programming engine from detecting those
4659 * inequalities and transferring them to the context. More importantly,
4660 * it ensures that those inequalities are transferred first and not
4661 * intermixed with inequalities that actually split the domain.
4663 * If the caller does not require the absence of existentially quantified
4664 * variables in the result (i.e., if ISL_OPT_QE is not set in "flags"),
4665 * then the actual domain of "bmap" can be used. This ensures that
4666 * the domain does not need to be split at all just to separate out
4667 * pieces of the domain that do not have a solution from piece that do.
4668 * This domain cannot be used in general because it may involve
4669 * (unknown) existentially quantified variables which will then also
4670 * appear in the solution.
4672 static __isl_give isl_basic_set *extract_domain(__isl_keep isl_basic_map *bmap,
4673 unsigned flags)
4675 int n_div;
4676 int n_out;
4678 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4679 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4680 bmap = isl_basic_map_copy(bmap);
4681 if (ISL_FL_ISSET(flags, ISL_OPT_QE)) {
4682 bmap = isl_basic_map_drop_constraints_involving_dims(bmap,
4683 isl_dim_div, 0, n_div);
4684 bmap = isl_basic_map_drop_constraints_involving_dims(bmap,
4685 isl_dim_out, 0, n_out);
4687 return isl_basic_map_domain(bmap);
4690 #undef TYPE
4691 #define TYPE isl_map
4692 #undef SUFFIX
4693 #define SUFFIX
4694 #include "isl_tab_lexopt_templ.c"
4696 struct isl_sol_for {
4697 struct isl_sol sol;
4698 int (*fn)(__isl_take isl_basic_set *dom,
4699 __isl_take isl_aff_list *list, void *user);
4700 void *user;
4703 static void sol_for_free(struct isl_sol_for *sol_for)
4705 if (!sol_for)
4706 return;
4707 if (sol_for->sol.context)
4708 sol_for->sol.context->op->free(sol_for->sol.context);
4709 free(sol_for);
4712 static void sol_for_free_wrap(struct isl_sol *sol)
4714 sol_for_free((struct isl_sol_for *)sol);
4717 /* Add the solution identified by the tableau and the context tableau.
4719 * See documentation of sol_add for more details.
4721 * Instead of constructing a basic map, this function calls a user
4722 * defined function with the current context as a basic set and
4723 * a list of affine expressions representing the relation between
4724 * the input and output. The space over which the affine expressions
4725 * are defined is the same as that of the domain. The number of
4726 * affine expressions in the list is equal to the number of output variables.
4728 static void sol_for_add(struct isl_sol_for *sol,
4729 struct isl_basic_set *dom, struct isl_mat *M)
4731 int i;
4732 isl_ctx *ctx;
4733 isl_local_space *ls;
4734 isl_aff *aff;
4735 isl_aff_list *list;
4737 if (sol->sol.error || !dom || !M)
4738 goto error;
4740 ctx = isl_basic_set_get_ctx(dom);
4741 ls = isl_basic_set_get_local_space(dom);
4742 list = isl_aff_list_alloc(ctx, M->n_row - 1);
4743 for (i = 1; i < M->n_row; ++i) {
4744 aff = isl_aff_alloc(isl_local_space_copy(ls));
4745 if (aff) {
4746 isl_int_set(aff->v->el[0], M->row[0][0]);
4747 isl_seq_cpy(aff->v->el + 1, M->row[i], M->n_col);
4749 aff = isl_aff_normalize(aff);
4750 list = isl_aff_list_add(list, aff);
4752 isl_local_space_free(ls);
4754 dom = isl_basic_set_finalize(dom);
4756 if (sol->fn(isl_basic_set_copy(dom), list, sol->user) < 0)
4757 goto error;
4759 isl_basic_set_free(dom);
4760 isl_mat_free(M);
4761 return;
4762 error:
4763 isl_basic_set_free(dom);
4764 isl_mat_free(M);
4765 sol->sol.error = 1;
4768 static void sol_for_add_wrap(struct isl_sol *sol,
4769 struct isl_basic_set *dom, struct isl_mat *M)
4771 sol_for_add((struct isl_sol_for *)sol, dom, M);
4774 static struct isl_sol_for *sol_for_init(struct isl_basic_map *bmap, int max,
4775 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4776 void *user),
4777 void *user)
4779 struct isl_sol_for *sol_for = NULL;
4780 isl_space *dom_dim;
4781 struct isl_basic_set *dom = NULL;
4783 sol_for = isl_calloc_type(bmap->ctx, struct isl_sol_for);
4784 if (!sol_for)
4785 goto error;
4787 dom_dim = isl_space_domain(isl_space_copy(bmap->dim));
4788 dom = isl_basic_set_universe(dom_dim);
4790 sol_for->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
4791 sol_for->sol.dec_level.callback.run = &sol_dec_level_wrap;
4792 sol_for->sol.dec_level.sol = &sol_for->sol;
4793 sol_for->fn = fn;
4794 sol_for->user = user;
4795 sol_for->sol.max = max;
4796 sol_for->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
4797 sol_for->sol.add = &sol_for_add_wrap;
4798 sol_for->sol.add_empty = NULL;
4799 sol_for->sol.free = &sol_for_free_wrap;
4801 sol_for->sol.context = isl_context_alloc(dom);
4802 if (!sol_for->sol.context)
4803 goto error;
4805 isl_basic_set_free(dom);
4806 return sol_for;
4807 error:
4808 isl_basic_set_free(dom);
4809 sol_for_free(sol_for);
4810 return NULL;
4813 static void sol_for_find_solutions(struct isl_sol_for *sol_for,
4814 struct isl_tab *tab)
4816 find_solutions_main(&sol_for->sol, tab);
4819 int isl_basic_map_foreach_lexopt(__isl_keep isl_basic_map *bmap, int max,
4820 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4821 void *user),
4822 void *user)
4824 struct isl_sol_for *sol_for = NULL;
4826 bmap = isl_basic_map_copy(bmap);
4827 bmap = isl_basic_map_detect_equalities(bmap);
4828 if (!bmap)
4829 return -1;
4831 sol_for = sol_for_init(bmap, max, fn, user);
4832 if (!sol_for)
4833 goto error;
4835 if (isl_basic_map_plain_is_empty(bmap))
4836 /* nothing */;
4837 else {
4838 struct isl_tab *tab;
4839 struct isl_context *context = sol_for->sol.context;
4840 tab = tab_for_lexmin(bmap,
4841 context->op->peek_basic_set(context), 1, max);
4842 tab = context->op->detect_nonnegative_parameters(context, tab);
4843 sol_for_find_solutions(sol_for, tab);
4844 if (sol_for->sol.error)
4845 goto error;
4848 sol_free(&sol_for->sol);
4849 isl_basic_map_free(bmap);
4850 return 0;
4851 error:
4852 sol_free(&sol_for->sol);
4853 isl_basic_map_free(bmap);
4854 return -1;
4857 int isl_basic_set_foreach_lexopt(__isl_keep isl_basic_set *bset, int max,
4858 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4859 void *user),
4860 void *user)
4862 return isl_basic_map_foreach_lexopt(bset, max, fn, user);
4865 /* Check if the given sequence of len variables starting at pos
4866 * represents a trivial (i.e., zero) solution.
4867 * The variables are assumed to be non-negative and to come in pairs,
4868 * with each pair representing a variable of unrestricted sign.
4869 * The solution is trivial if each such pair in the sequence consists
4870 * of two identical values, meaning that the variable being represented
4871 * has value zero.
4873 static int region_is_trivial(struct isl_tab *tab, int pos, int len)
4875 int i;
4877 if (len == 0)
4878 return 0;
4880 for (i = 0; i < len; i += 2) {
4881 int neg_row;
4882 int pos_row;
4884 neg_row = tab->var[pos + i].is_row ?
4885 tab->var[pos + i].index : -1;
4886 pos_row = tab->var[pos + i + 1].is_row ?
4887 tab->var[pos + i + 1].index : -1;
4889 if ((neg_row < 0 ||
4890 isl_int_is_zero(tab->mat->row[neg_row][1])) &&
4891 (pos_row < 0 ||
4892 isl_int_is_zero(tab->mat->row[pos_row][1])))
4893 continue;
4895 if (neg_row < 0 || pos_row < 0)
4896 return 0;
4897 if (isl_int_ne(tab->mat->row[neg_row][1],
4898 tab->mat->row[pos_row][1]))
4899 return 0;
4902 return 1;
4905 /* Return the index of the first trivial region or -1 if all regions
4906 * are non-trivial.
4908 static int first_trivial_region(struct isl_tab *tab,
4909 int n_region, struct isl_region *region)
4911 int i;
4913 for (i = 0; i < n_region; ++i) {
4914 if (region_is_trivial(tab, region[i].pos, region[i].len))
4915 return i;
4918 return -1;
4921 /* Check if the solution is optimal, i.e., whether the first
4922 * n_op entries are zero.
4924 static int is_optimal(__isl_keep isl_vec *sol, int n_op)
4926 int i;
4928 for (i = 0; i < n_op; ++i)
4929 if (!isl_int_is_zero(sol->el[1 + i]))
4930 return 0;
4931 return 1;
4934 /* Add constraints to "tab" that ensure that any solution is significantly
4935 * better than that represented by "sol". That is, find the first
4936 * relevant (within first n_op) non-zero coefficient and force it (along
4937 * with all previous coefficients) to be zero.
4938 * If the solution is already optimal (all relevant coefficients are zero),
4939 * then just mark the table as empty.
4941 * This function assumes that at least 2 * n_op more rows and at least
4942 * 2 * n_op more elements in the constraint array are available in the tableau.
4944 static int force_better_solution(struct isl_tab *tab,
4945 __isl_keep isl_vec *sol, int n_op)
4947 int i;
4948 isl_ctx *ctx;
4949 isl_vec *v = NULL;
4951 if (!sol)
4952 return -1;
4954 for (i = 0; i < n_op; ++i)
4955 if (!isl_int_is_zero(sol->el[1 + i]))
4956 break;
4958 if (i == n_op) {
4959 if (isl_tab_mark_empty(tab) < 0)
4960 return -1;
4961 return 0;
4964 ctx = isl_vec_get_ctx(sol);
4965 v = isl_vec_alloc(ctx, 1 + tab->n_var);
4966 if (!v)
4967 return -1;
4969 for (; i >= 0; --i) {
4970 v = isl_vec_clr(v);
4971 isl_int_set_si(v->el[1 + i], -1);
4972 if (add_lexmin_eq(tab, v->el) < 0)
4973 goto error;
4976 isl_vec_free(v);
4977 return 0;
4978 error:
4979 isl_vec_free(v);
4980 return -1;
4983 struct isl_trivial {
4984 int update;
4985 int region;
4986 int side;
4987 struct isl_tab_undo *snap;
4990 /* Return the lexicographically smallest non-trivial solution of the
4991 * given ILP problem.
4993 * All variables are assumed to be non-negative.
4995 * n_op is the number of initial coordinates to optimize.
4996 * That is, once a solution has been found, we will only continue looking
4997 * for solution that result in significantly better values for those
4998 * initial coordinates. That is, we only continue looking for solutions
4999 * that increase the number of initial zeros in this sequence.
5001 * A solution is non-trivial, if it is non-trivial on each of the
5002 * specified regions. Each region represents a sequence of pairs
5003 * of variables. A solution is non-trivial on such a region if
5004 * at least one of these pairs consists of different values, i.e.,
5005 * such that the non-negative variable represented by the pair is non-zero.
5007 * Whenever a conflict is encountered, all constraints involved are
5008 * reported to the caller through a call to "conflict".
5010 * We perform a simple branch-and-bound backtracking search.
5011 * Each level in the search represents initially trivial region that is forced
5012 * to be non-trivial.
5013 * At each level we consider n cases, where n is the length of the region.
5014 * In terms of the n/2 variables of unrestricted signs being encoded by
5015 * the region, we consider the cases
5016 * x_0 >= 1
5017 * x_0 <= -1
5018 * x_0 = 0 and x_1 >= 1
5019 * x_0 = 0 and x_1 <= -1
5020 * x_0 = 0 and x_1 = 0 and x_2 >= 1
5021 * x_0 = 0 and x_1 = 0 and x_2 <= -1
5022 * ...
5023 * The cases are considered in this order, assuming that each pair
5024 * x_i_a x_i_b represents the value x_i_b - x_i_a.
5025 * That is, x_0 >= 1 is enforced by adding the constraint
5026 * x_0_b - x_0_a >= 1
5028 __isl_give isl_vec *isl_tab_basic_set_non_trivial_lexmin(
5029 __isl_take isl_basic_set *bset, int n_op, int n_region,
5030 struct isl_region *region,
5031 int (*conflict)(int con, void *user), void *user)
5033 int i, j;
5034 int r;
5035 isl_ctx *ctx;
5036 isl_vec *v = NULL;
5037 isl_vec *sol = NULL;
5038 struct isl_tab *tab;
5039 struct isl_trivial *triv = NULL;
5040 int level, init;
5042 if (!bset)
5043 return NULL;
5045 ctx = isl_basic_set_get_ctx(bset);
5046 sol = isl_vec_alloc(ctx, 0);
5048 tab = tab_for_lexmin(bset, NULL, 0, 0);
5049 if (!tab)
5050 goto error;
5051 tab->conflict = conflict;
5052 tab->conflict_user = user;
5054 v = isl_vec_alloc(ctx, 1 + tab->n_var);
5055 triv = isl_calloc_array(ctx, struct isl_trivial, n_region);
5056 if (!v || (n_region && !triv))
5057 goto error;
5059 level = 0;
5060 init = 1;
5062 while (level >= 0) {
5063 int side, base;
5065 if (init) {
5066 tab = cut_to_integer_lexmin(tab, CUT_ONE);
5067 if (!tab)
5068 goto error;
5069 if (tab->empty)
5070 goto backtrack;
5071 r = first_trivial_region(tab, n_region, region);
5072 if (r < 0) {
5073 for (i = 0; i < level; ++i)
5074 triv[i].update = 1;
5075 isl_vec_free(sol);
5076 sol = isl_tab_get_sample_value(tab);
5077 if (!sol)
5078 goto error;
5079 if (is_optimal(sol, n_op))
5080 break;
5081 goto backtrack;
5083 if (level >= n_region)
5084 isl_die(ctx, isl_error_internal,
5085 "nesting level too deep", goto error);
5086 if (isl_tab_extend_cons(tab,
5087 2 * region[r].len + 2 * n_op) < 0)
5088 goto error;
5089 triv[level].region = r;
5090 triv[level].side = 0;
5093 r = triv[level].region;
5094 side = triv[level].side;
5095 base = 2 * (side/2);
5097 if (side >= region[r].len) {
5098 backtrack:
5099 level--;
5100 init = 0;
5101 if (level >= 0)
5102 if (isl_tab_rollback(tab, triv[level].snap) < 0)
5103 goto error;
5104 continue;
5107 if (triv[level].update) {
5108 if (force_better_solution(tab, sol, n_op) < 0)
5109 goto error;
5110 triv[level].update = 0;
5113 if (side == base && base >= 2) {
5114 for (j = base - 2; j < base; ++j) {
5115 v = isl_vec_clr(v);
5116 isl_int_set_si(v->el[1 + region[r].pos + j], 1);
5117 if (add_lexmin_eq(tab, v->el) < 0)
5118 goto error;
5122 triv[level].snap = isl_tab_snap(tab);
5123 if (isl_tab_push_basis(tab) < 0)
5124 goto error;
5126 v = isl_vec_clr(v);
5127 isl_int_set_si(v->el[0], -1);
5128 isl_int_set_si(v->el[1 + region[r].pos + side], -1);
5129 isl_int_set_si(v->el[1 + region[r].pos + (side ^ 1)], 1);
5130 tab = add_lexmin_ineq(tab, v->el);
5132 triv[level].side++;
5133 level++;
5134 init = 1;
5137 free(triv);
5138 isl_vec_free(v);
5139 isl_tab_free(tab);
5140 isl_basic_set_free(bset);
5142 return sol;
5143 error:
5144 free(triv);
5145 isl_vec_free(v);
5146 isl_tab_free(tab);
5147 isl_basic_set_free(bset);
5148 isl_vec_free(sol);
5149 return NULL;
5152 /* Wrapper for a tableau that is used for computing
5153 * the lexicographically smallest rational point of a non-negative set.
5154 * This point is represented by the sample value of "tab",
5155 * unless "tab" is empty.
5157 struct isl_tab_lexmin {
5158 isl_ctx *ctx;
5159 struct isl_tab *tab;
5162 /* Free "tl" and return NULL.
5164 __isl_null isl_tab_lexmin *isl_tab_lexmin_free(__isl_take isl_tab_lexmin *tl)
5166 if (!tl)
5167 return NULL;
5168 isl_ctx_deref(tl->ctx);
5169 isl_tab_free(tl->tab);
5170 free(tl);
5172 return NULL;
5175 /* Construct an isl_tab_lexmin for computing
5176 * the lexicographically smallest rational point in "bset",
5177 * assuming that all variables are non-negative.
5179 __isl_give isl_tab_lexmin *isl_tab_lexmin_from_basic_set(
5180 __isl_take isl_basic_set *bset)
5182 isl_ctx *ctx;
5183 isl_tab_lexmin *tl;
5185 if (!bset)
5186 return NULL;
5188 ctx = isl_basic_set_get_ctx(bset);
5189 tl = isl_calloc_type(ctx, struct isl_tab_lexmin);
5190 if (!tl)
5191 goto error;
5192 tl->ctx = ctx;
5193 isl_ctx_ref(ctx);
5194 tl->tab = tab_for_lexmin(bset, NULL, 0, 0);
5195 isl_basic_set_free(bset);
5196 if (!tl->tab)
5197 return isl_tab_lexmin_free(tl);
5198 return tl;
5199 error:
5200 isl_basic_set_free(bset);
5201 isl_tab_lexmin_free(tl);
5202 return NULL;
5205 /* Return the dimension of the set represented by "tl".
5207 int isl_tab_lexmin_dim(__isl_keep isl_tab_lexmin *tl)
5209 return tl ? tl->tab->n_var : -1;
5212 /* Add the equality with coefficients "eq" to "tl", updating the optimal
5213 * solution if needed.
5214 * The equality is added as two opposite inequality constraints.
5216 __isl_give isl_tab_lexmin *isl_tab_lexmin_add_eq(__isl_take isl_tab_lexmin *tl,
5217 isl_int *eq)
5219 unsigned n_var;
5221 if (!tl || !eq)
5222 return isl_tab_lexmin_free(tl);
5224 if (isl_tab_extend_cons(tl->tab, 2) < 0)
5225 return isl_tab_lexmin_free(tl);
5226 n_var = tl->tab->n_var;
5227 isl_seq_neg(eq, eq, 1 + n_var);
5228 tl->tab = add_lexmin_ineq(tl->tab, eq);
5229 isl_seq_neg(eq, eq, 1 + n_var);
5230 tl->tab = add_lexmin_ineq(tl->tab, eq);
5232 if (!tl->tab)
5233 return isl_tab_lexmin_free(tl);
5235 return tl;
5238 /* Return the lexicographically smallest rational point in the basic set
5239 * from which "tl" was constructed.
5240 * If the original input was empty, then return a zero-length vector.
5242 __isl_give isl_vec *isl_tab_lexmin_get_solution(__isl_keep isl_tab_lexmin *tl)
5244 if (!tl)
5245 return NULL;
5246 if (tl->tab->empty)
5247 return isl_vec_alloc(tl->ctx, 0);
5248 else
5249 return isl_tab_get_sample_value(tl->tab);
5252 /* Return the lexicographically smallest rational point in "bset",
5253 * assuming that all variables are non-negative.
5254 * If "bset" is empty, then return a zero-length vector.
5256 __isl_give isl_vec *isl_tab_basic_set_non_neg_lexmin(
5257 __isl_take isl_basic_set *bset)
5259 isl_tab_lexmin *tl;
5260 isl_vec *sol;
5262 tl = isl_tab_lexmin_from_basic_set(bset);
5263 sol = isl_tab_lexmin_get_solution(tl);
5264 isl_tab_lexmin_free(tl);
5265 return sol;
5268 struct isl_sol_pma {
5269 struct isl_sol sol;
5270 isl_pw_multi_aff *pma;
5271 isl_set *empty;
5274 static void sol_pma_free(struct isl_sol_pma *sol_pma)
5276 if (!sol_pma)
5277 return;
5278 if (sol_pma->sol.context)
5279 sol_pma->sol.context->op->free(sol_pma->sol.context);
5280 isl_pw_multi_aff_free(sol_pma->pma);
5281 isl_set_free(sol_pma->empty);
5282 free(sol_pma);
5285 /* This function is called for parts of the context where there is
5286 * no solution, with "bset" corresponding to the context tableau.
5287 * Simply add the basic set to the set "empty".
5289 static void sol_pma_add_empty(struct isl_sol_pma *sol,
5290 __isl_take isl_basic_set *bset)
5292 if (!bset || !sol->empty)
5293 goto error;
5295 sol->empty = isl_set_grow(sol->empty, 1);
5296 bset = isl_basic_set_simplify(bset);
5297 bset = isl_basic_set_finalize(bset);
5298 sol->empty = isl_set_add_basic_set(sol->empty, bset);
5299 if (!sol->empty)
5300 sol->sol.error = 1;
5301 return;
5302 error:
5303 isl_basic_set_free(bset);
5304 sol->sol.error = 1;
5307 /* Return the equality constraint in "bset" that defines existentially
5308 * quantified variable "pos" in terms of earlier dimensions.
5309 * The equality constraint is guaranteed to exist by the caller.
5310 * If "c" is not NULL, then it is the result of a previous call
5311 * to this function for the same variable, so simply return the input "c"
5312 * in that case.
5314 static __isl_give isl_constraint *get_equality(__isl_keep isl_basic_set *bset,
5315 int pos, __isl_take isl_constraint *c)
5317 int r;
5319 if (c)
5320 return c;
5321 r = isl_basic_set_has_defining_equality(bset, isl_dim_div, pos, &c);
5322 if (r < 0)
5323 return NULL;
5324 if (!r)
5325 isl_die(isl_basic_set_get_ctx(bset), isl_error_internal,
5326 "unexpected missing equality", return NULL);
5327 return c;
5330 /* Given a set "dom", of which only the first "n_known" existentially
5331 * quantified variables have a known explicit representation, and
5332 * a matrix "M", the rows of which are defined in terms of the dimensions
5333 * of "dom", eliminate all references to the existentially quantified
5334 * variables without a known explicit representation from "M"
5335 * by exploiting the equality constraints of "dom".
5337 * In particular, for each of those existentially quantified variables,
5338 * if there are non-zero entries in the corresponding column of "M",
5339 * then look for an equality constraint of "dom" that defines that variable
5340 * in terms of earlier variables and use it to clear the entries.
5342 * In particular, if the equality is of the form
5344 * f() + a alpha = 0
5346 * while the matrix entry is b/d (with d the global denominator of "M"),
5347 * then first scale the matrix such that the entry becomes b'/d' with
5348 * b' a multiple of a. Do this by multiplying the entire matrix
5349 * by abs(a/gcd(a,b)). Then subtract the equality multiplied by b'/a
5350 * from the row of "M" to clear the entry.
5352 static __isl_give isl_mat *eliminate_unknown_divs(__isl_take isl_mat *M,
5353 __isl_keep isl_basic_set *dom, int n_known)
5355 int i, j, n_div, off;
5356 isl_int t;
5357 isl_constraint *c = NULL;
5359 if (!M)
5360 return NULL;
5362 n_div = isl_basic_set_dim(dom, isl_dim_div);
5363 off = M->n_col - n_div;
5365 isl_int_init(t);
5366 for (i = n_div - 1; i >= n_known; --i) {
5367 for (j = 1; j < M->n_row; ++j) {
5368 if (isl_int_is_zero(M->row[j][off + i]))
5369 continue;
5370 c = get_equality(dom, i, c);
5371 if (!c)
5372 goto error;
5373 isl_int_gcd(t, M->row[j][off + i], c->v->el[off + i]);
5374 isl_int_divexact(t, c->v->el[off + i], t);
5375 isl_int_abs(t, t);
5376 M = isl_mat_scale(M, t);
5377 M = isl_mat_cow(M);
5378 if (!M)
5379 goto error;
5380 isl_int_divexact(t,
5381 M->row[j][off + i], c->v->el[off + i]);
5382 isl_seq_submul(M->row[j], t, c->v->el, M->n_col);
5384 c = isl_constraint_free(c);
5386 isl_int_clear(t);
5388 return M;
5389 error:
5390 isl_int_clear(t);
5391 isl_constraint_free(c);
5392 isl_mat_free(M);
5393 return NULL;
5396 /* Return the index of the last known div of "bset" after "start" and
5397 * up to (but not including) "end".
5398 * Return "start" if there is no such known div.
5400 static int last_known_div_after(__isl_keep isl_basic_set *bset,
5401 int start, int end)
5403 for (end = end - 1; end > start; --end) {
5404 if (isl_basic_set_div_is_known(bset, end))
5405 return end;
5408 return start;
5411 /* Set the affine expressions in "ma" according to the rows in "M", which
5412 * are defined over the local space "ls".
5413 * The matrix "M" may have extra (zero) columns beyond the number
5414 * of variables in "ls".
5416 static __isl_give isl_multi_aff *set_from_affine_matrix(
5417 __isl_take isl_multi_aff *ma, __isl_take isl_local_space *ls,
5418 __isl_take isl_mat *M)
5420 int i, dim;
5421 isl_aff *aff;
5423 if (!ma || !ls || !M)
5424 goto error;
5426 dim = isl_local_space_dim(ls, isl_dim_all);
5427 for (i = 1; i < M->n_row; ++i) {
5428 aff = isl_aff_alloc(isl_local_space_copy(ls));
5429 if (aff) {
5430 isl_int_set(aff->v->el[0], M->row[0][0]);
5431 isl_seq_cpy(aff->v->el + 1, M->row[i], 1 + dim);
5433 aff = isl_aff_normalize(aff);
5434 ma = isl_multi_aff_set_aff(ma, i - 1, aff);
5436 isl_local_space_free(ls);
5437 isl_mat_free(M);
5439 return ma;
5440 error:
5441 isl_local_space_free(ls);
5442 isl_mat_free(M);
5443 isl_multi_aff_free(ma);
5444 return NULL;
5447 /* Given a basic map "dom" that represents the context and an affine
5448 * matrix "M" that maps the dimensions of the context to the
5449 * output variables, construct an isl_pw_multi_aff with a single
5450 * cell corresponding to "dom" and affine expressions copied from "M".
5452 * Note that the description of the initial context may have involved
5453 * existentially quantified variables, in which case they also appear
5454 * in "dom". These need to be removed before creating the affine
5455 * expression because an affine expression cannot be defined in terms
5456 * of existentially quantified variables without a known representation.
5457 * In particular, they are first moved to the end in both "dom" and "M" and
5458 * then ignored in "M". In principle, the final columns of "M"
5459 * (i.e., those that will be ignored) should be zero at this stage
5460 * because align_context_divs adds the existentially quantified
5461 * variables of the context to the main tableau without any constraints.
5462 * The computed minimal value can therefore not depend on these variables.
5463 * However, additional integer divisions that get added for parametric cuts
5464 * get added to the end and they may happen to be equal to some affine
5465 * expression involving the original existentially quantified variables.
5466 * These equality constraints are then propagated to the main tableau
5467 * such that the computed minimum can in fact depend on those existentially
5468 * quantified variables. This dependence can however be removed again
5469 * by exploiting the equality constraints in "dom".
5470 * eliminate_unknown_divs takes care of this.
5472 static void sol_pma_add(struct isl_sol_pma *sol,
5473 __isl_take isl_basic_set *dom, __isl_take isl_mat *M)
5475 isl_local_space *ls;
5476 isl_multi_aff *maff;
5477 isl_pw_multi_aff *pma;
5478 int n_div, n_known, end, off;
5480 n_div = isl_basic_set_dim(dom, isl_dim_div);
5481 off = M->n_col - n_div;
5482 end = n_div;
5483 for (n_known = 0; n_known < end; ++n_known) {
5484 if (isl_basic_set_div_is_known(dom, n_known))
5485 continue;
5486 end = last_known_div_after(dom, n_known, end);
5487 if (end == n_known)
5488 break;
5489 isl_basic_set_swap_div(dom, n_known, end);
5490 M = isl_mat_swap_cols(M, off + n_known, off + end);
5492 dom = isl_basic_set_gauss(dom, NULL);
5493 if (n_known < n_div)
5494 M = eliminate_unknown_divs(M, dom, n_known);
5496 maff = isl_multi_aff_alloc(isl_pw_multi_aff_get_space(sol->pma));
5497 ls = isl_basic_set_get_local_space(dom);
5498 ls = isl_local_space_drop_dims(ls, isl_dim_div,
5499 n_known, n_div - n_known);
5500 maff = set_from_affine_matrix(maff, ls, M);
5501 dom = isl_basic_set_simplify(dom);
5502 dom = isl_basic_set_finalize(dom);
5503 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(dom), maff);
5504 sol->pma = isl_pw_multi_aff_add_disjoint(sol->pma, pma);
5505 if (!sol->pma)
5506 sol->sol.error = 1;
5509 static void sol_pma_free_wrap(struct isl_sol *sol)
5511 sol_pma_free((struct isl_sol_pma *)sol);
5514 static void sol_pma_add_empty_wrap(struct isl_sol *sol,
5515 __isl_take isl_basic_set *bset)
5517 sol_pma_add_empty((struct isl_sol_pma *)sol, bset);
5520 static void sol_pma_add_wrap(struct isl_sol *sol,
5521 __isl_take isl_basic_set *dom, __isl_take isl_mat *M)
5523 sol_pma_add((struct isl_sol_pma *)sol, dom, M);
5526 /* Construct an isl_sol_pma structure for accumulating the solution.
5527 * If track_empty is set, then we also keep track of the parts
5528 * of the context where there is no solution.
5529 * If max is set, then we are solving a maximization, rather than
5530 * a minimization problem, which means that the variables in the
5531 * tableau have value "M - x" rather than "M + x".
5533 static struct isl_sol *sol_pma_init(__isl_keep isl_basic_map *bmap,
5534 __isl_take isl_basic_set *dom, int track_empty, int max)
5536 struct isl_sol_pma *sol_pma = NULL;
5538 if (!bmap)
5539 goto error;
5541 sol_pma = isl_calloc_type(bmap->ctx, struct isl_sol_pma);
5542 if (!sol_pma)
5543 goto error;
5545 sol_pma->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
5546 sol_pma->sol.dec_level.callback.run = &sol_dec_level_wrap;
5547 sol_pma->sol.dec_level.sol = &sol_pma->sol;
5548 sol_pma->sol.max = max;
5549 sol_pma->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
5550 sol_pma->sol.add = &sol_pma_add_wrap;
5551 sol_pma->sol.add_empty = track_empty ? &sol_pma_add_empty_wrap : NULL;
5552 sol_pma->sol.free = &sol_pma_free_wrap;
5553 sol_pma->pma = isl_pw_multi_aff_empty(isl_basic_map_get_space(bmap));
5554 if (!sol_pma->pma)
5555 goto error;
5557 sol_pma->sol.context = isl_context_alloc(dom);
5558 if (!sol_pma->sol.context)
5559 goto error;
5561 if (track_empty) {
5562 sol_pma->empty = isl_set_alloc_space(isl_basic_set_get_space(dom),
5563 1, ISL_SET_DISJOINT);
5564 if (!sol_pma->empty)
5565 goto error;
5568 isl_basic_set_free(dom);
5569 return &sol_pma->sol;
5570 error:
5571 isl_basic_set_free(dom);
5572 sol_pma_free(sol_pma);
5573 return NULL;
5576 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
5577 * some obvious symmetries.
5579 * We call basic_map_partial_lexopt_base_sol and extract the results.
5581 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_base_pw_multi_aff(
5582 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5583 __isl_give isl_set **empty, int max)
5585 isl_pw_multi_aff *result = NULL;
5586 struct isl_sol *sol;
5587 struct isl_sol_pma *sol_pma;
5589 sol = basic_map_partial_lexopt_base_sol(bmap, dom, empty, max,
5590 &sol_pma_init);
5591 if (!sol)
5592 return NULL;
5593 sol_pma = (struct isl_sol_pma *) sol;
5595 result = isl_pw_multi_aff_copy(sol_pma->pma);
5596 if (empty)
5597 *empty = isl_set_copy(sol_pma->empty);
5598 sol_free(&sol_pma->sol);
5599 return result;
5602 /* Given that the last input variable of "maff" represents the minimum
5603 * of some bounds, check whether we need to plug in the expression
5604 * of the minimum.
5606 * In particular, check if the last input variable appears in any
5607 * of the expressions in "maff".
5609 static int need_substitution(__isl_keep isl_multi_aff *maff)
5611 int i;
5612 unsigned pos;
5614 pos = isl_multi_aff_dim(maff, isl_dim_in) - 1;
5616 for (i = 0; i < maff->n; ++i)
5617 if (isl_aff_involves_dims(maff->p[i], isl_dim_in, pos, 1))
5618 return 1;
5620 return 0;
5623 /* Given a set of upper bounds on the last "input" variable m,
5624 * construct a piecewise affine expression that selects
5625 * the minimal upper bound to m, i.e.,
5626 * divide the space into cells where one
5627 * of the upper bounds is smaller than all the others and select
5628 * this upper bound on that cell.
5630 * In particular, if there are n bounds b_i, then the result
5631 * consists of n cell, each one of the form
5633 * b_i <= b_j for j > i
5634 * b_i < b_j for j < i
5636 * The affine expression on this cell is
5638 * b_i
5640 static __isl_give isl_pw_aff *set_minimum_pa(__isl_take isl_space *space,
5641 __isl_take isl_mat *var)
5643 int i;
5644 isl_aff *aff = NULL;
5645 isl_basic_set *bset = NULL;
5646 isl_pw_aff *paff = NULL;
5647 isl_space *pw_space;
5648 isl_local_space *ls = NULL;
5650 if (!space || !var)
5651 goto error;
5653 ls = isl_local_space_from_space(isl_space_copy(space));
5654 pw_space = isl_space_copy(space);
5655 pw_space = isl_space_from_domain(pw_space);
5656 pw_space = isl_space_add_dims(pw_space, isl_dim_out, 1);
5657 paff = isl_pw_aff_alloc_size(pw_space, var->n_row);
5659 for (i = 0; i < var->n_row; ++i) {
5660 isl_pw_aff *paff_i;
5662 aff = isl_aff_alloc(isl_local_space_copy(ls));
5663 bset = isl_basic_set_alloc_space(isl_space_copy(space), 0,
5664 0, var->n_row - 1);
5665 if (!aff || !bset)
5666 goto error;
5667 isl_int_set_si(aff->v->el[0], 1);
5668 isl_seq_cpy(aff->v->el + 1, var->row[i], var->n_col);
5669 isl_int_set_si(aff->v->el[1 + var->n_col], 0);
5670 bset = select_minimum(bset, var, i);
5671 paff_i = isl_pw_aff_alloc(isl_set_from_basic_set(bset), aff);
5672 paff = isl_pw_aff_add_disjoint(paff, paff_i);
5675 isl_local_space_free(ls);
5676 isl_space_free(space);
5677 isl_mat_free(var);
5678 return paff;
5679 error:
5680 isl_aff_free(aff);
5681 isl_basic_set_free(bset);
5682 isl_pw_aff_free(paff);
5683 isl_local_space_free(ls);
5684 isl_space_free(space);
5685 isl_mat_free(var);
5686 return NULL;
5689 /* Given a piecewise multi-affine expression of which the last input variable
5690 * is the minimum of the bounds in "cst", plug in the value of the minimum.
5691 * This minimum expression is given in "min_expr_pa".
5692 * The set "min_expr" contains the same information, but in the form of a set.
5693 * The variable is subsequently projected out.
5695 * The implementation is similar to those of "split" and "split_domain".
5696 * If the variable appears in a given expression, then minimum expression
5697 * is plugged in. Otherwise, if the variable appears in the constraints
5698 * and a split is required, then the domain is split. Otherwise, no split
5699 * is performed.
5701 static __isl_give isl_pw_multi_aff *split_domain_pma(
5702 __isl_take isl_pw_multi_aff *opt, __isl_take isl_pw_aff *min_expr_pa,
5703 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
5705 int n_in;
5706 int i;
5707 isl_space *space;
5708 isl_pw_multi_aff *res;
5710 if (!opt || !min_expr || !cst)
5711 goto error;
5713 n_in = isl_pw_multi_aff_dim(opt, isl_dim_in);
5714 space = isl_pw_multi_aff_get_space(opt);
5715 space = isl_space_drop_dims(space, isl_dim_in, n_in - 1, 1);
5716 res = isl_pw_multi_aff_empty(space);
5718 for (i = 0; i < opt->n; ++i) {
5719 isl_pw_multi_aff *pma;
5721 pma = isl_pw_multi_aff_alloc(isl_set_copy(opt->p[i].set),
5722 isl_multi_aff_copy(opt->p[i].maff));
5723 if (need_substitution(opt->p[i].maff))
5724 pma = isl_pw_multi_aff_substitute(pma,
5725 isl_dim_in, n_in - 1, min_expr_pa);
5726 else if (need_split_set(opt->p[i].set, cst))
5727 pma = isl_pw_multi_aff_intersect_domain(pma,
5728 isl_set_copy(min_expr));
5729 pma = isl_pw_multi_aff_project_out(pma,
5730 isl_dim_in, n_in - 1, 1);
5732 res = isl_pw_multi_aff_add_disjoint(res, pma);
5735 isl_pw_multi_aff_free(opt);
5736 isl_pw_aff_free(min_expr_pa);
5737 isl_set_free(min_expr);
5738 isl_mat_free(cst);
5739 return res;
5740 error:
5741 isl_pw_multi_aff_free(opt);
5742 isl_pw_aff_free(min_expr_pa);
5743 isl_set_free(min_expr);
5744 isl_mat_free(cst);
5745 return NULL;
5748 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_pw_multi_aff(
5749 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5750 __isl_give isl_set **empty, int max);
5752 /* This function is called from basic_map_partial_lexopt_symm.
5753 * The last variable of "bmap" and "dom" corresponds to the minimum
5754 * of the bounds in "cst". "map_space" is the space of the original
5755 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
5756 * is the space of the original domain.
5758 * We recursively call basic_map_partial_lexopt and then plug in
5759 * the definition of the minimum in the result.
5761 static __isl_give isl_pw_multi_aff *
5762 basic_map_partial_lexopt_symm_core_pw_multi_aff(
5763 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5764 __isl_give isl_set **empty, int max, __isl_take isl_mat *cst,
5765 __isl_take isl_space *map_space, __isl_take isl_space *set_space)
5767 isl_pw_multi_aff *opt;
5768 isl_pw_aff *min_expr_pa;
5769 isl_set *min_expr;
5771 min_expr = set_minimum(isl_basic_set_get_space(dom), isl_mat_copy(cst));
5772 min_expr_pa = set_minimum_pa(isl_basic_set_get_space(dom),
5773 isl_mat_copy(cst));
5775 opt = basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, max);
5777 if (empty) {
5778 *empty = split(*empty,
5779 isl_set_copy(min_expr), isl_mat_copy(cst));
5780 *empty = isl_set_reset_space(*empty, set_space);
5783 opt = split_domain_pma(opt, min_expr_pa, min_expr, cst);
5784 opt = isl_pw_multi_aff_reset_space(opt, map_space);
5786 return opt;
5789 #undef TYPE
5790 #define TYPE isl_pw_multi_aff
5791 #undef SUFFIX
5792 #define SUFFIX _pw_multi_aff
5793 #include "isl_tab_lexopt_templ.c"