isl_scheduler.c: setup_lp: extract out add_bound_constant_constraints
[isl.git] / isl_tab_pip.c
bloba221eeffe069dd4f49443ec1bd0ec4e2c51325a5
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 #include <isl_ctx_private.h>
14 #include "isl_map_private.h"
15 #include <isl_seq.h>
16 #include "isl_tab.h"
17 #include "isl_sample.h"
18 #include <isl_mat_private.h>
19 #include <isl_vec_private.h>
20 #include <isl_aff_private.h>
21 #include <isl_options_private.h>
22 #include <isl_config.h>
25 * The implementation of parametric integer linear programming in this file
26 * was inspired by the paper "Parametric Integer Programming" and the
27 * report "Solving systems of affine (in)equalities" by Paul Feautrier
28 * (and others).
30 * The strategy used for obtaining a feasible solution is different
31 * from the one used in isl_tab.c. In particular, in isl_tab.c,
32 * upon finding a constraint that is not yet satisfied, we pivot
33 * in a row that increases the constant term of the row holding the
34 * constraint, making sure the sample solution remains feasible
35 * for all the constraints it already satisfied.
36 * Here, we always pivot in the row holding the constraint,
37 * choosing a column that induces the lexicographically smallest
38 * increment to the sample solution.
40 * By starting out from a sample value that is lexicographically
41 * smaller than any integer point in the problem space, the first
42 * feasible integer sample point we find will also be the lexicographically
43 * smallest. If all variables can be assumed to be non-negative,
44 * then the initial sample value may be chosen equal to zero.
45 * However, we will not make this assumption. Instead, we apply
46 * the "big parameter" trick. Any variable x is then not directly
47 * used in the tableau, but instead it is represented by another
48 * variable x' = M + x, where M is an arbitrarily large (positive)
49 * value. x' is therefore always non-negative, whatever the value of x.
50 * Taking as initial sample value x' = 0 corresponds to x = -M,
51 * which is always smaller than any possible value of x.
53 * The big parameter trick is used in the main tableau and
54 * also in the context tableau if isl_context_lex is used.
55 * In this case, each tableaus has its own big parameter.
56 * Before doing any real work, we check if all the parameters
57 * happen to be non-negative. If so, we drop the column corresponding
58 * to M from the initial context tableau.
59 * If isl_context_gbr is used, then the big parameter trick is only
60 * used in the main tableau.
63 struct isl_context;
64 struct isl_context_op {
65 /* detect nonnegative parameters in context and mark them in tab */
66 struct isl_tab *(*detect_nonnegative_parameters)(
67 struct isl_context *context, struct isl_tab *tab);
68 /* return temporary reference to basic set representation of context */
69 struct isl_basic_set *(*peek_basic_set)(struct isl_context *context);
70 /* return temporary reference to tableau representation of context */
71 struct isl_tab *(*peek_tab)(struct isl_context *context);
72 /* add equality; check is 1 if eq may not be valid;
73 * update is 1 if we may want to call ineq_sign on context later.
75 void (*add_eq)(struct isl_context *context, isl_int *eq,
76 int check, int update);
77 /* add inequality; check is 1 if ineq may not be valid;
78 * update is 1 if we may want to call ineq_sign on context later.
80 void (*add_ineq)(struct isl_context *context, isl_int *ineq,
81 int check, int update);
82 /* check sign of ineq based on previous information.
83 * strict is 1 if saturation should be treated as a positive sign.
85 enum isl_tab_row_sign (*ineq_sign)(struct isl_context *context,
86 isl_int *ineq, int strict);
87 /* check if inequality maintains feasibility */
88 int (*test_ineq)(struct isl_context *context, isl_int *ineq);
89 /* return index of a div that corresponds to "div" */
90 int (*get_div)(struct isl_context *context, struct isl_tab *tab,
91 struct isl_vec *div);
92 /* add div "div" to context and return non-negativity */
93 int (*add_div)(struct isl_context *context, struct isl_vec *div);
94 int (*detect_equalities)(struct isl_context *context,
95 struct isl_tab *tab);
96 /* return row index of "best" split */
97 int (*best_split)(struct isl_context *context, struct isl_tab *tab);
98 /* check if context has already been determined to be empty */
99 int (*is_empty)(struct isl_context *context);
100 /* check if context is still usable */
101 int (*is_ok)(struct isl_context *context);
102 /* save a copy/snapshot of context */
103 void *(*save)(struct isl_context *context);
104 /* restore saved context */
105 void (*restore)(struct isl_context *context, void *);
106 /* discard saved context */
107 void (*discard)(void *);
108 /* invalidate context */
109 void (*invalidate)(struct isl_context *context);
110 /* free context */
111 void (*free)(struct isl_context *context);
114 struct isl_context {
115 struct isl_context_op *op;
118 struct isl_context_lex {
119 struct isl_context context;
120 struct isl_tab *tab;
123 /* A stack (linked list) of solutions of subtrees of the search space.
125 * "M" describes the solution in terms of the dimensions of "dom".
126 * The number of columns of "M" is one more than the total number
127 * of dimensions of "dom".
129 * If "M" is NULL, then there is no solution on "dom".
131 struct isl_partial_sol {
132 int level;
133 struct isl_basic_set *dom;
134 struct isl_mat *M;
136 struct isl_partial_sol *next;
139 struct isl_sol;
140 struct isl_sol_callback {
141 struct isl_tab_callback callback;
142 struct isl_sol *sol;
145 /* isl_sol is an interface for constructing a solution to
146 * a parametric integer linear programming problem.
147 * Every time the algorithm reaches a state where a solution
148 * can be read off from the tableau (including cases where the tableau
149 * is empty), the function "add" is called on the isl_sol passed
150 * to find_solutions_main.
152 * The context tableau is owned by isl_sol and is updated incrementally.
154 * There are currently two implementations of this interface,
155 * isl_sol_map, which simply collects the solutions in an isl_map
156 * and (optionally) the parts of the context where there is no solution
157 * in an isl_set, and
158 * isl_sol_for, which calls a user-defined function for each part of
159 * the solution.
161 struct isl_sol {
162 int error;
163 int rational;
164 int level;
165 int max;
166 int n_out;
167 struct isl_context *context;
168 struct isl_partial_sol *partial;
169 void (*add)(struct isl_sol *sol,
170 struct isl_basic_set *dom, struct isl_mat *M);
171 void (*add_empty)(struct isl_sol *sol, struct isl_basic_set *bset);
172 void (*free)(struct isl_sol *sol);
173 struct isl_sol_callback dec_level;
176 static void sol_free(struct isl_sol *sol)
178 struct isl_partial_sol *partial, *next;
179 if (!sol)
180 return;
181 for (partial = sol->partial; partial; partial = next) {
182 next = partial->next;
183 isl_basic_set_free(partial->dom);
184 isl_mat_free(partial->M);
185 free(partial);
187 sol->free(sol);
190 /* Push a partial solution represented by a domain and mapping M
191 * onto the stack of partial solutions.
193 static void sol_push_sol(struct isl_sol *sol,
194 struct isl_basic_set *dom, struct isl_mat *M)
196 struct isl_partial_sol *partial;
198 if (sol->error || !dom)
199 goto error;
201 partial = isl_alloc_type(dom->ctx, struct isl_partial_sol);
202 if (!partial)
203 goto error;
205 partial->level = sol->level;
206 partial->dom = dom;
207 partial->M = M;
208 partial->next = sol->partial;
210 sol->partial = partial;
212 return;
213 error:
214 isl_basic_set_free(dom);
215 isl_mat_free(M);
216 sol->error = 1;
219 /* Pop one partial solution from the partial solution stack and
220 * pass it on to sol->add or sol->add_empty.
222 static void sol_pop_one(struct isl_sol *sol)
224 struct isl_partial_sol *partial;
226 partial = sol->partial;
227 sol->partial = partial->next;
229 if (partial->M)
230 sol->add(sol, partial->dom, partial->M);
231 else
232 sol->add_empty(sol, partial->dom);
233 free(partial);
236 /* Return a fresh copy of the domain represented by the context tableau.
238 static struct isl_basic_set *sol_domain(struct isl_sol *sol)
240 struct isl_basic_set *bset;
242 if (sol->error)
243 return NULL;
245 bset = isl_basic_set_dup(sol->context->op->peek_basic_set(sol->context));
246 bset = isl_basic_set_update_from_tab(bset,
247 sol->context->op->peek_tab(sol->context));
249 return bset;
252 /* Check whether two partial solutions have the same mapping, where n_div
253 * is the number of divs that the two partial solutions have in common.
255 static int same_solution(struct isl_partial_sol *s1, struct isl_partial_sol *s2,
256 unsigned n_div)
258 int i;
259 unsigned dim;
261 if (!s1->M != !s2->M)
262 return 0;
263 if (!s1->M)
264 return 1;
266 dim = isl_basic_set_total_dim(s1->dom) - s1->dom->n_div;
268 for (i = 0; i < s1->M->n_row; ++i) {
269 if (isl_seq_first_non_zero(s1->M->row[i]+1+dim+n_div,
270 s1->M->n_col-1-dim-n_div) != -1)
271 return 0;
272 if (isl_seq_first_non_zero(s2->M->row[i]+1+dim+n_div,
273 s2->M->n_col-1-dim-n_div) != -1)
274 return 0;
275 if (!isl_seq_eq(s1->M->row[i], s2->M->row[i], 1+dim+n_div))
276 return 0;
278 return 1;
281 /* Pop all solutions from the partial solution stack that were pushed onto
282 * the stack at levels that are deeper than the current level.
283 * If the two topmost elements on the stack have the same level
284 * and represent the same solution, then their domains are combined.
285 * This combined domain is the same as the current context domain
286 * as sol_pop is called each time we move back to a higher level.
287 * If the outer level (0) has been reached, then all partial solutions
288 * at the current level are also popped off.
290 static void sol_pop(struct isl_sol *sol)
292 struct isl_partial_sol *partial;
293 unsigned n_div;
295 if (sol->error)
296 return;
298 partial = sol->partial;
299 if (!partial)
300 return;
302 if (partial->level == 0 && sol->level == 0) {
303 for (partial = sol->partial; partial; partial = sol->partial)
304 sol_pop_one(sol);
305 return;
308 if (partial->level <= sol->level)
309 return;
311 if (partial->next && partial->next->level == partial->level) {
312 n_div = isl_basic_set_dim(
313 sol->context->op->peek_basic_set(sol->context),
314 isl_dim_div);
316 if (!same_solution(partial, partial->next, n_div)) {
317 sol_pop_one(sol);
318 sol_pop_one(sol);
319 } else {
320 struct isl_basic_set *bset;
321 isl_mat *M;
322 unsigned n;
324 n = isl_basic_set_dim(partial->next->dom, isl_dim_div);
325 n -= n_div;
326 bset = sol_domain(sol);
327 isl_basic_set_free(partial->next->dom);
328 partial->next->dom = bset;
329 M = partial->next->M;
330 if (M) {
331 M = isl_mat_drop_cols(M, M->n_col - n, n);
332 partial->next->M = M;
333 if (!M)
334 goto error;
336 partial->next->level = sol->level;
338 if (!bset)
339 goto error;
341 sol->partial = partial->next;
342 isl_basic_set_free(partial->dom);
343 isl_mat_free(partial->M);
344 free(partial);
346 } else
347 sol_pop_one(sol);
349 if (sol->level == 0) {
350 for (partial = sol->partial; partial; partial = sol->partial)
351 sol_pop_one(sol);
352 return;
355 if (0)
356 error: sol->error = 1;
359 static void sol_dec_level(struct isl_sol *sol)
361 if (sol->error)
362 return;
364 sol->level--;
366 sol_pop(sol);
369 static int sol_dec_level_wrap(struct isl_tab_callback *cb)
371 struct isl_sol_callback *callback = (struct isl_sol_callback *)cb;
373 sol_dec_level(callback->sol);
375 return callback->sol->error ? -1 : 0;
378 /* Move down to next level and push callback onto context tableau
379 * to decrease the level again when it gets rolled back across
380 * the current state. That is, dec_level will be called with
381 * the context tableau in the same state as it is when inc_level
382 * is called.
384 static void sol_inc_level(struct isl_sol *sol)
386 struct isl_tab *tab;
388 if (sol->error)
389 return;
391 sol->level++;
392 tab = sol->context->op->peek_tab(sol->context);
393 if (isl_tab_push_callback(tab, &sol->dec_level.callback) < 0)
394 sol->error = 1;
397 static void scale_rows(struct isl_mat *mat, isl_int m, int n_row)
399 int i;
401 if (isl_int_is_one(m))
402 return;
404 for (i = 0; i < n_row; ++i)
405 isl_seq_scale(mat->row[i], mat->row[i], m, mat->n_col);
408 /* Add the solution identified by the tableau and the context tableau.
410 * The layout of the variables is as follows.
411 * tab->n_var is equal to the total number of variables in the input
412 * map (including divs that were copied from the context)
413 * + the number of extra divs constructed
414 * Of these, the first tab->n_param and the last tab->n_div variables
415 * correspond to the variables in the context, i.e.,
416 * tab->n_param + tab->n_div = context_tab->n_var
417 * tab->n_param is equal to the number of parameters and input
418 * dimensions in the input map
419 * tab->n_div is equal to the number of divs in the context
421 * If there is no solution, then call add_empty with a basic set
422 * that corresponds to the context tableau. (If add_empty is NULL,
423 * then do nothing).
425 * If there is a solution, then first construct a matrix that maps
426 * all dimensions of the context to the output variables, i.e.,
427 * the output dimensions in the input map.
428 * The divs in the input map (if any) that do not correspond to any
429 * div in the context do not appear in the solution.
430 * The algorithm will make sure that they have an integer value,
431 * but these values themselves are of no interest.
432 * We have to be careful not to drop or rearrange any divs in the
433 * context because that would change the meaning of the matrix.
435 * To extract the value of the output variables, it should be noted
436 * that we always use a big parameter M in the main tableau and so
437 * the variable stored in this tableau is not an output variable x itself, but
438 * x' = M + x (in case of minimization)
439 * or
440 * x' = M - x (in case of maximization)
441 * If x' appears in a column, then its optimal value is zero,
442 * which means that the optimal value of x is an unbounded number
443 * (-M for minimization and M for maximization).
444 * We currently assume that the output dimensions in the original map
445 * are bounded, so this cannot occur.
446 * Similarly, when x' appears in a row, then the coefficient of M in that
447 * row is necessarily 1.
448 * If the row in the tableau represents
449 * d x' = c + d M + e(y)
450 * then, in case of minimization, the corresponding row in the matrix
451 * will be
452 * a c + a e(y)
453 * with a d = m, the (updated) common denominator of the matrix.
454 * In case of maximization, the row will be
455 * -a c - a e(y)
457 static void sol_add(struct isl_sol *sol, struct isl_tab *tab)
459 struct isl_basic_set *bset = NULL;
460 struct isl_mat *mat = NULL;
461 unsigned off;
462 int row;
463 isl_int m;
465 if (sol->error || !tab)
466 goto error;
468 if (tab->empty && !sol->add_empty)
469 return;
470 if (sol->context->op->is_empty(sol->context))
471 return;
473 bset = sol_domain(sol);
475 if (tab->empty) {
476 sol_push_sol(sol, bset, NULL);
477 return;
480 off = 2 + tab->M;
482 mat = isl_mat_alloc(tab->mat->ctx, 1 + sol->n_out,
483 1 + tab->n_param + tab->n_div);
484 if (!mat)
485 goto error;
487 isl_int_init(m);
489 isl_seq_clr(mat->row[0] + 1, mat->n_col - 1);
490 isl_int_set_si(mat->row[0][0], 1);
491 for (row = 0; row < sol->n_out; ++row) {
492 int i = tab->n_param + row;
493 int r, j;
495 isl_seq_clr(mat->row[1 + row], mat->n_col);
496 if (!tab->var[i].is_row) {
497 if (tab->M)
498 isl_die(mat->ctx, isl_error_invalid,
499 "unbounded optimum", goto error2);
500 continue;
503 r = tab->var[i].index;
504 if (tab->M &&
505 isl_int_ne(tab->mat->row[r][2], tab->mat->row[r][0]))
506 isl_die(mat->ctx, isl_error_invalid,
507 "unbounded optimum", goto error2);
508 isl_int_gcd(m, mat->row[0][0], tab->mat->row[r][0]);
509 isl_int_divexact(m, tab->mat->row[r][0], m);
510 scale_rows(mat, m, 1 + row);
511 isl_int_divexact(m, mat->row[0][0], tab->mat->row[r][0]);
512 isl_int_mul(mat->row[1 + row][0], m, tab->mat->row[r][1]);
513 for (j = 0; j < tab->n_param; ++j) {
514 int col;
515 if (tab->var[j].is_row)
516 continue;
517 col = tab->var[j].index;
518 isl_int_mul(mat->row[1 + row][1 + j], m,
519 tab->mat->row[r][off + col]);
521 for (j = 0; j < tab->n_div; ++j) {
522 int col;
523 if (tab->var[tab->n_var - tab->n_div+j].is_row)
524 continue;
525 col = tab->var[tab->n_var - tab->n_div+j].index;
526 isl_int_mul(mat->row[1 + row][1 + tab->n_param + j], m,
527 tab->mat->row[r][off + col]);
529 if (sol->max)
530 isl_seq_neg(mat->row[1 + row], mat->row[1 + row],
531 mat->n_col);
534 isl_int_clear(m);
536 sol_push_sol(sol, bset, mat);
537 return;
538 error2:
539 isl_int_clear(m);
540 error:
541 isl_basic_set_free(bset);
542 isl_mat_free(mat);
543 sol->error = 1;
546 struct isl_sol_map {
547 struct isl_sol sol;
548 struct isl_map *map;
549 struct isl_set *empty;
552 static void sol_map_free(struct isl_sol_map *sol_map)
554 if (!sol_map)
555 return;
556 if (sol_map->sol.context)
557 sol_map->sol.context->op->free(sol_map->sol.context);
558 isl_map_free(sol_map->map);
559 isl_set_free(sol_map->empty);
560 free(sol_map);
563 static void sol_map_free_wrap(struct isl_sol *sol)
565 sol_map_free((struct isl_sol_map *)sol);
568 /* This function is called for parts of the context where there is
569 * no solution, with "bset" corresponding to the context tableau.
570 * Simply add the basic set to the set "empty".
572 static void sol_map_add_empty(struct isl_sol_map *sol,
573 struct isl_basic_set *bset)
575 if (!bset || !sol->empty)
576 goto error;
578 sol->empty = isl_set_grow(sol->empty, 1);
579 bset = isl_basic_set_simplify(bset);
580 bset = isl_basic_set_finalize(bset);
581 sol->empty = isl_set_add_basic_set(sol->empty, isl_basic_set_copy(bset));
582 if (!sol->empty)
583 goto error;
584 isl_basic_set_free(bset);
585 return;
586 error:
587 isl_basic_set_free(bset);
588 sol->sol.error = 1;
591 static void sol_map_add_empty_wrap(struct isl_sol *sol,
592 struct isl_basic_set *bset)
594 sol_map_add_empty((struct isl_sol_map *)sol, bset);
597 /* Given a basic map "dom" that represents the context and an affine
598 * matrix "M" that maps the dimensions of the context to the
599 * output variables, construct a basic map with the same parameters
600 * and divs as the context, the dimensions of the context as input
601 * dimensions and a number of output dimensions that is equal to
602 * the number of output dimensions in the input map.
604 * The constraints and divs of the context are simply copied
605 * from "dom". For each row
606 * x = c + e(y)
607 * an equality
608 * c + e(y) - d x = 0
609 * is added, with d the common denominator of M.
611 static void sol_map_add(struct isl_sol_map *sol,
612 struct isl_basic_set *dom, struct isl_mat *M)
614 int i;
615 struct isl_basic_map *bmap = NULL;
616 unsigned n_eq;
617 unsigned n_ineq;
618 unsigned nparam;
619 unsigned total;
620 unsigned n_div;
621 unsigned n_out;
623 if (sol->sol.error || !dom || !M)
624 goto error;
626 n_out = sol->sol.n_out;
627 n_eq = dom->n_eq + n_out;
628 n_ineq = dom->n_ineq;
629 n_div = dom->n_div;
630 nparam = isl_basic_set_total_dim(dom) - n_div;
631 total = isl_map_dim(sol->map, isl_dim_all);
632 bmap = isl_basic_map_alloc_space(isl_map_get_space(sol->map),
633 n_div, n_eq, 2 * n_div + n_ineq);
634 if (!bmap)
635 goto error;
636 if (sol->sol.rational)
637 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
638 for (i = 0; i < dom->n_div; ++i) {
639 int k = isl_basic_map_alloc_div(bmap);
640 if (k < 0)
641 goto error;
642 isl_seq_cpy(bmap->div[k], dom->div[i], 1 + 1 + nparam);
643 isl_seq_clr(bmap->div[k] + 1 + 1 + nparam, total - nparam);
644 isl_seq_cpy(bmap->div[k] + 1 + 1 + total,
645 dom->div[i] + 1 + 1 + nparam, i);
647 for (i = 0; i < dom->n_eq; ++i) {
648 int k = isl_basic_map_alloc_equality(bmap);
649 if (k < 0)
650 goto error;
651 isl_seq_cpy(bmap->eq[k], dom->eq[i], 1 + nparam);
652 isl_seq_clr(bmap->eq[k] + 1 + nparam, total - nparam);
653 isl_seq_cpy(bmap->eq[k] + 1 + total,
654 dom->eq[i] + 1 + nparam, n_div);
656 for (i = 0; i < dom->n_ineq; ++i) {
657 int k = isl_basic_map_alloc_inequality(bmap);
658 if (k < 0)
659 goto error;
660 isl_seq_cpy(bmap->ineq[k], dom->ineq[i], 1 + nparam);
661 isl_seq_clr(bmap->ineq[k] + 1 + nparam, total - nparam);
662 isl_seq_cpy(bmap->ineq[k] + 1 + total,
663 dom->ineq[i] + 1 + nparam, n_div);
665 for (i = 0; i < M->n_row - 1; ++i) {
666 int k = isl_basic_map_alloc_equality(bmap);
667 if (k < 0)
668 goto error;
669 isl_seq_cpy(bmap->eq[k], M->row[1 + i], 1 + nparam);
670 isl_seq_clr(bmap->eq[k] + 1 + nparam, n_out);
671 isl_int_neg(bmap->eq[k][1 + nparam + i], M->row[0][0]);
672 isl_seq_cpy(bmap->eq[k] + 1 + nparam + n_out,
673 M->row[1 + i] + 1 + nparam, n_div);
675 bmap = isl_basic_map_simplify(bmap);
676 bmap = isl_basic_map_finalize(bmap);
677 sol->map = isl_map_grow(sol->map, 1);
678 sol->map = isl_map_add_basic_map(sol->map, bmap);
679 isl_basic_set_free(dom);
680 isl_mat_free(M);
681 if (!sol->map)
682 sol->sol.error = 1;
683 return;
684 error:
685 isl_basic_set_free(dom);
686 isl_mat_free(M);
687 isl_basic_map_free(bmap);
688 sol->sol.error = 1;
691 static void sol_map_add_wrap(struct isl_sol *sol,
692 struct isl_basic_set *dom, struct isl_mat *M)
694 sol_map_add((struct isl_sol_map *)sol, dom, M);
698 /* Store the "parametric constant" of row "row" of tableau "tab" in "line",
699 * i.e., the constant term and the coefficients of all variables that
700 * appear in the context tableau.
701 * Note that the coefficient of the big parameter M is NOT copied.
702 * The context tableau may not have a big parameter and even when it
703 * does, it is a different big parameter.
705 static void get_row_parameter_line(struct isl_tab *tab, int row, isl_int *line)
707 int i;
708 unsigned off = 2 + tab->M;
710 isl_int_set(line[0], tab->mat->row[row][1]);
711 for (i = 0; i < tab->n_param; ++i) {
712 if (tab->var[i].is_row)
713 isl_int_set_si(line[1 + i], 0);
714 else {
715 int col = tab->var[i].index;
716 isl_int_set(line[1 + i], tab->mat->row[row][off + col]);
719 for (i = 0; i < tab->n_div; ++i) {
720 if (tab->var[tab->n_var - tab->n_div + i].is_row)
721 isl_int_set_si(line[1 + tab->n_param + i], 0);
722 else {
723 int col = tab->var[tab->n_var - tab->n_div + i].index;
724 isl_int_set(line[1 + tab->n_param + i],
725 tab->mat->row[row][off + col]);
730 /* Check if rows "row1" and "row2" have identical "parametric constants",
731 * as explained above.
732 * In this case, we also insist that the coefficients of the big parameter
733 * be the same as the values of the constants will only be the same
734 * if these coefficients are also the same.
736 static int identical_parameter_line(struct isl_tab *tab, int row1, int row2)
738 int i;
739 unsigned off = 2 + tab->M;
741 if (isl_int_ne(tab->mat->row[row1][1], tab->mat->row[row2][1]))
742 return 0;
744 if (tab->M && isl_int_ne(tab->mat->row[row1][2],
745 tab->mat->row[row2][2]))
746 return 0;
748 for (i = 0; i < tab->n_param + tab->n_div; ++i) {
749 int pos = i < tab->n_param ? i :
750 tab->n_var - tab->n_div + i - tab->n_param;
751 int col;
753 if (tab->var[pos].is_row)
754 continue;
755 col = tab->var[pos].index;
756 if (isl_int_ne(tab->mat->row[row1][off + col],
757 tab->mat->row[row2][off + col]))
758 return 0;
760 return 1;
763 /* Return an inequality that expresses that the "parametric constant"
764 * should be non-negative.
765 * This function is only called when the coefficient of the big parameter
766 * is equal to zero.
768 static struct isl_vec *get_row_parameter_ineq(struct isl_tab *tab, int row)
770 struct isl_vec *ineq;
772 ineq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_param + tab->n_div);
773 if (!ineq)
774 return NULL;
776 get_row_parameter_line(tab, row, ineq->el);
777 if (ineq)
778 ineq = isl_vec_normalize(ineq);
780 return ineq;
783 /* Normalize a div expression of the form
785 * [(g*f(x) + c)/(g * m)]
787 * with c the constant term and f(x) the remaining coefficients, to
789 * [(f(x) + [c/g])/m]
791 static void normalize_div(__isl_keep isl_vec *div)
793 isl_ctx *ctx = isl_vec_get_ctx(div);
794 int len = div->size - 2;
796 isl_seq_gcd(div->el + 2, len, &ctx->normalize_gcd);
797 isl_int_gcd(ctx->normalize_gcd, ctx->normalize_gcd, div->el[0]);
799 if (isl_int_is_one(ctx->normalize_gcd))
800 return;
802 isl_int_divexact(div->el[0], div->el[0], ctx->normalize_gcd);
803 isl_int_fdiv_q(div->el[1], div->el[1], ctx->normalize_gcd);
804 isl_seq_scale_down(div->el + 2, div->el + 2, ctx->normalize_gcd, len);
807 /* Return a integer division for use in a parametric cut based on the given row.
808 * In particular, let the parametric constant of the row be
810 * \sum_i a_i y_i
812 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
813 * The div returned is equal to
815 * floor(\sum_i {-a_i} y_i) = floor((\sum_i (-a_i mod d) y_i)/d)
817 static struct isl_vec *get_row_parameter_div(struct isl_tab *tab, int row)
819 struct isl_vec *div;
821 div = isl_vec_alloc(tab->mat->ctx, 1 + 1 + tab->n_param + tab->n_div);
822 if (!div)
823 return NULL;
825 isl_int_set(div->el[0], tab->mat->row[row][0]);
826 get_row_parameter_line(tab, row, div->el + 1);
827 isl_seq_neg(div->el + 1, div->el + 1, div->size - 1);
828 normalize_div(div);
829 isl_seq_fdiv_r(div->el + 1, div->el + 1, div->el[0], div->size - 1);
831 return div;
834 /* Return a integer division for use in transferring an integrality constraint
835 * to the context.
836 * In particular, let the parametric constant of the row be
838 * \sum_i a_i y_i
840 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
841 * The the returned div is equal to
843 * floor(\sum_i {a_i} y_i) = floor((\sum_i (a_i mod d) y_i)/d)
845 static struct isl_vec *get_row_split_div(struct isl_tab *tab, int row)
847 struct isl_vec *div;
849 div = isl_vec_alloc(tab->mat->ctx, 1 + 1 + tab->n_param + tab->n_div);
850 if (!div)
851 return NULL;
853 isl_int_set(div->el[0], tab->mat->row[row][0]);
854 get_row_parameter_line(tab, row, div->el + 1);
855 normalize_div(div);
856 isl_seq_fdiv_r(div->el + 1, div->el + 1, div->el[0], div->size - 1);
858 return div;
861 /* Construct and return an inequality that expresses an upper bound
862 * on the given div.
863 * In particular, if the div is given by
865 * d = floor(e/m)
867 * then the inequality expresses
869 * m d <= e
871 static struct isl_vec *ineq_for_div(struct isl_basic_set *bset, unsigned div)
873 unsigned total;
874 unsigned div_pos;
875 struct isl_vec *ineq;
877 if (!bset)
878 return NULL;
880 total = isl_basic_set_total_dim(bset);
881 div_pos = 1 + total - bset->n_div + div;
883 ineq = isl_vec_alloc(bset->ctx, 1 + total);
884 if (!ineq)
885 return NULL;
887 isl_seq_cpy(ineq->el, bset->div[div] + 1, 1 + total);
888 isl_int_neg(ineq->el[div_pos], bset->div[div][0]);
889 return ineq;
892 /* Given a row in the tableau and a div that was created
893 * using get_row_split_div and that has been constrained to equality, i.e.,
895 * d = floor(\sum_i {a_i} y_i) = \sum_i {a_i} y_i
897 * replace the expression "\sum_i {a_i} y_i" in the row by d,
898 * i.e., we subtract "\sum_i {a_i} y_i" and add 1 d.
899 * The coefficients of the non-parameters in the tableau have been
900 * verified to be integral. We can therefore simply replace coefficient b
901 * by floor(b). For the coefficients of the parameters we have
902 * floor(a_i) = a_i - {a_i}, while for the other coefficients, we have
903 * floor(b) = b.
905 static struct isl_tab *set_row_cst_to_div(struct isl_tab *tab, int row, int div)
907 isl_seq_fdiv_q(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
908 tab->mat->row[row][0], 1 + tab->M + tab->n_col);
910 isl_int_set_si(tab->mat->row[row][0], 1);
912 if (tab->var[tab->n_var - tab->n_div + div].is_row) {
913 int drow = tab->var[tab->n_var - tab->n_div + div].index;
915 isl_assert(tab->mat->ctx,
916 isl_int_is_one(tab->mat->row[drow][0]), goto error);
917 isl_seq_combine(tab->mat->row[row] + 1,
918 tab->mat->ctx->one, tab->mat->row[row] + 1,
919 tab->mat->ctx->one, tab->mat->row[drow] + 1,
920 1 + tab->M + tab->n_col);
921 } else {
922 int dcol = tab->var[tab->n_var - tab->n_div + div].index;
924 isl_int_add_ui(tab->mat->row[row][2 + tab->M + dcol],
925 tab->mat->row[row][2 + tab->M + dcol], 1);
928 return tab;
929 error:
930 isl_tab_free(tab);
931 return NULL;
934 /* Check if the (parametric) constant of the given row is obviously
935 * negative, meaning that we don't need to consult the context tableau.
936 * If there is a big parameter and its coefficient is non-zero,
937 * then this coefficient determines the outcome.
938 * Otherwise, we check whether the constant is negative and
939 * all non-zero coefficients of parameters are negative and
940 * belong to non-negative parameters.
942 static int is_obviously_neg(struct isl_tab *tab, int row)
944 int i;
945 int col;
946 unsigned off = 2 + tab->M;
948 if (tab->M) {
949 if (isl_int_is_pos(tab->mat->row[row][2]))
950 return 0;
951 if (isl_int_is_neg(tab->mat->row[row][2]))
952 return 1;
955 if (isl_int_is_nonneg(tab->mat->row[row][1]))
956 return 0;
957 for (i = 0; i < tab->n_param; ++i) {
958 /* Eliminated parameter */
959 if (tab->var[i].is_row)
960 continue;
961 col = tab->var[i].index;
962 if (isl_int_is_zero(tab->mat->row[row][off + col]))
963 continue;
964 if (!tab->var[i].is_nonneg)
965 return 0;
966 if (isl_int_is_pos(tab->mat->row[row][off + col]))
967 return 0;
969 for (i = 0; i < tab->n_div; ++i) {
970 if (tab->var[tab->n_var - tab->n_div + i].is_row)
971 continue;
972 col = tab->var[tab->n_var - tab->n_div + i].index;
973 if (isl_int_is_zero(tab->mat->row[row][off + col]))
974 continue;
975 if (!tab->var[tab->n_var - tab->n_div + i].is_nonneg)
976 return 0;
977 if (isl_int_is_pos(tab->mat->row[row][off + col]))
978 return 0;
980 return 1;
983 /* Check if the (parametric) constant of the given row is obviously
984 * non-negative, meaning that we don't need to consult the context tableau.
985 * If there is a big parameter and its coefficient is non-zero,
986 * then this coefficient determines the outcome.
987 * Otherwise, we check whether the constant is non-negative and
988 * all non-zero coefficients of parameters are positive and
989 * belong to non-negative parameters.
991 static int is_obviously_nonneg(struct isl_tab *tab, int row)
993 int i;
994 int col;
995 unsigned off = 2 + tab->M;
997 if (tab->M) {
998 if (isl_int_is_pos(tab->mat->row[row][2]))
999 return 1;
1000 if (isl_int_is_neg(tab->mat->row[row][2]))
1001 return 0;
1004 if (isl_int_is_neg(tab->mat->row[row][1]))
1005 return 0;
1006 for (i = 0; i < tab->n_param; ++i) {
1007 /* Eliminated parameter */
1008 if (tab->var[i].is_row)
1009 continue;
1010 col = tab->var[i].index;
1011 if (isl_int_is_zero(tab->mat->row[row][off + col]))
1012 continue;
1013 if (!tab->var[i].is_nonneg)
1014 return 0;
1015 if (isl_int_is_neg(tab->mat->row[row][off + col]))
1016 return 0;
1018 for (i = 0; i < tab->n_div; ++i) {
1019 if (tab->var[tab->n_var - tab->n_div + i].is_row)
1020 continue;
1021 col = tab->var[tab->n_var - tab->n_div + i].index;
1022 if (isl_int_is_zero(tab->mat->row[row][off + col]))
1023 continue;
1024 if (!tab->var[tab->n_var - tab->n_div + i].is_nonneg)
1025 return 0;
1026 if (isl_int_is_neg(tab->mat->row[row][off + col]))
1027 return 0;
1029 return 1;
1032 /* Given a row r and two columns, return the column that would
1033 * lead to the lexicographically smallest increment in the sample
1034 * solution when leaving the basis in favor of the row.
1035 * Pivoting with column c will increment the sample value by a non-negative
1036 * constant times a_{V,c}/a_{r,c}, with a_{V,c} the elements of column c
1037 * corresponding to the non-parametric variables.
1038 * If variable v appears in a column c_v, the a_{v,c} = 1 iff c = c_v,
1039 * with all other entries in this virtual row equal to zero.
1040 * If variable v appears in a row, then a_{v,c} is the element in column c
1041 * of that row.
1043 * Let v be the first variable with a_{v,c1}/a_{r,c1} != a_{v,c2}/a_{r,c2}.
1044 * Then if a_{v,c1}/a_{r,c1} < a_{v,c2}/a_{r,c2}, i.e.,
1045 * a_{v,c2} a_{r,c1} - a_{v,c1} a_{r,c2} > 0, c1 results in the minimal
1046 * increment. Otherwise, it's c2.
1048 static int lexmin_col_pair(struct isl_tab *tab,
1049 int row, int col1, int col2, isl_int tmp)
1051 int i;
1052 isl_int *tr;
1054 tr = tab->mat->row[row] + 2 + tab->M;
1056 for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
1057 int s1, s2;
1058 isl_int *r;
1060 if (!tab->var[i].is_row) {
1061 if (tab->var[i].index == col1)
1062 return col2;
1063 if (tab->var[i].index == col2)
1064 return col1;
1065 continue;
1068 if (tab->var[i].index == row)
1069 continue;
1071 r = tab->mat->row[tab->var[i].index] + 2 + tab->M;
1072 s1 = isl_int_sgn(r[col1]);
1073 s2 = isl_int_sgn(r[col2]);
1074 if (s1 == 0 && s2 == 0)
1075 continue;
1076 if (s1 < s2)
1077 return col1;
1078 if (s2 < s1)
1079 return col2;
1081 isl_int_mul(tmp, r[col2], tr[col1]);
1082 isl_int_submul(tmp, r[col1], tr[col2]);
1083 if (isl_int_is_pos(tmp))
1084 return col1;
1085 if (isl_int_is_neg(tmp))
1086 return col2;
1088 return -1;
1091 /* Given a row in the tableau, find and return the column that would
1092 * result in the lexicographically smallest, but positive, increment
1093 * in the sample point.
1094 * If there is no such column, then return tab->n_col.
1095 * If anything goes wrong, return -1.
1097 static int lexmin_pivot_col(struct isl_tab *tab, int row)
1099 int j;
1100 int col = tab->n_col;
1101 isl_int *tr;
1102 isl_int tmp;
1104 tr = tab->mat->row[row] + 2 + tab->M;
1106 isl_int_init(tmp);
1108 for (j = tab->n_dead; j < tab->n_col; ++j) {
1109 if (tab->col_var[j] >= 0 &&
1110 (tab->col_var[j] < tab->n_param ||
1111 tab->col_var[j] >= tab->n_var - tab->n_div))
1112 continue;
1114 if (!isl_int_is_pos(tr[j]))
1115 continue;
1117 if (col == tab->n_col)
1118 col = j;
1119 else
1120 col = lexmin_col_pair(tab, row, col, j, tmp);
1121 isl_assert(tab->mat->ctx, col >= 0, goto error);
1124 isl_int_clear(tmp);
1125 return col;
1126 error:
1127 isl_int_clear(tmp);
1128 return -1;
1131 /* Return the first known violated constraint, i.e., a non-negative
1132 * constraint that currently has an either obviously negative value
1133 * or a previously determined to be negative value.
1135 * If any constraint has a negative coefficient for the big parameter,
1136 * if any, then we return one of these first.
1138 static int first_neg(struct isl_tab *tab)
1140 int row;
1142 if (tab->M)
1143 for (row = tab->n_redundant; row < tab->n_row; ++row) {
1144 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
1145 continue;
1146 if (!isl_int_is_neg(tab->mat->row[row][2]))
1147 continue;
1148 if (tab->row_sign)
1149 tab->row_sign[row] = isl_tab_row_neg;
1150 return row;
1152 for (row = tab->n_redundant; row < tab->n_row; ++row) {
1153 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
1154 continue;
1155 if (tab->row_sign) {
1156 if (tab->row_sign[row] == 0 &&
1157 is_obviously_neg(tab, row))
1158 tab->row_sign[row] = isl_tab_row_neg;
1159 if (tab->row_sign[row] != isl_tab_row_neg)
1160 continue;
1161 } else if (!is_obviously_neg(tab, row))
1162 continue;
1163 return row;
1165 return -1;
1168 /* Check whether the invariant that all columns are lexico-positive
1169 * is satisfied. This function is not called from the current code
1170 * but is useful during debugging.
1172 static void check_lexpos(struct isl_tab *tab) __attribute__ ((unused));
1173 static void check_lexpos(struct isl_tab *tab)
1175 unsigned off = 2 + tab->M;
1176 int col;
1177 int var;
1178 int row;
1180 for (col = tab->n_dead; col < tab->n_col; ++col) {
1181 if (tab->col_var[col] >= 0 &&
1182 (tab->col_var[col] < tab->n_param ||
1183 tab->col_var[col] >= tab->n_var - tab->n_div))
1184 continue;
1185 for (var = tab->n_param; var < tab->n_var - tab->n_div; ++var) {
1186 if (!tab->var[var].is_row) {
1187 if (tab->var[var].index == col)
1188 break;
1189 else
1190 continue;
1192 row = tab->var[var].index;
1193 if (isl_int_is_zero(tab->mat->row[row][off + col]))
1194 continue;
1195 if (isl_int_is_pos(tab->mat->row[row][off + col]))
1196 break;
1197 fprintf(stderr, "lexneg column %d (row %d)\n",
1198 col, row);
1200 if (var >= tab->n_var - tab->n_div)
1201 fprintf(stderr, "zero column %d\n", col);
1205 /* Report to the caller that the given constraint is part of an encountered
1206 * conflict.
1208 static int report_conflicting_constraint(struct isl_tab *tab, int con)
1210 return tab->conflict(con, tab->conflict_user);
1213 /* Given a conflicting row in the tableau, report all constraints
1214 * involved in the row to the caller. That is, the row itself
1215 * (if it represents a constraint) and all constraint columns with
1216 * non-zero (and therefore negative) coefficients.
1218 static int report_conflict(struct isl_tab *tab, int row)
1220 int j;
1221 isl_int *tr;
1223 if (!tab->conflict)
1224 return 0;
1226 if (tab->row_var[row] < 0 &&
1227 report_conflicting_constraint(tab, ~tab->row_var[row]) < 0)
1228 return -1;
1230 tr = tab->mat->row[row] + 2 + tab->M;
1232 for (j = tab->n_dead; j < tab->n_col; ++j) {
1233 if (tab->col_var[j] >= 0 &&
1234 (tab->col_var[j] < tab->n_param ||
1235 tab->col_var[j] >= tab->n_var - tab->n_div))
1236 continue;
1238 if (!isl_int_is_neg(tr[j]))
1239 continue;
1241 if (tab->col_var[j] < 0 &&
1242 report_conflicting_constraint(tab, ~tab->col_var[j]) < 0)
1243 return -1;
1246 return 0;
1249 /* Resolve all known or obviously violated constraints through pivoting.
1250 * In particular, as long as we can find any violated constraint, we
1251 * look for a pivoting column that would result in the lexicographically
1252 * smallest increment in the sample point. If there is no such column
1253 * then the tableau is infeasible.
1255 static int restore_lexmin(struct isl_tab *tab) WARN_UNUSED;
1256 static int restore_lexmin(struct isl_tab *tab)
1258 int row, col;
1260 if (!tab)
1261 return -1;
1262 if (tab->empty)
1263 return 0;
1264 while ((row = first_neg(tab)) != -1) {
1265 col = lexmin_pivot_col(tab, row);
1266 if (col >= tab->n_col) {
1267 if (report_conflict(tab, row) < 0)
1268 return -1;
1269 if (isl_tab_mark_empty(tab) < 0)
1270 return -1;
1271 return 0;
1273 if (col < 0)
1274 return -1;
1275 if (isl_tab_pivot(tab, row, col) < 0)
1276 return -1;
1278 return 0;
1281 /* Given a row that represents an equality, look for an appropriate
1282 * pivoting column.
1283 * In particular, if there are any non-zero coefficients among
1284 * the non-parameter variables, then we take the last of these
1285 * variables. Eliminating this variable in terms of the other
1286 * variables and/or parameters does not influence the property
1287 * that all column in the initial tableau are lexicographically
1288 * positive. The row corresponding to the eliminated variable
1289 * will only have non-zero entries below the diagonal of the
1290 * initial tableau. That is, we transform
1292 * I I
1293 * 1 into a
1294 * I I
1296 * If there is no such non-parameter variable, then we are dealing with
1297 * pure parameter equality and we pick any parameter with coefficient 1 or -1
1298 * for elimination. This will ensure that the eliminated parameter
1299 * always has an integer value whenever all the other parameters are integral.
1300 * If there is no such parameter then we return -1.
1302 static int last_var_col_or_int_par_col(struct isl_tab *tab, int row)
1304 unsigned off = 2 + tab->M;
1305 int i;
1307 for (i = tab->n_var - tab->n_div - 1; i >= 0 && i >= tab->n_param; --i) {
1308 int col;
1309 if (tab->var[i].is_row)
1310 continue;
1311 col = tab->var[i].index;
1312 if (col <= tab->n_dead)
1313 continue;
1314 if (!isl_int_is_zero(tab->mat->row[row][off + col]))
1315 return col;
1317 for (i = tab->n_dead; i < tab->n_col; ++i) {
1318 if (isl_int_is_one(tab->mat->row[row][off + i]))
1319 return i;
1320 if (isl_int_is_negone(tab->mat->row[row][off + i]))
1321 return i;
1323 return -1;
1326 /* Add an equality that is known to be valid to the tableau.
1327 * We first check if we can eliminate a variable or a parameter.
1328 * If not, we add the equality as two inequalities.
1329 * In this case, the equality was a pure parameter equality and there
1330 * is no need to resolve any constraint violations.
1332 static struct isl_tab *add_lexmin_valid_eq(struct isl_tab *tab, isl_int *eq)
1334 int i;
1335 int r;
1337 if (!tab)
1338 return NULL;
1339 r = isl_tab_add_row(tab, eq);
1340 if (r < 0)
1341 goto error;
1343 r = tab->con[r].index;
1344 i = last_var_col_or_int_par_col(tab, r);
1345 if (i < 0) {
1346 tab->con[r].is_nonneg = 1;
1347 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1348 goto error;
1349 isl_seq_neg(eq, eq, 1 + tab->n_var);
1350 r = isl_tab_add_row(tab, eq);
1351 if (r < 0)
1352 goto error;
1353 tab->con[r].is_nonneg = 1;
1354 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1355 goto error;
1356 } else {
1357 if (isl_tab_pivot(tab, r, i) < 0)
1358 goto error;
1359 if (isl_tab_kill_col(tab, i) < 0)
1360 goto error;
1361 tab->n_eq++;
1364 return tab;
1365 error:
1366 isl_tab_free(tab);
1367 return NULL;
1370 /* Check if the given row is a pure constant.
1372 static int is_constant(struct isl_tab *tab, int row)
1374 unsigned off = 2 + tab->M;
1376 return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1377 tab->n_col - tab->n_dead) == -1;
1380 /* Add an equality that may or may not be valid to the tableau.
1381 * If the resulting row is a pure constant, then it must be zero.
1382 * Otherwise, the resulting tableau is empty.
1384 * If the row is not a pure constant, then we add two inequalities,
1385 * each time checking that they can be satisfied.
1386 * In the end we try to use one of the two constraints to eliminate
1387 * a column.
1389 static int add_lexmin_eq(struct isl_tab *tab, isl_int *eq) WARN_UNUSED;
1390 static int add_lexmin_eq(struct isl_tab *tab, isl_int *eq)
1392 int r1, r2;
1393 int row;
1394 struct isl_tab_undo *snap;
1396 if (!tab)
1397 return -1;
1398 snap = isl_tab_snap(tab);
1399 r1 = isl_tab_add_row(tab, eq);
1400 if (r1 < 0)
1401 return -1;
1402 tab->con[r1].is_nonneg = 1;
1403 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r1]) < 0)
1404 return -1;
1406 row = tab->con[r1].index;
1407 if (is_constant(tab, row)) {
1408 if (!isl_int_is_zero(tab->mat->row[row][1]) ||
1409 (tab->M && !isl_int_is_zero(tab->mat->row[row][2]))) {
1410 if (isl_tab_mark_empty(tab) < 0)
1411 return -1;
1412 return 0;
1414 if (isl_tab_rollback(tab, snap) < 0)
1415 return -1;
1416 return 0;
1419 if (restore_lexmin(tab) < 0)
1420 return -1;
1421 if (tab->empty)
1422 return 0;
1424 isl_seq_neg(eq, eq, 1 + tab->n_var);
1426 r2 = isl_tab_add_row(tab, eq);
1427 if (r2 < 0)
1428 return -1;
1429 tab->con[r2].is_nonneg = 1;
1430 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r2]) < 0)
1431 return -1;
1433 if (restore_lexmin(tab) < 0)
1434 return -1;
1435 if (tab->empty)
1436 return 0;
1438 if (!tab->con[r1].is_row) {
1439 if (isl_tab_kill_col(tab, tab->con[r1].index) < 0)
1440 return -1;
1441 } else if (!tab->con[r2].is_row) {
1442 if (isl_tab_kill_col(tab, tab->con[r2].index) < 0)
1443 return -1;
1446 if (tab->bmap) {
1447 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1448 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1449 return -1;
1450 isl_seq_neg(eq, eq, 1 + tab->n_var);
1451 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1452 isl_seq_neg(eq, eq, 1 + tab->n_var);
1453 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1454 return -1;
1455 if (!tab->bmap)
1456 return -1;
1459 return 0;
1462 /* Add an inequality to the tableau, resolving violations using
1463 * restore_lexmin.
1465 static struct isl_tab *add_lexmin_ineq(struct isl_tab *tab, isl_int *ineq)
1467 int r;
1469 if (!tab)
1470 return NULL;
1471 if (tab->bmap) {
1472 tab->bmap = isl_basic_map_add_ineq(tab->bmap, ineq);
1473 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1474 goto error;
1475 if (!tab->bmap)
1476 goto error;
1478 r = isl_tab_add_row(tab, ineq);
1479 if (r < 0)
1480 goto error;
1481 tab->con[r].is_nonneg = 1;
1482 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1483 goto error;
1484 if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1485 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1486 goto error;
1487 return tab;
1490 if (restore_lexmin(tab) < 0)
1491 goto error;
1492 if (!tab->empty && tab->con[r].is_row &&
1493 isl_tab_row_is_redundant(tab, tab->con[r].index))
1494 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1495 goto error;
1496 return tab;
1497 error:
1498 isl_tab_free(tab);
1499 return NULL;
1502 /* Check if the coefficients of the parameters are all integral.
1504 static int integer_parameter(struct isl_tab *tab, int row)
1506 int i;
1507 int col;
1508 unsigned off = 2 + tab->M;
1510 for (i = 0; i < tab->n_param; ++i) {
1511 /* Eliminated parameter */
1512 if (tab->var[i].is_row)
1513 continue;
1514 col = tab->var[i].index;
1515 if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
1516 tab->mat->row[row][0]))
1517 return 0;
1519 for (i = 0; i < tab->n_div; ++i) {
1520 if (tab->var[tab->n_var - tab->n_div + i].is_row)
1521 continue;
1522 col = tab->var[tab->n_var - tab->n_div + i].index;
1523 if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
1524 tab->mat->row[row][0]))
1525 return 0;
1527 return 1;
1530 /* Check if the coefficients of the non-parameter variables are all integral.
1532 static int integer_variable(struct isl_tab *tab, int row)
1534 int i;
1535 unsigned off = 2 + tab->M;
1537 for (i = tab->n_dead; i < tab->n_col; ++i) {
1538 if (tab->col_var[i] >= 0 &&
1539 (tab->col_var[i] < tab->n_param ||
1540 tab->col_var[i] >= tab->n_var - tab->n_div))
1541 continue;
1542 if (!isl_int_is_divisible_by(tab->mat->row[row][off + i],
1543 tab->mat->row[row][0]))
1544 return 0;
1546 return 1;
1549 /* Check if the constant term is integral.
1551 static int integer_constant(struct isl_tab *tab, int row)
1553 return isl_int_is_divisible_by(tab->mat->row[row][1],
1554 tab->mat->row[row][0]);
1557 #define I_CST 1 << 0
1558 #define I_PAR 1 << 1
1559 #define I_VAR 1 << 2
1561 /* Check for next (non-parameter) variable after "var" (first if var == -1)
1562 * that is non-integer and therefore requires a cut and return
1563 * the index of the variable.
1564 * For parametric tableaus, there are three parts in a row,
1565 * the constant, the coefficients of the parameters and the rest.
1566 * For each part, we check whether the coefficients in that part
1567 * are all integral and if so, set the corresponding flag in *f.
1568 * If the constant and the parameter part are integral, then the
1569 * current sample value is integral and no cut is required
1570 * (irrespective of whether the variable part is integral).
1572 static int next_non_integer_var(struct isl_tab *tab, int var, int *f)
1574 var = var < 0 ? tab->n_param : var + 1;
1576 for (; var < tab->n_var - tab->n_div; ++var) {
1577 int flags = 0;
1578 int row;
1579 if (!tab->var[var].is_row)
1580 continue;
1581 row = tab->var[var].index;
1582 if (integer_constant(tab, row))
1583 ISL_FL_SET(flags, I_CST);
1584 if (integer_parameter(tab, row))
1585 ISL_FL_SET(flags, I_PAR);
1586 if (ISL_FL_ISSET(flags, I_CST) && ISL_FL_ISSET(flags, I_PAR))
1587 continue;
1588 if (integer_variable(tab, row))
1589 ISL_FL_SET(flags, I_VAR);
1590 *f = flags;
1591 return var;
1593 return -1;
1596 /* Check for first (non-parameter) variable that is non-integer and
1597 * therefore requires a cut and return the corresponding row.
1598 * For parametric tableaus, there are three parts in a row,
1599 * the constant, the coefficients of the parameters and the rest.
1600 * For each part, we check whether the coefficients in that part
1601 * are all integral and if so, set the corresponding flag in *f.
1602 * If the constant and the parameter part are integral, then the
1603 * current sample value is integral and no cut is required
1604 * (irrespective of whether the variable part is integral).
1606 static int first_non_integer_row(struct isl_tab *tab, int *f)
1608 int var = next_non_integer_var(tab, -1, f);
1610 return var < 0 ? -1 : tab->var[var].index;
1613 /* Add a (non-parametric) cut to cut away the non-integral sample
1614 * value of the given row.
1616 * If the row is given by
1618 * m r = f + \sum_i a_i y_i
1620 * then the cut is
1622 * c = - {-f/m} + \sum_i {a_i/m} y_i >= 0
1624 * The big parameter, if any, is ignored, since it is assumed to be big
1625 * enough to be divisible by any integer.
1626 * If the tableau is actually a parametric tableau, then this function
1627 * is only called when all coefficients of the parameters are integral.
1628 * The cut therefore has zero coefficients for the parameters.
1630 * The current value is known to be negative, so row_sign, if it
1631 * exists, is set accordingly.
1633 * Return the row of the cut or -1.
1635 static int add_cut(struct isl_tab *tab, int row)
1637 int i;
1638 int r;
1639 isl_int *r_row;
1640 unsigned off = 2 + tab->M;
1642 if (isl_tab_extend_cons(tab, 1) < 0)
1643 return -1;
1644 r = isl_tab_allocate_con(tab);
1645 if (r < 0)
1646 return -1;
1648 r_row = tab->mat->row[tab->con[r].index];
1649 isl_int_set(r_row[0], tab->mat->row[row][0]);
1650 isl_int_neg(r_row[1], tab->mat->row[row][1]);
1651 isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
1652 isl_int_neg(r_row[1], r_row[1]);
1653 if (tab->M)
1654 isl_int_set_si(r_row[2], 0);
1655 for (i = 0; i < tab->n_col; ++i)
1656 isl_int_fdiv_r(r_row[off + i],
1657 tab->mat->row[row][off + i], tab->mat->row[row][0]);
1659 tab->con[r].is_nonneg = 1;
1660 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1661 return -1;
1662 if (tab->row_sign)
1663 tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
1665 return tab->con[r].index;
1668 #define CUT_ALL 1
1669 #define CUT_ONE 0
1671 /* Given a non-parametric tableau, add cuts until an integer
1672 * sample point is obtained or until the tableau is determined
1673 * to be integer infeasible.
1674 * As long as there is any non-integer value in the sample point,
1675 * we add appropriate cuts, if possible, for each of these
1676 * non-integer values and then resolve the violated
1677 * cut constraints using restore_lexmin.
1678 * If one of the corresponding rows is equal to an integral
1679 * combination of variables/constraints plus a non-integral constant,
1680 * then there is no way to obtain an integer point and we return
1681 * a tableau that is marked empty.
1682 * The parameter cutting_strategy controls the strategy used when adding cuts
1683 * to remove non-integer points. CUT_ALL adds all possible cuts
1684 * before continuing the search. CUT_ONE adds only one cut at a time.
1686 static struct isl_tab *cut_to_integer_lexmin(struct isl_tab *tab,
1687 int cutting_strategy)
1689 int var;
1690 int row;
1691 int flags;
1693 if (!tab)
1694 return NULL;
1695 if (tab->empty)
1696 return tab;
1698 while ((var = next_non_integer_var(tab, -1, &flags)) != -1) {
1699 do {
1700 if (ISL_FL_ISSET(flags, I_VAR)) {
1701 if (isl_tab_mark_empty(tab) < 0)
1702 goto error;
1703 return tab;
1705 row = tab->var[var].index;
1706 row = add_cut(tab, row);
1707 if (row < 0)
1708 goto error;
1709 if (cutting_strategy == CUT_ONE)
1710 break;
1711 } while ((var = next_non_integer_var(tab, var, &flags)) != -1);
1712 if (restore_lexmin(tab) < 0)
1713 goto error;
1714 if (tab->empty)
1715 break;
1717 return tab;
1718 error:
1719 isl_tab_free(tab);
1720 return NULL;
1723 /* Check whether all the currently active samples also satisfy the inequality
1724 * "ineq" (treated as an equality if eq is set).
1725 * Remove those samples that do not.
1727 static struct isl_tab *check_samples(struct isl_tab *tab, isl_int *ineq, int eq)
1729 int i;
1730 isl_int v;
1732 if (!tab)
1733 return NULL;
1735 isl_assert(tab->mat->ctx, tab->bmap, goto error);
1736 isl_assert(tab->mat->ctx, tab->samples, goto error);
1737 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, goto error);
1739 isl_int_init(v);
1740 for (i = tab->n_outside; i < tab->n_sample; ++i) {
1741 int sgn;
1742 isl_seq_inner_product(ineq, tab->samples->row[i],
1743 1 + tab->n_var, &v);
1744 sgn = isl_int_sgn(v);
1745 if (eq ? (sgn == 0) : (sgn >= 0))
1746 continue;
1747 tab = isl_tab_drop_sample(tab, i);
1748 if (!tab)
1749 break;
1751 isl_int_clear(v);
1753 return tab;
1754 error:
1755 isl_tab_free(tab);
1756 return NULL;
1759 /* Check whether the sample value of the tableau is finite,
1760 * i.e., either the tableau does not use a big parameter, or
1761 * all values of the variables are equal to the big parameter plus
1762 * some constant. This constant is the actual sample value.
1764 static int sample_is_finite(struct isl_tab *tab)
1766 int i;
1768 if (!tab->M)
1769 return 1;
1771 for (i = 0; i < tab->n_var; ++i) {
1772 int row;
1773 if (!tab->var[i].is_row)
1774 return 0;
1775 row = tab->var[i].index;
1776 if (isl_int_ne(tab->mat->row[row][0], tab->mat->row[row][2]))
1777 return 0;
1779 return 1;
1782 /* Check if the context tableau of sol has any integer points.
1783 * Leave tab in empty state if no integer point can be found.
1784 * If an integer point can be found and if moreover it is finite,
1785 * then it is added to the list of sample values.
1787 * This function is only called when none of the currently active sample
1788 * values satisfies the most recently added constraint.
1790 static struct isl_tab *check_integer_feasible(struct isl_tab *tab)
1792 struct isl_tab_undo *snap;
1794 if (!tab)
1795 return NULL;
1797 snap = isl_tab_snap(tab);
1798 if (isl_tab_push_basis(tab) < 0)
1799 goto error;
1801 tab = cut_to_integer_lexmin(tab, CUT_ALL);
1802 if (!tab)
1803 goto error;
1805 if (!tab->empty && sample_is_finite(tab)) {
1806 struct isl_vec *sample;
1808 sample = isl_tab_get_sample_value(tab);
1810 if (isl_tab_add_sample(tab, sample) < 0)
1811 goto error;
1814 if (!tab->empty && isl_tab_rollback(tab, snap) < 0)
1815 goto error;
1817 return tab;
1818 error:
1819 isl_tab_free(tab);
1820 return NULL;
1823 /* Check if any of the currently active sample values satisfies
1824 * the inequality "ineq" (an equality if eq is set).
1826 static int tab_has_valid_sample(struct isl_tab *tab, isl_int *ineq, int eq)
1828 int i;
1829 isl_int v;
1831 if (!tab)
1832 return -1;
1834 isl_assert(tab->mat->ctx, tab->bmap, return -1);
1835 isl_assert(tab->mat->ctx, tab->samples, return -1);
1836 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, return -1);
1838 isl_int_init(v);
1839 for (i = tab->n_outside; i < tab->n_sample; ++i) {
1840 int sgn;
1841 isl_seq_inner_product(ineq, tab->samples->row[i],
1842 1 + tab->n_var, &v);
1843 sgn = isl_int_sgn(v);
1844 if (eq ? (sgn == 0) : (sgn >= 0))
1845 break;
1847 isl_int_clear(v);
1849 return i < tab->n_sample;
1852 /* Add a div specified by "div" to the tableau "tab" and return
1853 * 1 if the div is obviously non-negative.
1855 static int context_tab_add_div(struct isl_tab *tab, struct isl_vec *div,
1856 int (*add_ineq)(void *user, isl_int *), void *user)
1858 int i;
1859 int r;
1860 struct isl_mat *samples;
1861 int nonneg;
1863 r = isl_tab_add_div(tab, div, add_ineq, user);
1864 if (r < 0)
1865 return -1;
1866 nonneg = tab->var[r].is_nonneg;
1867 tab->var[r].frozen = 1;
1869 samples = isl_mat_extend(tab->samples,
1870 tab->n_sample, 1 + tab->n_var);
1871 tab->samples = samples;
1872 if (!samples)
1873 return -1;
1874 for (i = tab->n_outside; i < samples->n_row; ++i) {
1875 isl_seq_inner_product(div->el + 1, samples->row[i],
1876 div->size - 1, &samples->row[i][samples->n_col - 1]);
1877 isl_int_fdiv_q(samples->row[i][samples->n_col - 1],
1878 samples->row[i][samples->n_col - 1], div->el[0]);
1881 return nonneg;
1884 /* Add a div specified by "div" to both the main tableau and
1885 * the context tableau. In case of the main tableau, we only
1886 * need to add an extra div. In the context tableau, we also
1887 * need to express the meaning of the div.
1888 * Return the index of the div or -1 if anything went wrong.
1890 static int add_div(struct isl_tab *tab, struct isl_context *context,
1891 struct isl_vec *div)
1893 int r;
1894 int nonneg;
1896 if ((nonneg = context->op->add_div(context, div)) < 0)
1897 goto error;
1899 if (!context->op->is_ok(context))
1900 goto error;
1902 if (isl_tab_extend_vars(tab, 1) < 0)
1903 goto error;
1904 r = isl_tab_allocate_var(tab);
1905 if (r < 0)
1906 goto error;
1907 if (nonneg)
1908 tab->var[r].is_nonneg = 1;
1909 tab->var[r].frozen = 1;
1910 tab->n_div++;
1912 return tab->n_div - 1;
1913 error:
1914 context->op->invalidate(context);
1915 return -1;
1918 static int find_div(struct isl_tab *tab, isl_int *div, isl_int denom)
1920 int i;
1921 unsigned total = isl_basic_map_total_dim(tab->bmap);
1923 for (i = 0; i < tab->bmap->n_div; ++i) {
1924 if (isl_int_ne(tab->bmap->div[i][0], denom))
1925 continue;
1926 if (!isl_seq_eq(tab->bmap->div[i] + 1, div, 1 + total))
1927 continue;
1928 return i;
1930 return -1;
1933 /* Return the index of a div that corresponds to "div".
1934 * We first check if we already have such a div and if not, we create one.
1936 static int get_div(struct isl_tab *tab, struct isl_context *context,
1937 struct isl_vec *div)
1939 int d;
1940 struct isl_tab *context_tab = context->op->peek_tab(context);
1942 if (!context_tab)
1943 return -1;
1945 d = find_div(context_tab, div->el + 1, div->el[0]);
1946 if (d != -1)
1947 return d;
1949 return add_div(tab, context, div);
1952 /* Add a parametric cut to cut away the non-integral sample value
1953 * of the give row.
1954 * Let a_i be the coefficients of the constant term and the parameters
1955 * and let b_i be the coefficients of the variables or constraints
1956 * in basis of the tableau.
1957 * Let q be the div q = floor(\sum_i {-a_i} y_i).
1959 * The cut is expressed as
1961 * c = \sum_i -{-a_i} y_i + \sum_i {b_i} x_i + q >= 0
1963 * If q did not already exist in the context tableau, then it is added first.
1964 * If q is in a column of the main tableau then the "+ q" can be accomplished
1965 * by setting the corresponding entry to the denominator of the constraint.
1966 * If q happens to be in a row of the main tableau, then the corresponding
1967 * row needs to be added instead (taking care of the denominators).
1968 * Note that this is very unlikely, but perhaps not entirely impossible.
1970 * The current value of the cut is known to be negative (or at least
1971 * non-positive), so row_sign is set accordingly.
1973 * Return the row of the cut or -1.
1975 static int add_parametric_cut(struct isl_tab *tab, int row,
1976 struct isl_context *context)
1978 struct isl_vec *div;
1979 int d;
1980 int i;
1981 int r;
1982 isl_int *r_row;
1983 int col;
1984 int n;
1985 unsigned off = 2 + tab->M;
1987 if (!context)
1988 return -1;
1990 div = get_row_parameter_div(tab, row);
1991 if (!div)
1992 return -1;
1994 n = tab->n_div;
1995 d = context->op->get_div(context, tab, div);
1996 isl_vec_free(div);
1997 if (d < 0)
1998 return -1;
2000 if (isl_tab_extend_cons(tab, 1) < 0)
2001 return -1;
2002 r = isl_tab_allocate_con(tab);
2003 if (r < 0)
2004 return -1;
2006 r_row = tab->mat->row[tab->con[r].index];
2007 isl_int_set(r_row[0], tab->mat->row[row][0]);
2008 isl_int_neg(r_row[1], tab->mat->row[row][1]);
2009 isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
2010 isl_int_neg(r_row[1], r_row[1]);
2011 if (tab->M)
2012 isl_int_set_si(r_row[2], 0);
2013 for (i = 0; i < tab->n_param; ++i) {
2014 if (tab->var[i].is_row)
2015 continue;
2016 col = tab->var[i].index;
2017 isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
2018 isl_int_fdiv_r(r_row[off + col], r_row[off + col],
2019 tab->mat->row[row][0]);
2020 isl_int_neg(r_row[off + col], r_row[off + col]);
2022 for (i = 0; i < tab->n_div; ++i) {
2023 if (tab->var[tab->n_var - tab->n_div + i].is_row)
2024 continue;
2025 col = tab->var[tab->n_var - tab->n_div + i].index;
2026 isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
2027 isl_int_fdiv_r(r_row[off + col], r_row[off + col],
2028 tab->mat->row[row][0]);
2029 isl_int_neg(r_row[off + col], r_row[off + col]);
2031 for (i = 0; i < tab->n_col; ++i) {
2032 if (tab->col_var[i] >= 0 &&
2033 (tab->col_var[i] < tab->n_param ||
2034 tab->col_var[i] >= tab->n_var - tab->n_div))
2035 continue;
2036 isl_int_fdiv_r(r_row[off + i],
2037 tab->mat->row[row][off + i], tab->mat->row[row][0]);
2039 if (tab->var[tab->n_var - tab->n_div + d].is_row) {
2040 isl_int gcd;
2041 int d_row = tab->var[tab->n_var - tab->n_div + d].index;
2042 isl_int_init(gcd);
2043 isl_int_gcd(gcd, tab->mat->row[d_row][0], r_row[0]);
2044 isl_int_divexact(r_row[0], r_row[0], gcd);
2045 isl_int_divexact(gcd, tab->mat->row[d_row][0], gcd);
2046 isl_seq_combine(r_row + 1, gcd, r_row + 1,
2047 r_row[0], tab->mat->row[d_row] + 1,
2048 off - 1 + tab->n_col);
2049 isl_int_mul(r_row[0], r_row[0], tab->mat->row[d_row][0]);
2050 isl_int_clear(gcd);
2051 } else {
2052 col = tab->var[tab->n_var - tab->n_div + d].index;
2053 isl_int_set(r_row[off + col], tab->mat->row[row][0]);
2056 tab->con[r].is_nonneg = 1;
2057 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
2058 return -1;
2059 if (tab->row_sign)
2060 tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
2062 row = tab->con[r].index;
2064 if (d >= n && context->op->detect_equalities(context, tab) < 0)
2065 return -1;
2067 return row;
2070 /* Construct a tableau for bmap that can be used for computing
2071 * the lexicographic minimum (or maximum) of bmap.
2072 * If not NULL, then dom is the domain where the minimum
2073 * should be computed. In this case, we set up a parametric
2074 * tableau with row signs (initialized to "unknown").
2075 * If M is set, then the tableau will use a big parameter.
2076 * If max is set, then a maximum should be computed instead of a minimum.
2077 * This means that for each variable x, the tableau will contain the variable
2078 * x' = M - x, rather than x' = M + x. This in turn means that the coefficient
2079 * of the variables in all constraints are negated prior to adding them
2080 * to the tableau.
2082 static struct isl_tab *tab_for_lexmin(struct isl_basic_map *bmap,
2083 struct isl_basic_set *dom, unsigned M, int max)
2085 int i;
2086 struct isl_tab *tab;
2087 unsigned n_var;
2088 unsigned o_var;
2090 tab = isl_tab_alloc(bmap->ctx, 2 * bmap->n_eq + bmap->n_ineq + 1,
2091 isl_basic_map_total_dim(bmap), M);
2092 if (!tab)
2093 return NULL;
2095 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
2096 if (dom) {
2097 tab->n_param = isl_basic_set_total_dim(dom) - dom->n_div;
2098 tab->n_div = dom->n_div;
2099 tab->row_sign = isl_calloc_array(bmap->ctx,
2100 enum isl_tab_row_sign, tab->mat->n_row);
2101 if (tab->mat->n_row && !tab->row_sign)
2102 goto error;
2104 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
2105 if (isl_tab_mark_empty(tab) < 0)
2106 goto error;
2107 return tab;
2110 for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
2111 tab->var[i].is_nonneg = 1;
2112 tab->var[i].frozen = 1;
2114 o_var = 1 + tab->n_param;
2115 n_var = tab->n_var - tab->n_param - tab->n_div;
2116 for (i = 0; i < bmap->n_eq; ++i) {
2117 if (max)
2118 isl_seq_neg(bmap->eq[i] + o_var,
2119 bmap->eq[i] + o_var, n_var);
2120 tab = add_lexmin_valid_eq(tab, bmap->eq[i]);
2121 if (max)
2122 isl_seq_neg(bmap->eq[i] + o_var,
2123 bmap->eq[i] + o_var, n_var);
2124 if (!tab || tab->empty)
2125 return tab;
2127 if (bmap->n_eq && restore_lexmin(tab) < 0)
2128 goto error;
2129 for (i = 0; i < bmap->n_ineq; ++i) {
2130 if (max)
2131 isl_seq_neg(bmap->ineq[i] + o_var,
2132 bmap->ineq[i] + o_var, n_var);
2133 tab = add_lexmin_ineq(tab, bmap->ineq[i]);
2134 if (max)
2135 isl_seq_neg(bmap->ineq[i] + o_var,
2136 bmap->ineq[i] + o_var, n_var);
2137 if (!tab || tab->empty)
2138 return tab;
2140 return tab;
2141 error:
2142 isl_tab_free(tab);
2143 return NULL;
2146 /* Given a main tableau where more than one row requires a split,
2147 * determine and return the "best" row to split on.
2149 * Given two rows in the main tableau, if the inequality corresponding
2150 * to the first row is redundant with respect to that of the second row
2151 * in the current tableau, then it is better to split on the second row,
2152 * since in the positive part, both row will be positive.
2153 * (In the negative part a pivot will have to be performed and just about
2154 * anything can happen to the sign of the other row.)
2156 * As a simple heuristic, we therefore select the row that makes the most
2157 * of the other rows redundant.
2159 * Perhaps it would also be useful to look at the number of constraints
2160 * that conflict with any given constraint.
2162 * best is the best row so far (-1 when we have not found any row yet).
2163 * best_r is the number of other rows made redundant by row best.
2164 * When best is still -1, bset_r is meaningless, but it is initialized
2165 * to some arbitrary value (0) anyway. Without this redundant initialization
2166 * valgrind may warn about uninitialized memory accesses when isl
2167 * is compiled with some versions of gcc.
2169 static int best_split(struct isl_tab *tab, struct isl_tab *context_tab)
2171 struct isl_tab_undo *snap;
2172 int split;
2173 int row;
2174 int best = -1;
2175 int best_r = 0;
2177 if (isl_tab_extend_cons(context_tab, 2) < 0)
2178 return -1;
2180 snap = isl_tab_snap(context_tab);
2182 for (split = tab->n_redundant; split < tab->n_row; ++split) {
2183 struct isl_tab_undo *snap2;
2184 struct isl_vec *ineq = NULL;
2185 int r = 0;
2186 int ok;
2188 if (!isl_tab_var_from_row(tab, split)->is_nonneg)
2189 continue;
2190 if (tab->row_sign[split] != isl_tab_row_any)
2191 continue;
2193 ineq = get_row_parameter_ineq(tab, split);
2194 if (!ineq)
2195 return -1;
2196 ok = isl_tab_add_ineq(context_tab, ineq->el) >= 0;
2197 isl_vec_free(ineq);
2198 if (!ok)
2199 return -1;
2201 snap2 = isl_tab_snap(context_tab);
2203 for (row = tab->n_redundant; row < tab->n_row; ++row) {
2204 struct isl_tab_var *var;
2206 if (row == split)
2207 continue;
2208 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
2209 continue;
2210 if (tab->row_sign[row] != isl_tab_row_any)
2211 continue;
2213 ineq = get_row_parameter_ineq(tab, row);
2214 if (!ineq)
2215 return -1;
2216 ok = isl_tab_add_ineq(context_tab, ineq->el) >= 0;
2217 isl_vec_free(ineq);
2218 if (!ok)
2219 return -1;
2220 var = &context_tab->con[context_tab->n_con - 1];
2221 if (!context_tab->empty &&
2222 !isl_tab_min_at_most_neg_one(context_tab, var))
2223 r++;
2224 if (isl_tab_rollback(context_tab, snap2) < 0)
2225 return -1;
2227 if (best == -1 || r > best_r) {
2228 best = split;
2229 best_r = r;
2231 if (isl_tab_rollback(context_tab, snap) < 0)
2232 return -1;
2235 return best;
2238 static struct isl_basic_set *context_lex_peek_basic_set(
2239 struct isl_context *context)
2241 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2242 if (!clex->tab)
2243 return NULL;
2244 return isl_tab_peek_bset(clex->tab);
2247 static struct isl_tab *context_lex_peek_tab(struct isl_context *context)
2249 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2250 return clex->tab;
2253 static void context_lex_add_eq(struct isl_context *context, isl_int *eq,
2254 int check, int update)
2256 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2257 if (isl_tab_extend_cons(clex->tab, 2) < 0)
2258 goto error;
2259 if (add_lexmin_eq(clex->tab, eq) < 0)
2260 goto error;
2261 if (check) {
2262 int v = tab_has_valid_sample(clex->tab, eq, 1);
2263 if (v < 0)
2264 goto error;
2265 if (!v)
2266 clex->tab = check_integer_feasible(clex->tab);
2268 if (update)
2269 clex->tab = check_samples(clex->tab, eq, 1);
2270 return;
2271 error:
2272 isl_tab_free(clex->tab);
2273 clex->tab = NULL;
2276 static void context_lex_add_ineq(struct isl_context *context, isl_int *ineq,
2277 int check, int update)
2279 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2280 if (isl_tab_extend_cons(clex->tab, 1) < 0)
2281 goto error;
2282 clex->tab = add_lexmin_ineq(clex->tab, ineq);
2283 if (check) {
2284 int v = tab_has_valid_sample(clex->tab, ineq, 0);
2285 if (v < 0)
2286 goto error;
2287 if (!v)
2288 clex->tab = check_integer_feasible(clex->tab);
2290 if (update)
2291 clex->tab = check_samples(clex->tab, ineq, 0);
2292 return;
2293 error:
2294 isl_tab_free(clex->tab);
2295 clex->tab = NULL;
2298 static int context_lex_add_ineq_wrap(void *user, isl_int *ineq)
2300 struct isl_context *context = (struct isl_context *)user;
2301 context_lex_add_ineq(context, ineq, 0, 0);
2302 return context->op->is_ok(context) ? 0 : -1;
2305 /* Check which signs can be obtained by "ineq" on all the currently
2306 * active sample values. See row_sign for more information.
2308 static enum isl_tab_row_sign tab_ineq_sign(struct isl_tab *tab, isl_int *ineq,
2309 int strict)
2311 int i;
2312 int sgn;
2313 isl_int tmp;
2314 enum isl_tab_row_sign res = isl_tab_row_unknown;
2316 isl_assert(tab->mat->ctx, tab->samples, return isl_tab_row_unknown);
2317 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var,
2318 return isl_tab_row_unknown);
2320 isl_int_init(tmp);
2321 for (i = tab->n_outside; i < tab->n_sample; ++i) {
2322 isl_seq_inner_product(tab->samples->row[i], ineq,
2323 1 + tab->n_var, &tmp);
2324 sgn = isl_int_sgn(tmp);
2325 if (sgn > 0 || (sgn == 0 && strict)) {
2326 if (res == isl_tab_row_unknown)
2327 res = isl_tab_row_pos;
2328 if (res == isl_tab_row_neg)
2329 res = isl_tab_row_any;
2331 if (sgn < 0) {
2332 if (res == isl_tab_row_unknown)
2333 res = isl_tab_row_neg;
2334 if (res == isl_tab_row_pos)
2335 res = isl_tab_row_any;
2337 if (res == isl_tab_row_any)
2338 break;
2340 isl_int_clear(tmp);
2342 return res;
2345 static enum isl_tab_row_sign context_lex_ineq_sign(struct isl_context *context,
2346 isl_int *ineq, int strict)
2348 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2349 return tab_ineq_sign(clex->tab, ineq, strict);
2352 /* Check whether "ineq" can be added to the tableau without rendering
2353 * it infeasible.
2355 static int context_lex_test_ineq(struct isl_context *context, isl_int *ineq)
2357 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2358 struct isl_tab_undo *snap;
2359 int feasible;
2361 if (!clex->tab)
2362 return -1;
2364 if (isl_tab_extend_cons(clex->tab, 1) < 0)
2365 return -1;
2367 snap = isl_tab_snap(clex->tab);
2368 if (isl_tab_push_basis(clex->tab) < 0)
2369 return -1;
2370 clex->tab = add_lexmin_ineq(clex->tab, ineq);
2371 clex->tab = check_integer_feasible(clex->tab);
2372 if (!clex->tab)
2373 return -1;
2374 feasible = !clex->tab->empty;
2375 if (isl_tab_rollback(clex->tab, snap) < 0)
2376 return -1;
2378 return feasible;
2381 static int context_lex_get_div(struct isl_context *context, struct isl_tab *tab,
2382 struct isl_vec *div)
2384 return get_div(tab, context, div);
2387 /* Add a div specified by "div" to the context tableau and return
2388 * 1 if the div is obviously non-negative.
2389 * context_tab_add_div will always return 1, because all variables
2390 * in a isl_context_lex tableau are non-negative.
2391 * However, if we are using a big parameter in the context, then this only
2392 * reflects the non-negativity of the variable used to _encode_ the
2393 * div, i.e., div' = M + div, so we can't draw any conclusions.
2395 static int context_lex_add_div(struct isl_context *context, struct isl_vec *div)
2397 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2398 int nonneg;
2399 nonneg = context_tab_add_div(clex->tab, div,
2400 context_lex_add_ineq_wrap, context);
2401 if (nonneg < 0)
2402 return -1;
2403 if (clex->tab->M)
2404 return 0;
2405 return nonneg;
2408 static int context_lex_detect_equalities(struct isl_context *context,
2409 struct isl_tab *tab)
2411 return 0;
2414 static int context_lex_best_split(struct isl_context *context,
2415 struct isl_tab *tab)
2417 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2418 struct isl_tab_undo *snap;
2419 int r;
2421 snap = isl_tab_snap(clex->tab);
2422 if (isl_tab_push_basis(clex->tab) < 0)
2423 return -1;
2424 r = best_split(tab, clex->tab);
2426 if (r >= 0 && isl_tab_rollback(clex->tab, snap) < 0)
2427 return -1;
2429 return r;
2432 static int context_lex_is_empty(struct isl_context *context)
2434 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2435 if (!clex->tab)
2436 return -1;
2437 return clex->tab->empty;
2440 static void *context_lex_save(struct isl_context *context)
2442 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2443 struct isl_tab_undo *snap;
2445 snap = isl_tab_snap(clex->tab);
2446 if (isl_tab_push_basis(clex->tab) < 0)
2447 return NULL;
2448 if (isl_tab_save_samples(clex->tab) < 0)
2449 return NULL;
2451 return snap;
2454 static void context_lex_restore(struct isl_context *context, void *save)
2456 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2457 if (isl_tab_rollback(clex->tab, (struct isl_tab_undo *)save) < 0) {
2458 isl_tab_free(clex->tab);
2459 clex->tab = NULL;
2463 static void context_lex_discard(void *save)
2467 static int context_lex_is_ok(struct isl_context *context)
2469 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2470 return !!clex->tab;
2473 /* For each variable in the context tableau, check if the variable can
2474 * only attain non-negative values. If so, mark the parameter as non-negative
2475 * in the main tableau. This allows for a more direct identification of some
2476 * cases of violated constraints.
2478 static struct isl_tab *tab_detect_nonnegative_parameters(struct isl_tab *tab,
2479 struct isl_tab *context_tab)
2481 int i;
2482 struct isl_tab_undo *snap;
2483 struct isl_vec *ineq = NULL;
2484 struct isl_tab_var *var;
2485 int n;
2487 if (context_tab->n_var == 0)
2488 return tab;
2490 ineq = isl_vec_alloc(tab->mat->ctx, 1 + context_tab->n_var);
2491 if (!ineq)
2492 goto error;
2494 if (isl_tab_extend_cons(context_tab, 1) < 0)
2495 goto error;
2497 snap = isl_tab_snap(context_tab);
2499 n = 0;
2500 isl_seq_clr(ineq->el, ineq->size);
2501 for (i = 0; i < context_tab->n_var; ++i) {
2502 isl_int_set_si(ineq->el[1 + i], 1);
2503 if (isl_tab_add_ineq(context_tab, ineq->el) < 0)
2504 goto error;
2505 var = &context_tab->con[context_tab->n_con - 1];
2506 if (!context_tab->empty &&
2507 !isl_tab_min_at_most_neg_one(context_tab, var)) {
2508 int j = i;
2509 if (i >= tab->n_param)
2510 j = i - tab->n_param + tab->n_var - tab->n_div;
2511 tab->var[j].is_nonneg = 1;
2512 n++;
2514 isl_int_set_si(ineq->el[1 + i], 0);
2515 if (isl_tab_rollback(context_tab, snap) < 0)
2516 goto error;
2519 if (context_tab->M && n == context_tab->n_var) {
2520 context_tab->mat = isl_mat_drop_cols(context_tab->mat, 2, 1);
2521 context_tab->M = 0;
2524 isl_vec_free(ineq);
2525 return tab;
2526 error:
2527 isl_vec_free(ineq);
2528 isl_tab_free(tab);
2529 return NULL;
2532 static struct isl_tab *context_lex_detect_nonnegative_parameters(
2533 struct isl_context *context, struct isl_tab *tab)
2535 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2536 struct isl_tab_undo *snap;
2538 if (!tab)
2539 return NULL;
2541 snap = isl_tab_snap(clex->tab);
2542 if (isl_tab_push_basis(clex->tab) < 0)
2543 goto error;
2545 tab = tab_detect_nonnegative_parameters(tab, clex->tab);
2547 if (isl_tab_rollback(clex->tab, snap) < 0)
2548 goto error;
2550 return tab;
2551 error:
2552 isl_tab_free(tab);
2553 return NULL;
2556 static void context_lex_invalidate(struct isl_context *context)
2558 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2559 isl_tab_free(clex->tab);
2560 clex->tab = NULL;
2563 static void context_lex_free(struct isl_context *context)
2565 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2566 isl_tab_free(clex->tab);
2567 free(clex);
2570 struct isl_context_op isl_context_lex_op = {
2571 context_lex_detect_nonnegative_parameters,
2572 context_lex_peek_basic_set,
2573 context_lex_peek_tab,
2574 context_lex_add_eq,
2575 context_lex_add_ineq,
2576 context_lex_ineq_sign,
2577 context_lex_test_ineq,
2578 context_lex_get_div,
2579 context_lex_add_div,
2580 context_lex_detect_equalities,
2581 context_lex_best_split,
2582 context_lex_is_empty,
2583 context_lex_is_ok,
2584 context_lex_save,
2585 context_lex_restore,
2586 context_lex_discard,
2587 context_lex_invalidate,
2588 context_lex_free,
2591 static struct isl_tab *context_tab_for_lexmin(struct isl_basic_set *bset)
2593 struct isl_tab *tab;
2595 if (!bset)
2596 return NULL;
2597 tab = tab_for_lexmin((struct isl_basic_map *)bset, NULL, 1, 0);
2598 if (!tab)
2599 goto error;
2600 if (isl_tab_track_bset(tab, bset) < 0)
2601 goto error;
2602 tab = isl_tab_init_samples(tab);
2603 return tab;
2604 error:
2605 isl_basic_set_free(bset);
2606 return NULL;
2609 static struct isl_context *isl_context_lex_alloc(struct isl_basic_set *dom)
2611 struct isl_context_lex *clex;
2613 if (!dom)
2614 return NULL;
2616 clex = isl_alloc_type(dom->ctx, struct isl_context_lex);
2617 if (!clex)
2618 return NULL;
2620 clex->context.op = &isl_context_lex_op;
2622 clex->tab = context_tab_for_lexmin(isl_basic_set_copy(dom));
2623 if (restore_lexmin(clex->tab) < 0)
2624 goto error;
2625 clex->tab = check_integer_feasible(clex->tab);
2626 if (!clex->tab)
2627 goto error;
2629 return &clex->context;
2630 error:
2631 clex->context.op->free(&clex->context);
2632 return NULL;
2635 /* Representation of the context when using generalized basis reduction.
2637 * "shifted" contains the offsets of the unit hypercubes that lie inside the
2638 * context. Any rational point in "shifted" can therefore be rounded
2639 * up to an integer point in the context.
2640 * If the context is constrained by any equality, then "shifted" is not used
2641 * as it would be empty.
2643 struct isl_context_gbr {
2644 struct isl_context context;
2645 struct isl_tab *tab;
2646 struct isl_tab *shifted;
2647 struct isl_tab *cone;
2650 static struct isl_tab *context_gbr_detect_nonnegative_parameters(
2651 struct isl_context *context, struct isl_tab *tab)
2653 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2654 if (!tab)
2655 return NULL;
2656 return tab_detect_nonnegative_parameters(tab, cgbr->tab);
2659 static struct isl_basic_set *context_gbr_peek_basic_set(
2660 struct isl_context *context)
2662 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2663 if (!cgbr->tab)
2664 return NULL;
2665 return isl_tab_peek_bset(cgbr->tab);
2668 static struct isl_tab *context_gbr_peek_tab(struct isl_context *context)
2670 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2671 return cgbr->tab;
2674 /* Initialize the "shifted" tableau of the context, which
2675 * contains the constraints of the original tableau shifted
2676 * by the sum of all negative coefficients. This ensures
2677 * that any rational point in the shifted tableau can
2678 * be rounded up to yield an integer point in the original tableau.
2680 static void gbr_init_shifted(struct isl_context_gbr *cgbr)
2682 int i, j;
2683 struct isl_vec *cst;
2684 struct isl_basic_set *bset = isl_tab_peek_bset(cgbr->tab);
2685 unsigned dim = isl_basic_set_total_dim(bset);
2687 cst = isl_vec_alloc(cgbr->tab->mat->ctx, bset->n_ineq);
2688 if (!cst)
2689 return;
2691 for (i = 0; i < bset->n_ineq; ++i) {
2692 isl_int_set(cst->el[i], bset->ineq[i][0]);
2693 for (j = 0; j < dim; ++j) {
2694 if (!isl_int_is_neg(bset->ineq[i][1 + j]))
2695 continue;
2696 isl_int_add(bset->ineq[i][0], bset->ineq[i][0],
2697 bset->ineq[i][1 + j]);
2701 cgbr->shifted = isl_tab_from_basic_set(bset, 0);
2703 for (i = 0; i < bset->n_ineq; ++i)
2704 isl_int_set(bset->ineq[i][0], cst->el[i]);
2706 isl_vec_free(cst);
2709 /* Check if the shifted tableau is non-empty, and if so
2710 * use the sample point to construct an integer point
2711 * of the context tableau.
2713 static struct isl_vec *gbr_get_shifted_sample(struct isl_context_gbr *cgbr)
2715 struct isl_vec *sample;
2717 if (!cgbr->shifted)
2718 gbr_init_shifted(cgbr);
2719 if (!cgbr->shifted)
2720 return NULL;
2721 if (cgbr->shifted->empty)
2722 return isl_vec_alloc(cgbr->tab->mat->ctx, 0);
2724 sample = isl_tab_get_sample_value(cgbr->shifted);
2725 sample = isl_vec_ceil(sample);
2727 return sample;
2730 static struct isl_basic_set *drop_constant_terms(struct isl_basic_set *bset)
2732 int i;
2734 if (!bset)
2735 return NULL;
2737 for (i = 0; i < bset->n_eq; ++i)
2738 isl_int_set_si(bset->eq[i][0], 0);
2740 for (i = 0; i < bset->n_ineq; ++i)
2741 isl_int_set_si(bset->ineq[i][0], 0);
2743 return bset;
2746 static int use_shifted(struct isl_context_gbr *cgbr)
2748 if (!cgbr->tab)
2749 return 0;
2750 return cgbr->tab->bmap->n_eq == 0 && cgbr->tab->bmap->n_div == 0;
2753 static struct isl_vec *gbr_get_sample(struct isl_context_gbr *cgbr)
2755 struct isl_basic_set *bset;
2756 struct isl_basic_set *cone;
2758 if (isl_tab_sample_is_integer(cgbr->tab))
2759 return isl_tab_get_sample_value(cgbr->tab);
2761 if (use_shifted(cgbr)) {
2762 struct isl_vec *sample;
2764 sample = gbr_get_shifted_sample(cgbr);
2765 if (!sample || sample->size > 0)
2766 return sample;
2768 isl_vec_free(sample);
2771 if (!cgbr->cone) {
2772 bset = isl_tab_peek_bset(cgbr->tab);
2773 cgbr->cone = isl_tab_from_recession_cone(bset, 0);
2774 if (!cgbr->cone)
2775 return NULL;
2776 if (isl_tab_track_bset(cgbr->cone,
2777 isl_basic_set_copy(bset)) < 0)
2778 return NULL;
2780 if (isl_tab_detect_implicit_equalities(cgbr->cone) < 0)
2781 return NULL;
2783 if (cgbr->cone->n_dead == cgbr->cone->n_col) {
2784 struct isl_vec *sample;
2785 struct isl_tab_undo *snap;
2787 if (cgbr->tab->basis) {
2788 if (cgbr->tab->basis->n_col != 1 + cgbr->tab->n_var) {
2789 isl_mat_free(cgbr->tab->basis);
2790 cgbr->tab->basis = NULL;
2792 cgbr->tab->n_zero = 0;
2793 cgbr->tab->n_unbounded = 0;
2796 snap = isl_tab_snap(cgbr->tab);
2798 sample = isl_tab_sample(cgbr->tab);
2800 if (!sample || isl_tab_rollback(cgbr->tab, snap) < 0) {
2801 isl_vec_free(sample);
2802 return NULL;
2805 return sample;
2808 cone = isl_basic_set_dup(isl_tab_peek_bset(cgbr->cone));
2809 cone = drop_constant_terms(cone);
2810 cone = isl_basic_set_update_from_tab(cone, cgbr->cone);
2811 cone = isl_basic_set_underlying_set(cone);
2812 cone = isl_basic_set_gauss(cone, NULL);
2814 bset = isl_basic_set_dup(isl_tab_peek_bset(cgbr->tab));
2815 bset = isl_basic_set_update_from_tab(bset, cgbr->tab);
2816 bset = isl_basic_set_underlying_set(bset);
2817 bset = isl_basic_set_gauss(bset, NULL);
2819 return isl_basic_set_sample_with_cone(bset, cone);
2822 static void check_gbr_integer_feasible(struct isl_context_gbr *cgbr)
2824 struct isl_vec *sample;
2826 if (!cgbr->tab)
2827 return;
2829 if (cgbr->tab->empty)
2830 return;
2832 sample = gbr_get_sample(cgbr);
2833 if (!sample)
2834 goto error;
2836 if (sample->size == 0) {
2837 isl_vec_free(sample);
2838 if (isl_tab_mark_empty(cgbr->tab) < 0)
2839 goto error;
2840 return;
2843 if (isl_tab_add_sample(cgbr->tab, sample) < 0)
2844 goto error;
2846 return;
2847 error:
2848 isl_tab_free(cgbr->tab);
2849 cgbr->tab = NULL;
2852 static struct isl_tab *add_gbr_eq(struct isl_tab *tab, isl_int *eq)
2854 if (!tab)
2855 return NULL;
2857 if (isl_tab_extend_cons(tab, 2) < 0)
2858 goto error;
2860 if (isl_tab_add_eq(tab, eq) < 0)
2861 goto error;
2863 return tab;
2864 error:
2865 isl_tab_free(tab);
2866 return NULL;
2869 /* Add the equality described by "eq" to the context.
2870 * If "check" is set, then we check if the context is empty after
2871 * adding the equality.
2872 * If "update" is set, then we check if the samples are still valid.
2874 * We do not explicitly add shifted copies of the equality to
2875 * cgbr->shifted since they would conflict with each other.
2876 * Instead, we directly mark cgbr->shifted empty.
2878 static void context_gbr_add_eq(struct isl_context *context, isl_int *eq,
2879 int check, int update)
2881 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2883 cgbr->tab = add_gbr_eq(cgbr->tab, eq);
2885 if (cgbr->shifted && !cgbr->shifted->empty && use_shifted(cgbr)) {
2886 if (isl_tab_mark_empty(cgbr->shifted) < 0)
2887 goto error;
2890 if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
2891 if (isl_tab_extend_cons(cgbr->cone, 2) < 0)
2892 goto error;
2893 if (isl_tab_add_eq(cgbr->cone, eq) < 0)
2894 goto error;
2897 if (check) {
2898 int v = tab_has_valid_sample(cgbr->tab, eq, 1);
2899 if (v < 0)
2900 goto error;
2901 if (!v)
2902 check_gbr_integer_feasible(cgbr);
2904 if (update)
2905 cgbr->tab = check_samples(cgbr->tab, eq, 1);
2906 return;
2907 error:
2908 isl_tab_free(cgbr->tab);
2909 cgbr->tab = NULL;
2912 static void add_gbr_ineq(struct isl_context_gbr *cgbr, isl_int *ineq)
2914 if (!cgbr->tab)
2915 return;
2917 if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
2918 goto error;
2920 if (isl_tab_add_ineq(cgbr->tab, ineq) < 0)
2921 goto error;
2923 if (cgbr->shifted && !cgbr->shifted->empty && use_shifted(cgbr)) {
2924 int i;
2925 unsigned dim;
2926 dim = isl_basic_map_total_dim(cgbr->tab->bmap);
2928 if (isl_tab_extend_cons(cgbr->shifted, 1) < 0)
2929 goto error;
2931 for (i = 0; i < dim; ++i) {
2932 if (!isl_int_is_neg(ineq[1 + i]))
2933 continue;
2934 isl_int_add(ineq[0], ineq[0], ineq[1 + i]);
2937 if (isl_tab_add_ineq(cgbr->shifted, ineq) < 0)
2938 goto error;
2940 for (i = 0; i < dim; ++i) {
2941 if (!isl_int_is_neg(ineq[1 + i]))
2942 continue;
2943 isl_int_sub(ineq[0], ineq[0], ineq[1 + i]);
2947 if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
2948 if (isl_tab_extend_cons(cgbr->cone, 1) < 0)
2949 goto error;
2950 if (isl_tab_add_ineq(cgbr->cone, ineq) < 0)
2951 goto error;
2954 return;
2955 error:
2956 isl_tab_free(cgbr->tab);
2957 cgbr->tab = NULL;
2960 static void context_gbr_add_ineq(struct isl_context *context, isl_int *ineq,
2961 int check, int update)
2963 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2965 add_gbr_ineq(cgbr, ineq);
2966 if (!cgbr->tab)
2967 return;
2969 if (check) {
2970 int v = tab_has_valid_sample(cgbr->tab, ineq, 0);
2971 if (v < 0)
2972 goto error;
2973 if (!v)
2974 check_gbr_integer_feasible(cgbr);
2976 if (update)
2977 cgbr->tab = check_samples(cgbr->tab, ineq, 0);
2978 return;
2979 error:
2980 isl_tab_free(cgbr->tab);
2981 cgbr->tab = NULL;
2984 static int context_gbr_add_ineq_wrap(void *user, isl_int *ineq)
2986 struct isl_context *context = (struct isl_context *)user;
2987 context_gbr_add_ineq(context, ineq, 0, 0);
2988 return context->op->is_ok(context) ? 0 : -1;
2991 static enum isl_tab_row_sign context_gbr_ineq_sign(struct isl_context *context,
2992 isl_int *ineq, int strict)
2994 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2995 return tab_ineq_sign(cgbr->tab, ineq, strict);
2998 /* Check whether "ineq" can be added to the tableau without rendering
2999 * it infeasible.
3001 static int context_gbr_test_ineq(struct isl_context *context, isl_int *ineq)
3003 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3004 struct isl_tab_undo *snap;
3005 struct isl_tab_undo *shifted_snap = NULL;
3006 struct isl_tab_undo *cone_snap = NULL;
3007 int feasible;
3009 if (!cgbr->tab)
3010 return -1;
3012 if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
3013 return -1;
3015 snap = isl_tab_snap(cgbr->tab);
3016 if (cgbr->shifted)
3017 shifted_snap = isl_tab_snap(cgbr->shifted);
3018 if (cgbr->cone)
3019 cone_snap = isl_tab_snap(cgbr->cone);
3020 add_gbr_ineq(cgbr, ineq);
3021 check_gbr_integer_feasible(cgbr);
3022 if (!cgbr->tab)
3023 return -1;
3024 feasible = !cgbr->tab->empty;
3025 if (isl_tab_rollback(cgbr->tab, snap) < 0)
3026 return -1;
3027 if (shifted_snap) {
3028 if (isl_tab_rollback(cgbr->shifted, shifted_snap))
3029 return -1;
3030 } else if (cgbr->shifted) {
3031 isl_tab_free(cgbr->shifted);
3032 cgbr->shifted = NULL;
3034 if (cone_snap) {
3035 if (isl_tab_rollback(cgbr->cone, cone_snap))
3036 return -1;
3037 } else if (cgbr->cone) {
3038 isl_tab_free(cgbr->cone);
3039 cgbr->cone = NULL;
3042 return feasible;
3045 /* Return the column of the last of the variables associated to
3046 * a column that has a non-zero coefficient.
3047 * This function is called in a context where only coefficients
3048 * of parameters or divs can be non-zero.
3050 static int last_non_zero_var_col(struct isl_tab *tab, isl_int *p)
3052 int i;
3053 int col;
3055 if (tab->n_var == 0)
3056 return -1;
3058 for (i = tab->n_var - 1; i >= 0; --i) {
3059 if (i >= tab->n_param && i < tab->n_var - tab->n_div)
3060 continue;
3061 if (tab->var[i].is_row)
3062 continue;
3063 col = tab->var[i].index;
3064 if (!isl_int_is_zero(p[col]))
3065 return col;
3068 return -1;
3071 /* Look through all the recently added equalities in the context
3072 * to see if we can propagate any of them to the main tableau.
3074 * The newly added equalities in the context are encoded as pairs
3075 * of inequalities starting at inequality "first".
3077 * We tentatively add each of these equalities to the main tableau
3078 * and if this happens to result in a row with a final coefficient
3079 * that is one or negative one, we use it to kill a column
3080 * in the main tableau. Otherwise, we discard the tentatively
3081 * added row.
3083 * Return 0 on success and -1 on failure.
3085 static int propagate_equalities(struct isl_context_gbr *cgbr,
3086 struct isl_tab *tab, unsigned first)
3088 int i;
3089 struct isl_vec *eq = NULL;
3091 eq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
3092 if (!eq)
3093 goto error;
3095 if (isl_tab_extend_cons(tab, (cgbr->tab->bmap->n_ineq - first)/2) < 0)
3096 goto error;
3098 isl_seq_clr(eq->el + 1 + tab->n_param,
3099 tab->n_var - tab->n_param - tab->n_div);
3100 for (i = first; i < cgbr->tab->bmap->n_ineq; i += 2) {
3101 int j;
3102 int r;
3103 struct isl_tab_undo *snap;
3104 snap = isl_tab_snap(tab);
3106 isl_seq_cpy(eq->el, cgbr->tab->bmap->ineq[i], 1 + tab->n_param);
3107 isl_seq_cpy(eq->el + 1 + tab->n_var - tab->n_div,
3108 cgbr->tab->bmap->ineq[i] + 1 + tab->n_param,
3109 tab->n_div);
3111 r = isl_tab_add_row(tab, eq->el);
3112 if (r < 0)
3113 goto error;
3114 r = tab->con[r].index;
3115 j = last_non_zero_var_col(tab, tab->mat->row[r] + 2 + tab->M);
3116 if (j < 0 || j < tab->n_dead ||
3117 !isl_int_is_one(tab->mat->row[r][0]) ||
3118 (!isl_int_is_one(tab->mat->row[r][2 + tab->M + j]) &&
3119 !isl_int_is_negone(tab->mat->row[r][2 + tab->M + j]))) {
3120 if (isl_tab_rollback(tab, snap) < 0)
3121 goto error;
3122 continue;
3124 if (isl_tab_pivot(tab, r, j) < 0)
3125 goto error;
3126 if (isl_tab_kill_col(tab, j) < 0)
3127 goto error;
3129 if (restore_lexmin(tab) < 0)
3130 goto error;
3133 isl_vec_free(eq);
3135 return 0;
3136 error:
3137 isl_vec_free(eq);
3138 isl_tab_free(cgbr->tab);
3139 cgbr->tab = NULL;
3140 return -1;
3143 static int context_gbr_detect_equalities(struct isl_context *context,
3144 struct isl_tab *tab)
3146 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3147 unsigned n_ineq;
3149 if (!cgbr->cone) {
3150 struct isl_basic_set *bset = isl_tab_peek_bset(cgbr->tab);
3151 cgbr->cone = isl_tab_from_recession_cone(bset, 0);
3152 if (!cgbr->cone)
3153 goto error;
3154 if (isl_tab_track_bset(cgbr->cone,
3155 isl_basic_set_copy(bset)) < 0)
3156 goto error;
3158 if (isl_tab_detect_implicit_equalities(cgbr->cone) < 0)
3159 goto error;
3161 n_ineq = cgbr->tab->bmap->n_ineq;
3162 cgbr->tab = isl_tab_detect_equalities(cgbr->tab, cgbr->cone);
3163 if (!cgbr->tab)
3164 return -1;
3165 if (cgbr->tab->bmap->n_ineq > n_ineq &&
3166 propagate_equalities(cgbr, tab, n_ineq) < 0)
3167 return -1;
3169 return 0;
3170 error:
3171 isl_tab_free(cgbr->tab);
3172 cgbr->tab = NULL;
3173 return -1;
3176 static int context_gbr_get_div(struct isl_context *context, struct isl_tab *tab,
3177 struct isl_vec *div)
3179 return get_div(tab, context, div);
3182 static int context_gbr_add_div(struct isl_context *context, struct isl_vec *div)
3184 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3185 if (cgbr->cone) {
3186 int k;
3188 if (isl_tab_extend_cons(cgbr->cone, 3) < 0)
3189 return -1;
3190 if (isl_tab_extend_vars(cgbr->cone, 1) < 0)
3191 return -1;
3192 if (isl_tab_allocate_var(cgbr->cone) <0)
3193 return -1;
3195 cgbr->cone->bmap = isl_basic_map_extend_space(cgbr->cone->bmap,
3196 isl_basic_map_get_space(cgbr->cone->bmap), 1, 0, 2);
3197 k = isl_basic_map_alloc_div(cgbr->cone->bmap);
3198 if (k < 0)
3199 return -1;
3200 isl_seq_cpy(cgbr->cone->bmap->div[k], div->el, div->size);
3201 if (isl_tab_push(cgbr->cone, isl_tab_undo_bmap_div) < 0)
3202 return -1;
3204 return context_tab_add_div(cgbr->tab, div,
3205 context_gbr_add_ineq_wrap, context);
3208 static int context_gbr_best_split(struct isl_context *context,
3209 struct isl_tab *tab)
3211 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3212 struct isl_tab_undo *snap;
3213 int r;
3215 snap = isl_tab_snap(cgbr->tab);
3216 r = best_split(tab, cgbr->tab);
3218 if (r >= 0 && isl_tab_rollback(cgbr->tab, snap) < 0)
3219 return -1;
3221 return r;
3224 static int context_gbr_is_empty(struct isl_context *context)
3226 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3227 if (!cgbr->tab)
3228 return -1;
3229 return cgbr->tab->empty;
3232 struct isl_gbr_tab_undo {
3233 struct isl_tab_undo *tab_snap;
3234 struct isl_tab_undo *shifted_snap;
3235 struct isl_tab_undo *cone_snap;
3238 static void *context_gbr_save(struct isl_context *context)
3240 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3241 struct isl_gbr_tab_undo *snap;
3243 if (!cgbr->tab)
3244 return NULL;
3246 snap = isl_alloc_type(cgbr->tab->mat->ctx, struct isl_gbr_tab_undo);
3247 if (!snap)
3248 return NULL;
3250 snap->tab_snap = isl_tab_snap(cgbr->tab);
3251 if (isl_tab_save_samples(cgbr->tab) < 0)
3252 goto error;
3254 if (cgbr->shifted)
3255 snap->shifted_snap = isl_tab_snap(cgbr->shifted);
3256 else
3257 snap->shifted_snap = NULL;
3259 if (cgbr->cone)
3260 snap->cone_snap = isl_tab_snap(cgbr->cone);
3261 else
3262 snap->cone_snap = NULL;
3264 return snap;
3265 error:
3266 free(snap);
3267 return NULL;
3270 static void context_gbr_restore(struct isl_context *context, void *save)
3272 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3273 struct isl_gbr_tab_undo *snap = (struct isl_gbr_tab_undo *)save;
3274 if (!snap)
3275 goto error;
3276 if (isl_tab_rollback(cgbr->tab, snap->tab_snap) < 0)
3277 goto error;
3279 if (snap->shifted_snap) {
3280 if (isl_tab_rollback(cgbr->shifted, snap->shifted_snap) < 0)
3281 goto error;
3282 } else if (cgbr->shifted) {
3283 isl_tab_free(cgbr->shifted);
3284 cgbr->shifted = NULL;
3287 if (snap->cone_snap) {
3288 if (isl_tab_rollback(cgbr->cone, snap->cone_snap) < 0)
3289 goto error;
3290 } else if (cgbr->cone) {
3291 isl_tab_free(cgbr->cone);
3292 cgbr->cone = NULL;
3295 free(snap);
3297 return;
3298 error:
3299 free(snap);
3300 isl_tab_free(cgbr->tab);
3301 cgbr->tab = NULL;
3304 static void context_gbr_discard(void *save)
3306 struct isl_gbr_tab_undo *snap = (struct isl_gbr_tab_undo *)save;
3307 free(snap);
3310 static int context_gbr_is_ok(struct isl_context *context)
3312 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3313 return !!cgbr->tab;
3316 static void context_gbr_invalidate(struct isl_context *context)
3318 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3319 isl_tab_free(cgbr->tab);
3320 cgbr->tab = NULL;
3323 static void context_gbr_free(struct isl_context *context)
3325 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3326 isl_tab_free(cgbr->tab);
3327 isl_tab_free(cgbr->shifted);
3328 isl_tab_free(cgbr->cone);
3329 free(cgbr);
3332 struct isl_context_op isl_context_gbr_op = {
3333 context_gbr_detect_nonnegative_parameters,
3334 context_gbr_peek_basic_set,
3335 context_gbr_peek_tab,
3336 context_gbr_add_eq,
3337 context_gbr_add_ineq,
3338 context_gbr_ineq_sign,
3339 context_gbr_test_ineq,
3340 context_gbr_get_div,
3341 context_gbr_add_div,
3342 context_gbr_detect_equalities,
3343 context_gbr_best_split,
3344 context_gbr_is_empty,
3345 context_gbr_is_ok,
3346 context_gbr_save,
3347 context_gbr_restore,
3348 context_gbr_discard,
3349 context_gbr_invalidate,
3350 context_gbr_free,
3353 static struct isl_context *isl_context_gbr_alloc(struct isl_basic_set *dom)
3355 struct isl_context_gbr *cgbr;
3357 if (!dom)
3358 return NULL;
3360 cgbr = isl_calloc_type(dom->ctx, struct isl_context_gbr);
3361 if (!cgbr)
3362 return NULL;
3364 cgbr->context.op = &isl_context_gbr_op;
3366 cgbr->shifted = NULL;
3367 cgbr->cone = NULL;
3368 cgbr->tab = isl_tab_from_basic_set(dom, 1);
3369 cgbr->tab = isl_tab_init_samples(cgbr->tab);
3370 if (!cgbr->tab)
3371 goto error;
3372 check_gbr_integer_feasible(cgbr);
3374 return &cgbr->context;
3375 error:
3376 cgbr->context.op->free(&cgbr->context);
3377 return NULL;
3380 static struct isl_context *isl_context_alloc(struct isl_basic_set *dom)
3382 if (!dom)
3383 return NULL;
3385 if (dom->ctx->opt->context == ISL_CONTEXT_LEXMIN)
3386 return isl_context_lex_alloc(dom);
3387 else
3388 return isl_context_gbr_alloc(dom);
3391 /* Construct an isl_sol_map structure for accumulating the solution.
3392 * If track_empty is set, then we also keep track of the parts
3393 * of the context where there is no solution.
3394 * If max is set, then we are solving a maximization, rather than
3395 * a minimization problem, which means that the variables in the
3396 * tableau have value "M - x" rather than "M + x".
3398 static struct isl_sol *sol_map_init(struct isl_basic_map *bmap,
3399 struct isl_basic_set *dom, int track_empty, int max)
3401 struct isl_sol_map *sol_map = NULL;
3403 if (!bmap)
3404 goto error;
3406 sol_map = isl_calloc_type(bmap->ctx, struct isl_sol_map);
3407 if (!sol_map)
3408 goto error;
3410 sol_map->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
3411 sol_map->sol.dec_level.callback.run = &sol_dec_level_wrap;
3412 sol_map->sol.dec_level.sol = &sol_map->sol;
3413 sol_map->sol.max = max;
3414 sol_map->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
3415 sol_map->sol.add = &sol_map_add_wrap;
3416 sol_map->sol.add_empty = track_empty ? &sol_map_add_empty_wrap : NULL;
3417 sol_map->sol.free = &sol_map_free_wrap;
3418 sol_map->map = isl_map_alloc_space(isl_basic_map_get_space(bmap), 1,
3419 ISL_MAP_DISJOINT);
3420 if (!sol_map->map)
3421 goto error;
3423 sol_map->sol.context = isl_context_alloc(dom);
3424 if (!sol_map->sol.context)
3425 goto error;
3427 if (track_empty) {
3428 sol_map->empty = isl_set_alloc_space(isl_basic_set_get_space(dom),
3429 1, ISL_SET_DISJOINT);
3430 if (!sol_map->empty)
3431 goto error;
3434 isl_basic_set_free(dom);
3435 return &sol_map->sol;
3436 error:
3437 isl_basic_set_free(dom);
3438 sol_map_free(sol_map);
3439 return NULL;
3442 /* Check whether all coefficients of (non-parameter) variables
3443 * are non-positive, meaning that no pivots can be performed on the row.
3445 static int is_critical(struct isl_tab *tab, int row)
3447 int j;
3448 unsigned off = 2 + tab->M;
3450 for (j = tab->n_dead; j < tab->n_col; ++j) {
3451 if (tab->col_var[j] >= 0 &&
3452 (tab->col_var[j] < tab->n_param ||
3453 tab->col_var[j] >= tab->n_var - tab->n_div))
3454 continue;
3456 if (isl_int_is_pos(tab->mat->row[row][off + j]))
3457 return 0;
3460 return 1;
3463 /* Check whether the inequality represented by vec is strict over the integers,
3464 * i.e., there are no integer values satisfying the constraint with
3465 * equality. This happens if the gcd of the coefficients is not a divisor
3466 * of the constant term. If so, scale the constraint down by the gcd
3467 * of the coefficients.
3469 static int is_strict(struct isl_vec *vec)
3471 isl_int gcd;
3472 int strict = 0;
3474 isl_int_init(gcd);
3475 isl_seq_gcd(vec->el + 1, vec->size - 1, &gcd);
3476 if (!isl_int_is_one(gcd)) {
3477 strict = !isl_int_is_divisible_by(vec->el[0], gcd);
3478 isl_int_fdiv_q(vec->el[0], vec->el[0], gcd);
3479 isl_seq_scale_down(vec->el + 1, vec->el + 1, gcd, vec->size-1);
3481 isl_int_clear(gcd);
3483 return strict;
3486 /* Determine the sign of the given row of the main tableau.
3487 * The result is one of
3488 * isl_tab_row_pos: always non-negative; no pivot needed
3489 * isl_tab_row_neg: always non-positive; pivot
3490 * isl_tab_row_any: can be both positive and negative; split
3492 * We first handle some simple cases
3493 * - the row sign may be known already
3494 * - the row may be obviously non-negative
3495 * - the parametric constant may be equal to that of another row
3496 * for which we know the sign. This sign will be either "pos" or
3497 * "any". If it had been "neg" then we would have pivoted before.
3499 * If none of these cases hold, we check the value of the row for each
3500 * of the currently active samples. Based on the signs of these values
3501 * we make an initial determination of the sign of the row.
3503 * all zero -> unk(nown)
3504 * all non-negative -> pos
3505 * all non-positive -> neg
3506 * both negative and positive -> all
3508 * If we end up with "all", we are done.
3509 * Otherwise, we perform a check for positive and/or negative
3510 * values as follows.
3512 * samples neg unk pos
3513 * <0 ? Y N Y N
3514 * pos any pos
3515 * >0 ? Y N Y N
3516 * any neg any neg
3518 * There is no special sign for "zero", because we can usually treat zero
3519 * as either non-negative or non-positive, whatever works out best.
3520 * However, if the row is "critical", meaning that pivoting is impossible
3521 * then we don't want to limp zero with the non-positive case, because
3522 * then we we would lose the solution for those values of the parameters
3523 * where the value of the row is zero. Instead, we treat 0 as non-negative
3524 * ensuring a split if the row can attain both zero and negative values.
3525 * The same happens when the original constraint was one that could not
3526 * be satisfied with equality by any integer values of the parameters.
3527 * In this case, we normalize the constraint, but then a value of zero
3528 * for the normalized constraint is actually a positive value for the
3529 * original constraint, so again we need to treat zero as non-negative.
3530 * In both these cases, we have the following decision tree instead:
3532 * all non-negative -> pos
3533 * all negative -> neg
3534 * both negative and non-negative -> all
3536 * samples neg pos
3537 * <0 ? Y N
3538 * any pos
3539 * >=0 ? Y N
3540 * any neg
3542 static enum isl_tab_row_sign row_sign(struct isl_tab *tab,
3543 struct isl_sol *sol, int row)
3545 struct isl_vec *ineq = NULL;
3546 enum isl_tab_row_sign res = isl_tab_row_unknown;
3547 int critical;
3548 int strict;
3549 int row2;
3551 if (tab->row_sign[row] != isl_tab_row_unknown)
3552 return tab->row_sign[row];
3553 if (is_obviously_nonneg(tab, row))
3554 return isl_tab_row_pos;
3555 for (row2 = tab->n_redundant; row2 < tab->n_row; ++row2) {
3556 if (tab->row_sign[row2] == isl_tab_row_unknown)
3557 continue;
3558 if (identical_parameter_line(tab, row, row2))
3559 return tab->row_sign[row2];
3562 critical = is_critical(tab, row);
3564 ineq = get_row_parameter_ineq(tab, row);
3565 if (!ineq)
3566 goto error;
3568 strict = is_strict(ineq);
3570 res = sol->context->op->ineq_sign(sol->context, ineq->el,
3571 critical || strict);
3573 if (res == isl_tab_row_unknown || res == isl_tab_row_pos) {
3574 /* test for negative values */
3575 int feasible;
3576 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3577 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3579 feasible = sol->context->op->test_ineq(sol->context, ineq->el);
3580 if (feasible < 0)
3581 goto error;
3582 if (!feasible)
3583 res = isl_tab_row_pos;
3584 else
3585 res = (res == isl_tab_row_unknown) ? isl_tab_row_neg
3586 : isl_tab_row_any;
3587 if (res == isl_tab_row_neg) {
3588 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3589 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3593 if (res == isl_tab_row_neg) {
3594 /* test for positive values */
3595 int feasible;
3596 if (!critical && !strict)
3597 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3599 feasible = sol->context->op->test_ineq(sol->context, ineq->el);
3600 if (feasible < 0)
3601 goto error;
3602 if (feasible)
3603 res = isl_tab_row_any;
3606 isl_vec_free(ineq);
3607 return res;
3608 error:
3609 isl_vec_free(ineq);
3610 return isl_tab_row_unknown;
3613 static void find_solutions(struct isl_sol *sol, struct isl_tab *tab);
3615 /* Find solutions for values of the parameters that satisfy the given
3616 * inequality.
3618 * We currently take a snapshot of the context tableau that is reset
3619 * when we return from this function, while we make a copy of the main
3620 * tableau, leaving the original main tableau untouched.
3621 * These are fairly arbitrary choices. Making a copy also of the context
3622 * tableau would obviate the need to undo any changes made to it later,
3623 * while taking a snapshot of the main tableau could reduce memory usage.
3624 * If we were to switch to taking a snapshot of the main tableau,
3625 * we would have to keep in mind that we need to save the row signs
3626 * and that we need to do this before saving the current basis
3627 * such that the basis has been restore before we restore the row signs.
3629 static void find_in_pos(struct isl_sol *sol, struct isl_tab *tab, isl_int *ineq)
3631 void *saved;
3633 if (!sol->context)
3634 goto error;
3635 saved = sol->context->op->save(sol->context);
3637 tab = isl_tab_dup(tab);
3638 if (!tab)
3639 goto error;
3641 sol->context->op->add_ineq(sol->context, ineq, 0, 1);
3643 find_solutions(sol, tab);
3645 if (!sol->error)
3646 sol->context->op->restore(sol->context, saved);
3647 else
3648 sol->context->op->discard(saved);
3649 return;
3650 error:
3651 sol->error = 1;
3654 /* Record the absence of solutions for those values of the parameters
3655 * that do not satisfy the given inequality with equality.
3657 static void no_sol_in_strict(struct isl_sol *sol,
3658 struct isl_tab *tab, struct isl_vec *ineq)
3660 int empty;
3661 void *saved;
3663 if (!sol->context || sol->error)
3664 goto error;
3665 saved = sol->context->op->save(sol->context);
3667 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3669 sol->context->op->add_ineq(sol->context, ineq->el, 1, 0);
3670 if (!sol->context)
3671 goto error;
3673 empty = tab->empty;
3674 tab->empty = 1;
3675 sol_add(sol, tab);
3676 tab->empty = empty;
3678 isl_int_add_ui(ineq->el[0], ineq->el[0], 1);
3680 sol->context->op->restore(sol->context, saved);
3681 return;
3682 error:
3683 sol->error = 1;
3686 /* Compute the lexicographic minimum of the set represented by the main
3687 * tableau "tab" within the context "sol->context_tab".
3688 * On entry the sample value of the main tableau is lexicographically
3689 * less than or equal to this lexicographic minimum.
3690 * Pivots are performed until a feasible point is found, which is then
3691 * necessarily equal to the minimum, or until the tableau is found to
3692 * be infeasible. Some pivots may need to be performed for only some
3693 * feasible values of the context tableau. If so, the context tableau
3694 * is split into a part where the pivot is needed and a part where it is not.
3696 * Whenever we enter the main loop, the main tableau is such that no
3697 * "obvious" pivots need to be performed on it, where "obvious" means
3698 * that the given row can be seen to be negative without looking at
3699 * the context tableau. In particular, for non-parametric problems,
3700 * no pivots need to be performed on the main tableau.
3701 * The caller of find_solutions is responsible for making this property
3702 * hold prior to the first iteration of the loop, while restore_lexmin
3703 * is called before every other iteration.
3705 * Inside the main loop, we first examine the signs of the rows of
3706 * the main tableau within the context of the context tableau.
3707 * If we find a row that is always non-positive for all values of
3708 * the parameters satisfying the context tableau and negative for at
3709 * least one value of the parameters, we perform the appropriate pivot
3710 * and start over. An exception is the case where no pivot can be
3711 * performed on the row. In this case, we require that the sign of
3712 * the row is negative for all values of the parameters (rather than just
3713 * non-positive). This special case is handled inside row_sign, which
3714 * will say that the row can have any sign if it determines that it can
3715 * attain both negative and zero values.
3717 * If we can't find a row that always requires a pivot, but we can find
3718 * one or more rows that require a pivot for some values of the parameters
3719 * (i.e., the row can attain both positive and negative signs), then we split
3720 * the context tableau into two parts, one where we force the sign to be
3721 * non-negative and one where we force is to be negative.
3722 * The non-negative part is handled by a recursive call (through find_in_pos).
3723 * Upon returning from this call, we continue with the negative part and
3724 * perform the required pivot.
3726 * If no such rows can be found, all rows are non-negative and we have
3727 * found a (rational) feasible point. If we only wanted a rational point
3728 * then we are done.
3729 * Otherwise, we check if all values of the sample point of the tableau
3730 * are integral for the variables. If so, we have found the minimal
3731 * integral point and we are done.
3732 * If the sample point is not integral, then we need to make a distinction
3733 * based on whether the constant term is non-integral or the coefficients
3734 * of the parameters. Furthermore, in order to decide how to handle
3735 * the non-integrality, we also need to know whether the coefficients
3736 * of the other columns in the tableau are integral. This leads
3737 * to the following table. The first two rows do not correspond
3738 * to a non-integral sample point and are only mentioned for completeness.
3740 * constant parameters other
3742 * int int int |
3743 * int int rat | -> no problem
3745 * rat int int -> fail
3747 * rat int rat -> cut
3749 * int rat rat |
3750 * rat rat rat | -> parametric cut
3752 * int rat int |
3753 * rat rat int | -> split context
3755 * If the parametric constant is completely integral, then there is nothing
3756 * to be done. If the constant term is non-integral, but all the other
3757 * coefficient are integral, then there is nothing that can be done
3758 * and the tableau has no integral solution.
3759 * If, on the other hand, one or more of the other columns have rational
3760 * coefficients, but the parameter coefficients are all integral, then
3761 * we can perform a regular (non-parametric) cut.
3762 * Finally, if there is any parameter coefficient that is non-integral,
3763 * then we need to involve the context tableau. There are two cases here.
3764 * If at least one other column has a rational coefficient, then we
3765 * can perform a parametric cut in the main tableau by adding a new
3766 * integer division in the context tableau.
3767 * If all other columns have integral coefficients, then we need to
3768 * enforce that the rational combination of parameters (c + \sum a_i y_i)/m
3769 * is always integral. We do this by introducing an integer division
3770 * q = floor((c + \sum a_i y_i)/m) and stipulating that its argument should
3771 * always be integral in the context tableau, i.e., m q = c + \sum a_i y_i.
3772 * Since q is expressed in the tableau as
3773 * c + \sum a_i y_i - m q >= 0
3774 * -c - \sum a_i y_i + m q + m - 1 >= 0
3775 * it is sufficient to add the inequality
3776 * -c - \sum a_i y_i + m q >= 0
3777 * In the part of the context where this inequality does not hold, the
3778 * main tableau is marked as being empty.
3780 static void find_solutions(struct isl_sol *sol, struct isl_tab *tab)
3782 struct isl_context *context;
3783 int r;
3785 if (!tab || sol->error)
3786 goto error;
3788 context = sol->context;
3790 if (tab->empty)
3791 goto done;
3792 if (context->op->is_empty(context))
3793 goto done;
3795 for (r = 0; r >= 0 && tab && !tab->empty; r = restore_lexmin(tab)) {
3796 int flags;
3797 int row;
3798 enum isl_tab_row_sign sgn;
3799 int split = -1;
3800 int n_split = 0;
3802 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3803 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
3804 continue;
3805 sgn = row_sign(tab, sol, row);
3806 if (!sgn)
3807 goto error;
3808 tab->row_sign[row] = sgn;
3809 if (sgn == isl_tab_row_any)
3810 n_split++;
3811 if (sgn == isl_tab_row_any && split == -1)
3812 split = row;
3813 if (sgn == isl_tab_row_neg)
3814 break;
3816 if (row < tab->n_row)
3817 continue;
3818 if (split != -1) {
3819 struct isl_vec *ineq;
3820 if (n_split != 1)
3821 split = context->op->best_split(context, tab);
3822 if (split < 0)
3823 goto error;
3824 ineq = get_row_parameter_ineq(tab, split);
3825 if (!ineq)
3826 goto error;
3827 is_strict(ineq);
3828 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3829 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
3830 continue;
3831 if (tab->row_sign[row] == isl_tab_row_any)
3832 tab->row_sign[row] = isl_tab_row_unknown;
3834 tab->row_sign[split] = isl_tab_row_pos;
3835 sol_inc_level(sol);
3836 find_in_pos(sol, tab, ineq->el);
3837 tab->row_sign[split] = isl_tab_row_neg;
3838 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3839 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3840 if (!sol->error)
3841 context->op->add_ineq(context, ineq->el, 0, 1);
3842 isl_vec_free(ineq);
3843 if (sol->error)
3844 goto error;
3845 continue;
3847 if (tab->rational)
3848 break;
3849 row = first_non_integer_row(tab, &flags);
3850 if (row < 0)
3851 break;
3852 if (ISL_FL_ISSET(flags, I_PAR)) {
3853 if (ISL_FL_ISSET(flags, I_VAR)) {
3854 if (isl_tab_mark_empty(tab) < 0)
3855 goto error;
3856 break;
3858 row = add_cut(tab, row);
3859 } else if (ISL_FL_ISSET(flags, I_VAR)) {
3860 struct isl_vec *div;
3861 struct isl_vec *ineq;
3862 int d;
3863 div = get_row_split_div(tab, row);
3864 if (!div)
3865 goto error;
3866 d = context->op->get_div(context, tab, div);
3867 isl_vec_free(div);
3868 if (d < 0)
3869 goto error;
3870 ineq = ineq_for_div(context->op->peek_basic_set(context), d);
3871 if (!ineq)
3872 goto error;
3873 sol_inc_level(sol);
3874 no_sol_in_strict(sol, tab, ineq);
3875 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3876 context->op->add_ineq(context, ineq->el, 1, 1);
3877 isl_vec_free(ineq);
3878 if (sol->error || !context->op->is_ok(context))
3879 goto error;
3880 tab = set_row_cst_to_div(tab, row, d);
3881 if (context->op->is_empty(context))
3882 break;
3883 } else
3884 row = add_parametric_cut(tab, row, context);
3885 if (row < 0)
3886 goto error;
3888 if (r < 0)
3889 goto error;
3890 done:
3891 sol_add(sol, tab);
3892 isl_tab_free(tab);
3893 return;
3894 error:
3895 isl_tab_free(tab);
3896 sol->error = 1;
3899 /* Does "sol" contain a pair of partial solutions that could potentially
3900 * be merged?
3902 * We currently only check that "sol" is not in an error state
3903 * and that there are at least two partial solutions of which the final two
3904 * are defined at the same level.
3906 static int sol_has_mergeable_solutions(struct isl_sol *sol)
3908 if (sol->error)
3909 return 0;
3910 if (!sol->partial)
3911 return 0;
3912 if (!sol->partial->next)
3913 return 0;
3914 return sol->partial->level == sol->partial->next->level;
3917 /* Compute the lexicographic minimum of the set represented by the main
3918 * tableau "tab" within the context "sol->context_tab".
3920 * As a preprocessing step, we first transfer all the purely parametric
3921 * equalities from the main tableau to the context tableau, i.e.,
3922 * parameters that have been pivoted to a row.
3923 * These equalities are ignored by the main algorithm, because the
3924 * corresponding rows may not be marked as being non-negative.
3925 * In parts of the context where the added equality does not hold,
3926 * the main tableau is marked as being empty.
3928 * Before we embark on the actual computation, we save a copy
3929 * of the context. When we return, we check if there are any
3930 * partial solutions that can potentially be merged. If so,
3931 * we perform a rollback to the initial state of the context.
3932 * The merging of partial solutions happens inside calls to
3933 * sol_dec_level that are pushed onto the undo stack of the context.
3934 * If there are no partial solutions that can potentially be merged
3935 * then the rollback is skipped as it would just be wasted effort.
3937 static void find_solutions_main(struct isl_sol *sol, struct isl_tab *tab)
3939 int row;
3940 void *saved;
3942 if (!tab)
3943 goto error;
3945 sol->level = 0;
3947 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3948 int p;
3949 struct isl_vec *eq;
3951 if (tab->row_var[row] < 0)
3952 continue;
3953 if (tab->row_var[row] >= tab->n_param &&
3954 tab->row_var[row] < tab->n_var - tab->n_div)
3955 continue;
3956 if (tab->row_var[row] < tab->n_param)
3957 p = tab->row_var[row];
3958 else
3959 p = tab->row_var[row]
3960 + tab->n_param - (tab->n_var - tab->n_div);
3962 eq = isl_vec_alloc(tab->mat->ctx, 1+tab->n_param+tab->n_div);
3963 if (!eq)
3964 goto error;
3965 get_row_parameter_line(tab, row, eq->el);
3966 isl_int_neg(eq->el[1 + p], tab->mat->row[row][0]);
3967 eq = isl_vec_normalize(eq);
3969 sol_inc_level(sol);
3970 no_sol_in_strict(sol, tab, eq);
3972 isl_seq_neg(eq->el, eq->el, eq->size);
3973 sol_inc_level(sol);
3974 no_sol_in_strict(sol, tab, eq);
3975 isl_seq_neg(eq->el, eq->el, eq->size);
3977 sol->context->op->add_eq(sol->context, eq->el, 1, 1);
3979 isl_vec_free(eq);
3981 if (isl_tab_mark_redundant(tab, row) < 0)
3982 goto error;
3984 if (sol->context->op->is_empty(sol->context))
3985 break;
3987 row = tab->n_redundant - 1;
3990 saved = sol->context->op->save(sol->context);
3992 find_solutions(sol, tab);
3994 if (sol_has_mergeable_solutions(sol))
3995 sol->context->op->restore(sol->context, saved);
3996 else
3997 sol->context->op->discard(saved);
3999 sol->level = 0;
4000 sol_pop(sol);
4002 return;
4003 error:
4004 isl_tab_free(tab);
4005 sol->error = 1;
4008 /* Check if integer division "div" of "dom" also occurs in "bmap".
4009 * If so, return its position within the divs.
4010 * If not, return -1.
4012 static int find_context_div(struct isl_basic_map *bmap,
4013 struct isl_basic_set *dom, unsigned div)
4015 int i;
4016 unsigned b_dim = isl_space_dim(bmap->dim, isl_dim_all);
4017 unsigned d_dim = isl_space_dim(dom->dim, isl_dim_all);
4019 if (isl_int_is_zero(dom->div[div][0]))
4020 return -1;
4021 if (isl_seq_first_non_zero(dom->div[div] + 2 + d_dim, dom->n_div) != -1)
4022 return -1;
4024 for (i = 0; i < bmap->n_div; ++i) {
4025 if (isl_int_is_zero(bmap->div[i][0]))
4026 continue;
4027 if (isl_seq_first_non_zero(bmap->div[i] + 2 + d_dim,
4028 (b_dim - d_dim) + bmap->n_div) != -1)
4029 continue;
4030 if (isl_seq_eq(bmap->div[i], dom->div[div], 2 + d_dim))
4031 return i;
4033 return -1;
4036 /* The correspondence between the variables in the main tableau,
4037 * the context tableau, and the input map and domain is as follows.
4038 * The first n_param and the last n_div variables of the main tableau
4039 * form the variables of the context tableau.
4040 * In the basic map, these n_param variables correspond to the
4041 * parameters and the input dimensions. In the domain, they correspond
4042 * to the parameters and the set dimensions.
4043 * The n_div variables correspond to the integer divisions in the domain.
4044 * To ensure that everything lines up, we may need to copy some of the
4045 * integer divisions of the domain to the map. These have to be placed
4046 * in the same order as those in the context and they have to be placed
4047 * after any other integer divisions that the map may have.
4048 * This function performs the required reordering.
4050 static struct isl_basic_map *align_context_divs(struct isl_basic_map *bmap,
4051 struct isl_basic_set *dom)
4053 int i;
4054 int common = 0;
4055 int other;
4057 for (i = 0; i < dom->n_div; ++i)
4058 if (find_context_div(bmap, dom, i) != -1)
4059 common++;
4060 other = bmap->n_div - common;
4061 if (dom->n_div - common > 0) {
4062 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
4063 dom->n_div - common, 0, 0);
4064 if (!bmap)
4065 return NULL;
4067 for (i = 0; i < dom->n_div; ++i) {
4068 int pos = find_context_div(bmap, dom, i);
4069 if (pos < 0) {
4070 pos = isl_basic_map_alloc_div(bmap);
4071 if (pos < 0)
4072 goto error;
4073 isl_int_set_si(bmap->div[pos][0], 0);
4075 if (pos != other + i)
4076 isl_basic_map_swap_div(bmap, pos, other + i);
4078 return bmap;
4079 error:
4080 isl_basic_map_free(bmap);
4081 return NULL;
4084 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4085 * some obvious symmetries.
4087 * We make sure the divs in the domain are properly ordered,
4088 * because they will be added one by one in the given order
4089 * during the construction of the solution map.
4091 static struct isl_sol *basic_map_partial_lexopt_base(
4092 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4093 __isl_give isl_set **empty, int max,
4094 struct isl_sol *(*init)(__isl_keep isl_basic_map *bmap,
4095 __isl_take isl_basic_set *dom, int track_empty, int max))
4097 struct isl_tab *tab;
4098 struct isl_sol *sol = NULL;
4099 struct isl_context *context;
4101 if (dom->n_div) {
4102 dom = isl_basic_set_order_divs(dom);
4103 bmap = align_context_divs(bmap, dom);
4105 sol = init(bmap, dom, !!empty, max);
4106 if (!sol)
4107 goto error;
4109 context = sol->context;
4110 if (isl_basic_set_plain_is_empty(context->op->peek_basic_set(context)))
4111 /* nothing */;
4112 else if (isl_basic_map_plain_is_empty(bmap)) {
4113 if (sol->add_empty)
4114 sol->add_empty(sol,
4115 isl_basic_set_copy(context->op->peek_basic_set(context)));
4116 } else {
4117 tab = tab_for_lexmin(bmap,
4118 context->op->peek_basic_set(context), 1, max);
4119 tab = context->op->detect_nonnegative_parameters(context, tab);
4120 find_solutions_main(sol, tab);
4122 if (sol->error)
4123 goto error;
4125 isl_basic_map_free(bmap);
4126 return sol;
4127 error:
4128 sol_free(sol);
4129 isl_basic_map_free(bmap);
4130 return NULL;
4133 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4134 * some obvious symmetries.
4136 * We call basic_map_partial_lexopt_base and extract the results.
4138 static __isl_give isl_map *basic_map_partial_lexopt_base_map(
4139 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4140 __isl_give isl_set **empty, int max)
4142 isl_map *result = NULL;
4143 struct isl_sol *sol;
4144 struct isl_sol_map *sol_map;
4146 sol = basic_map_partial_lexopt_base(bmap, dom, empty, max,
4147 &sol_map_init);
4148 if (!sol)
4149 return NULL;
4150 sol_map = (struct isl_sol_map *) sol;
4152 result = isl_map_copy(sol_map->map);
4153 if (empty)
4154 *empty = isl_set_copy(sol_map->empty);
4155 sol_free(&sol_map->sol);
4156 return result;
4159 /* Structure used during detection of parallel constraints.
4160 * n_in: number of "input" variables: isl_dim_param + isl_dim_in
4161 * n_out: number of "output" variables: isl_dim_out + isl_dim_div
4162 * val: the coefficients of the output variables
4164 struct isl_constraint_equal_info {
4165 isl_basic_map *bmap;
4166 unsigned n_in;
4167 unsigned n_out;
4168 isl_int *val;
4171 /* Check whether the coefficients of the output variables
4172 * of the constraint in "entry" are equal to info->val.
4174 static int constraint_equal(const void *entry, const void *val)
4176 isl_int **row = (isl_int **)entry;
4177 const struct isl_constraint_equal_info *info = val;
4179 return isl_seq_eq((*row) + 1 + info->n_in, info->val, info->n_out);
4182 /* Check whether "bmap" has a pair of constraints that have
4183 * the same coefficients for the output variables.
4184 * Note that the coefficients of the existentially quantified
4185 * variables need to be zero since the existentially quantified
4186 * of the result are usually not the same as those of the input.
4187 * the isl_dim_out and isl_dim_div dimensions.
4188 * If so, return 1 and return the row indices of the two constraints
4189 * in *first and *second.
4191 static int parallel_constraints(__isl_keep isl_basic_map *bmap,
4192 int *first, int *second)
4194 int i;
4195 isl_ctx *ctx;
4196 struct isl_hash_table *table = NULL;
4197 struct isl_hash_table_entry *entry;
4198 struct isl_constraint_equal_info info;
4199 unsigned n_out;
4200 unsigned n_div;
4202 ctx = isl_basic_map_get_ctx(bmap);
4203 table = isl_hash_table_alloc(ctx, bmap->n_ineq);
4204 if (!table)
4205 goto error;
4207 info.n_in = isl_basic_map_dim(bmap, isl_dim_param) +
4208 isl_basic_map_dim(bmap, isl_dim_in);
4209 info.bmap = bmap;
4210 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4211 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4212 info.n_out = n_out + n_div;
4213 for (i = 0; i < bmap->n_ineq; ++i) {
4214 uint32_t hash;
4216 info.val = bmap->ineq[i] + 1 + info.n_in;
4217 if (isl_seq_first_non_zero(info.val, n_out) < 0)
4218 continue;
4219 if (isl_seq_first_non_zero(info.val + n_out, n_div) >= 0)
4220 continue;
4221 hash = isl_seq_get_hash(info.val, info.n_out);
4222 entry = isl_hash_table_find(ctx, table, hash,
4223 constraint_equal, &info, 1);
4224 if (!entry)
4225 goto error;
4226 if (entry->data)
4227 break;
4228 entry->data = &bmap->ineq[i];
4231 if (i < bmap->n_ineq) {
4232 *first = ((isl_int **)entry->data) - bmap->ineq;
4233 *second = i;
4236 isl_hash_table_free(ctx, table);
4238 return i < bmap->n_ineq;
4239 error:
4240 isl_hash_table_free(ctx, table);
4241 return -1;
4244 /* Given a set of upper bounds in "var", add constraints to "bset"
4245 * that make the i-th bound smallest.
4247 * In particular, if there are n bounds b_i, then add the constraints
4249 * b_i <= b_j for j > i
4250 * b_i < b_j for j < i
4252 static __isl_give isl_basic_set *select_minimum(__isl_take isl_basic_set *bset,
4253 __isl_keep isl_mat *var, int i)
4255 isl_ctx *ctx;
4256 int j, k;
4258 ctx = isl_mat_get_ctx(var);
4260 for (j = 0; j < var->n_row; ++j) {
4261 if (j == i)
4262 continue;
4263 k = isl_basic_set_alloc_inequality(bset);
4264 if (k < 0)
4265 goto error;
4266 isl_seq_combine(bset->ineq[k], ctx->one, var->row[j],
4267 ctx->negone, var->row[i], var->n_col);
4268 isl_int_set_si(bset->ineq[k][var->n_col], 0);
4269 if (j < i)
4270 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
4273 bset = isl_basic_set_finalize(bset);
4275 return bset;
4276 error:
4277 isl_basic_set_free(bset);
4278 return NULL;
4281 /* Given a set of upper bounds on the last "input" variable m,
4282 * construct a set that assigns the minimal upper bound to m, i.e.,
4283 * construct a set that divides the space into cells where one
4284 * of the upper bounds is smaller than all the others and assign
4285 * this upper bound to m.
4287 * In particular, if there are n bounds b_i, then the result
4288 * consists of n basic sets, each one of the form
4290 * m = b_i
4291 * b_i <= b_j for j > i
4292 * b_i < b_j for j < i
4294 static __isl_give isl_set *set_minimum(__isl_take isl_space *dim,
4295 __isl_take isl_mat *var)
4297 int i, k;
4298 isl_basic_set *bset = NULL;
4299 isl_set *set = NULL;
4301 if (!dim || !var)
4302 goto error;
4304 set = isl_set_alloc_space(isl_space_copy(dim),
4305 var->n_row, ISL_SET_DISJOINT);
4307 for (i = 0; i < var->n_row; ++i) {
4308 bset = isl_basic_set_alloc_space(isl_space_copy(dim), 0,
4309 1, var->n_row - 1);
4310 k = isl_basic_set_alloc_equality(bset);
4311 if (k < 0)
4312 goto error;
4313 isl_seq_cpy(bset->eq[k], var->row[i], var->n_col);
4314 isl_int_set_si(bset->eq[k][var->n_col], -1);
4315 bset = select_minimum(bset, var, i);
4316 set = isl_set_add_basic_set(set, bset);
4319 isl_space_free(dim);
4320 isl_mat_free(var);
4321 return set;
4322 error:
4323 isl_basic_set_free(bset);
4324 isl_set_free(set);
4325 isl_space_free(dim);
4326 isl_mat_free(var);
4327 return NULL;
4330 /* Given that the last input variable of "bmap" represents the minimum
4331 * of the bounds in "cst", check whether we need to split the domain
4332 * based on which bound attains the minimum.
4334 * A split is needed when the minimum appears in an integer division
4335 * or in an equality. Otherwise, it is only needed if it appears in
4336 * an upper bound that is different from the upper bounds on which it
4337 * is defined.
4339 static int need_split_basic_map(__isl_keep isl_basic_map *bmap,
4340 __isl_keep isl_mat *cst)
4342 int i, j;
4343 unsigned total;
4344 unsigned pos;
4346 pos = cst->n_col - 1;
4347 total = isl_basic_map_dim(bmap, isl_dim_all);
4349 for (i = 0; i < bmap->n_div; ++i)
4350 if (!isl_int_is_zero(bmap->div[i][2 + pos]))
4351 return 1;
4353 for (i = 0; i < bmap->n_eq; ++i)
4354 if (!isl_int_is_zero(bmap->eq[i][1 + pos]))
4355 return 1;
4357 for (i = 0; i < bmap->n_ineq; ++i) {
4358 if (isl_int_is_nonneg(bmap->ineq[i][1 + pos]))
4359 continue;
4360 if (!isl_int_is_negone(bmap->ineq[i][1 + pos]))
4361 return 1;
4362 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + pos + 1,
4363 total - pos - 1) >= 0)
4364 return 1;
4366 for (j = 0; j < cst->n_row; ++j)
4367 if (isl_seq_eq(bmap->ineq[i], cst->row[j], cst->n_col))
4368 break;
4369 if (j >= cst->n_row)
4370 return 1;
4373 return 0;
4376 /* Given that the last set variable of "bset" represents the minimum
4377 * of the bounds in "cst", check whether we need to split the domain
4378 * based on which bound attains the minimum.
4380 * We simply call need_split_basic_map here. This is safe because
4381 * the position of the minimum is computed from "cst" and not
4382 * from "bmap".
4384 static int need_split_basic_set(__isl_keep isl_basic_set *bset,
4385 __isl_keep isl_mat *cst)
4387 return need_split_basic_map((isl_basic_map *)bset, cst);
4390 /* Given that the last set variable of "set" represents the minimum
4391 * of the bounds in "cst", check whether we need to split the domain
4392 * based on which bound attains the minimum.
4394 static int need_split_set(__isl_keep isl_set *set, __isl_keep isl_mat *cst)
4396 int i;
4398 for (i = 0; i < set->n; ++i)
4399 if (need_split_basic_set(set->p[i], cst))
4400 return 1;
4402 return 0;
4405 /* Given a set of which the last set variable is the minimum
4406 * of the bounds in "cst", split each basic set in the set
4407 * in pieces where one of the bounds is (strictly) smaller than the others.
4408 * This subdivision is given in "min_expr".
4409 * The variable is subsequently projected out.
4411 * We only do the split when it is needed.
4412 * For example if the last input variable m = min(a,b) and the only
4413 * constraints in the given basic set are lower bounds on m,
4414 * i.e., l <= m = min(a,b), then we can simply project out m
4415 * to obtain l <= a and l <= b, without having to split on whether
4416 * m is equal to a or b.
4418 static __isl_give isl_set *split(__isl_take isl_set *empty,
4419 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
4421 int n_in;
4422 int i;
4423 isl_space *dim;
4424 isl_set *res;
4426 if (!empty || !min_expr || !cst)
4427 goto error;
4429 n_in = isl_set_dim(empty, isl_dim_set);
4430 dim = isl_set_get_space(empty);
4431 dim = isl_space_drop_dims(dim, isl_dim_set, n_in - 1, 1);
4432 res = isl_set_empty(dim);
4434 for (i = 0; i < empty->n; ++i) {
4435 isl_set *set;
4437 set = isl_set_from_basic_set(isl_basic_set_copy(empty->p[i]));
4438 if (need_split_basic_set(empty->p[i], cst))
4439 set = isl_set_intersect(set, isl_set_copy(min_expr));
4440 set = isl_set_remove_dims(set, isl_dim_set, n_in - 1, 1);
4442 res = isl_set_union_disjoint(res, set);
4445 isl_set_free(empty);
4446 isl_set_free(min_expr);
4447 isl_mat_free(cst);
4448 return res;
4449 error:
4450 isl_set_free(empty);
4451 isl_set_free(min_expr);
4452 isl_mat_free(cst);
4453 return NULL;
4456 /* Given a map of which the last input variable is the minimum
4457 * of the bounds in "cst", split each basic set in the set
4458 * in pieces where one of the bounds is (strictly) smaller than the others.
4459 * This subdivision is given in "min_expr".
4460 * The variable is subsequently projected out.
4462 * The implementation is essentially the same as that of "split".
4464 static __isl_give isl_map *split_domain(__isl_take isl_map *opt,
4465 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
4467 int n_in;
4468 int i;
4469 isl_space *dim;
4470 isl_map *res;
4472 if (!opt || !min_expr || !cst)
4473 goto error;
4475 n_in = isl_map_dim(opt, isl_dim_in);
4476 dim = isl_map_get_space(opt);
4477 dim = isl_space_drop_dims(dim, isl_dim_in, n_in - 1, 1);
4478 res = isl_map_empty(dim);
4480 for (i = 0; i < opt->n; ++i) {
4481 isl_map *map;
4483 map = isl_map_from_basic_map(isl_basic_map_copy(opt->p[i]));
4484 if (need_split_basic_map(opt->p[i], cst))
4485 map = isl_map_intersect_domain(map,
4486 isl_set_copy(min_expr));
4487 map = isl_map_remove_dims(map, isl_dim_in, n_in - 1, 1);
4489 res = isl_map_union_disjoint(res, map);
4492 isl_map_free(opt);
4493 isl_set_free(min_expr);
4494 isl_mat_free(cst);
4495 return res;
4496 error:
4497 isl_map_free(opt);
4498 isl_set_free(min_expr);
4499 isl_mat_free(cst);
4500 return NULL;
4503 static __isl_give isl_map *basic_map_partial_lexopt(
4504 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4505 __isl_give isl_set **empty, int max);
4507 union isl_lex_res {
4508 void *p;
4509 isl_map *map;
4510 isl_pw_multi_aff *pma;
4513 /* This function is called from basic_map_partial_lexopt_symm.
4514 * The last variable of "bmap" and "dom" corresponds to the minimum
4515 * of the bounds in "cst". "map_space" is the space of the original
4516 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
4517 * is the space of the original domain.
4519 * We recursively call basic_map_partial_lexopt and then plug in
4520 * the definition of the minimum in the result.
4522 static __isl_give union isl_lex_res basic_map_partial_lexopt_symm_map_core(
4523 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4524 __isl_give isl_set **empty, int max, __isl_take isl_mat *cst,
4525 __isl_take isl_space *map_space, __isl_take isl_space *set_space)
4527 isl_map *opt;
4528 isl_set *min_expr;
4529 union isl_lex_res res;
4531 min_expr = set_minimum(isl_basic_set_get_space(dom), isl_mat_copy(cst));
4533 opt = basic_map_partial_lexopt(bmap, dom, empty, max);
4535 if (empty) {
4536 *empty = split(*empty,
4537 isl_set_copy(min_expr), isl_mat_copy(cst));
4538 *empty = isl_set_reset_space(*empty, set_space);
4541 opt = split_domain(opt, min_expr, cst);
4542 opt = isl_map_reset_space(opt, map_space);
4544 res.map = opt;
4545 return res;
4548 /* Given a basic map with at least two parallel constraints (as found
4549 * by the function parallel_constraints), first look for more constraints
4550 * parallel to the two constraint and replace the found list of parallel
4551 * constraints by a single constraint with as "input" part the minimum
4552 * of the input parts of the list of constraints. Then, recursively call
4553 * basic_map_partial_lexopt (possibly finding more parallel constraints)
4554 * and plug in the definition of the minimum in the result.
4556 * More specifically, given a set of constraints
4558 * a x + b_i(p) >= 0
4560 * Replace this set by a single constraint
4562 * a x + u >= 0
4564 * with u a new parameter with constraints
4566 * u <= b_i(p)
4568 * Any solution to the new system is also a solution for the original system
4569 * since
4571 * a x >= -u >= -b_i(p)
4573 * Moreover, m = min_i(b_i(p)) satisfies the constraints on u and can
4574 * therefore be plugged into the solution.
4576 static union isl_lex_res basic_map_partial_lexopt_symm(
4577 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4578 __isl_give isl_set **empty, int max, int first, int second,
4579 __isl_give union isl_lex_res (*core)(__isl_take isl_basic_map *bmap,
4580 __isl_take isl_basic_set *dom,
4581 __isl_give isl_set **empty,
4582 int max, __isl_take isl_mat *cst,
4583 __isl_take isl_space *map_space,
4584 __isl_take isl_space *set_space))
4586 int i, n, k;
4587 int *list = NULL;
4588 unsigned n_in, n_out, n_div;
4589 isl_ctx *ctx;
4590 isl_vec *var = NULL;
4591 isl_mat *cst = NULL;
4592 isl_space *map_space, *set_space;
4593 union isl_lex_res res;
4595 map_space = isl_basic_map_get_space(bmap);
4596 set_space = empty ? isl_basic_set_get_space(dom) : NULL;
4598 n_in = isl_basic_map_dim(bmap, isl_dim_param) +
4599 isl_basic_map_dim(bmap, isl_dim_in);
4600 n_out = isl_basic_map_dim(bmap, isl_dim_all) - n_in;
4602 ctx = isl_basic_map_get_ctx(bmap);
4603 list = isl_alloc_array(ctx, int, bmap->n_ineq);
4604 var = isl_vec_alloc(ctx, n_out);
4605 if ((bmap->n_ineq && !list) || (n_out && !var))
4606 goto error;
4608 list[0] = first;
4609 list[1] = second;
4610 isl_seq_cpy(var->el, bmap->ineq[first] + 1 + n_in, n_out);
4611 for (i = second + 1, n = 2; i < bmap->n_ineq; ++i) {
4612 if (isl_seq_eq(var->el, bmap->ineq[i] + 1 + n_in, n_out))
4613 list[n++] = i;
4616 cst = isl_mat_alloc(ctx, n, 1 + n_in);
4617 if (!cst)
4618 goto error;
4620 for (i = 0; i < n; ++i)
4621 isl_seq_cpy(cst->row[i], bmap->ineq[list[i]], 1 + n_in);
4623 bmap = isl_basic_map_cow(bmap);
4624 if (!bmap)
4625 goto error;
4626 for (i = n - 1; i >= 0; --i)
4627 if (isl_basic_map_drop_inequality(bmap, list[i]) < 0)
4628 goto error;
4630 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, 1);
4631 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
4632 k = isl_basic_map_alloc_inequality(bmap);
4633 if (k < 0)
4634 goto error;
4635 isl_seq_clr(bmap->ineq[k], 1 + n_in);
4636 isl_int_set_si(bmap->ineq[k][1 + n_in], 1);
4637 isl_seq_cpy(bmap->ineq[k] + 1 + n_in + 1, var->el, n_out);
4638 bmap = isl_basic_map_finalize(bmap);
4640 n_div = isl_basic_set_dim(dom, isl_dim_div);
4641 dom = isl_basic_set_add_dims(dom, isl_dim_set, 1);
4642 dom = isl_basic_set_extend_constraints(dom, 0, n);
4643 for (i = 0; i < n; ++i) {
4644 k = isl_basic_set_alloc_inequality(dom);
4645 if (k < 0)
4646 goto error;
4647 isl_seq_cpy(dom->ineq[k], cst->row[i], 1 + n_in);
4648 isl_int_set_si(dom->ineq[k][1 + n_in], -1);
4649 isl_seq_clr(dom->ineq[k] + 1 + n_in + 1, n_div);
4652 isl_vec_free(var);
4653 free(list);
4655 return core(bmap, dom, empty, max, cst, map_space, set_space);
4656 error:
4657 isl_space_free(map_space);
4658 isl_space_free(set_space);
4659 isl_mat_free(cst);
4660 isl_vec_free(var);
4661 free(list);
4662 isl_basic_set_free(dom);
4663 isl_basic_map_free(bmap);
4664 res.p = NULL;
4665 return res;
4668 static __isl_give isl_map *basic_map_partial_lexopt_symm_map(
4669 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4670 __isl_give isl_set **empty, int max, int first, int second)
4672 return basic_map_partial_lexopt_symm(bmap, dom, empty, max,
4673 first, second, &basic_map_partial_lexopt_symm_map_core).map;
4676 /* Recursive part of isl_tab_basic_map_partial_lexopt, after detecting
4677 * equalities and removing redundant constraints.
4679 * We first check if there are any parallel constraints (left).
4680 * If not, we are in the base case.
4681 * If there are parallel constraints, we replace them by a single
4682 * constraint in basic_map_partial_lexopt_symm and then call
4683 * this function recursively to look for more parallel constraints.
4685 static __isl_give isl_map *basic_map_partial_lexopt(
4686 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4687 __isl_give isl_set **empty, int max)
4689 int par = 0;
4690 int first, second;
4692 if (!bmap)
4693 goto error;
4695 if (bmap->ctx->opt->pip_symmetry)
4696 par = parallel_constraints(bmap, &first, &second);
4697 if (par < 0)
4698 goto error;
4699 if (!par)
4700 return basic_map_partial_lexopt_base_map(bmap, dom, empty, max);
4702 return basic_map_partial_lexopt_symm_map(bmap, dom, empty, max,
4703 first, second);
4704 error:
4705 isl_basic_set_free(dom);
4706 isl_basic_map_free(bmap);
4707 return NULL;
4710 /* Compute the lexicographic minimum (or maximum if "max" is set)
4711 * of "bmap" over the domain "dom" and return the result as a map.
4712 * If "empty" is not NULL, then *empty is assigned a set that
4713 * contains those parts of the domain where there is no solution.
4714 * If "bmap" is marked as rational (ISL_BASIC_MAP_RATIONAL),
4715 * then we compute the rational optimum. Otherwise, we compute
4716 * the integral optimum.
4718 * We perform some preprocessing. As the PILP solver does not
4719 * handle implicit equalities very well, we first make sure all
4720 * the equalities are explicitly available.
4722 * We also add context constraints to the basic map and remove
4723 * redundant constraints. This is only needed because of the
4724 * way we handle simple symmetries. In particular, we currently look
4725 * for symmetries on the constraints, before we set up the main tableau.
4726 * It is then no good to look for symmetries on possibly redundant constraints.
4728 struct isl_map *isl_tab_basic_map_partial_lexopt(
4729 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4730 struct isl_set **empty, int max)
4732 if (empty)
4733 *empty = NULL;
4734 if (!bmap || !dom)
4735 goto error;
4737 isl_assert(bmap->ctx,
4738 isl_basic_map_compatible_domain(bmap, dom), goto error);
4740 if (isl_basic_set_dim(dom, isl_dim_all) == 0)
4741 return basic_map_partial_lexopt(bmap, dom, empty, max);
4743 bmap = isl_basic_map_intersect_domain(bmap, isl_basic_set_copy(dom));
4744 bmap = isl_basic_map_detect_equalities(bmap);
4745 bmap = isl_basic_map_remove_redundancies(bmap);
4747 return basic_map_partial_lexopt(bmap, dom, empty, max);
4748 error:
4749 isl_basic_set_free(dom);
4750 isl_basic_map_free(bmap);
4751 return NULL;
4754 struct isl_sol_for {
4755 struct isl_sol sol;
4756 int (*fn)(__isl_take isl_basic_set *dom,
4757 __isl_take isl_aff_list *list, void *user);
4758 void *user;
4761 static void sol_for_free(struct isl_sol_for *sol_for)
4763 if (!sol_for)
4764 return;
4765 if (sol_for->sol.context)
4766 sol_for->sol.context->op->free(sol_for->sol.context);
4767 free(sol_for);
4770 static void sol_for_free_wrap(struct isl_sol *sol)
4772 sol_for_free((struct isl_sol_for *)sol);
4775 /* Add the solution identified by the tableau and the context tableau.
4777 * See documentation of sol_add for more details.
4779 * Instead of constructing a basic map, this function calls a user
4780 * defined function with the current context as a basic set and
4781 * a list of affine expressions representing the relation between
4782 * the input and output. The space over which the affine expressions
4783 * are defined is the same as that of the domain. The number of
4784 * affine expressions in the list is equal to the number of output variables.
4786 static void sol_for_add(struct isl_sol_for *sol,
4787 struct isl_basic_set *dom, struct isl_mat *M)
4789 int i;
4790 isl_ctx *ctx;
4791 isl_local_space *ls;
4792 isl_aff *aff;
4793 isl_aff_list *list;
4795 if (sol->sol.error || !dom || !M)
4796 goto error;
4798 ctx = isl_basic_set_get_ctx(dom);
4799 ls = isl_basic_set_get_local_space(dom);
4800 list = isl_aff_list_alloc(ctx, M->n_row - 1);
4801 for (i = 1; i < M->n_row; ++i) {
4802 aff = isl_aff_alloc(isl_local_space_copy(ls));
4803 if (aff) {
4804 isl_int_set(aff->v->el[0], M->row[0][0]);
4805 isl_seq_cpy(aff->v->el + 1, M->row[i], M->n_col);
4807 aff = isl_aff_normalize(aff);
4808 list = isl_aff_list_add(list, aff);
4810 isl_local_space_free(ls);
4812 dom = isl_basic_set_finalize(dom);
4814 if (sol->fn(isl_basic_set_copy(dom), list, sol->user) < 0)
4815 goto error;
4817 isl_basic_set_free(dom);
4818 isl_mat_free(M);
4819 return;
4820 error:
4821 isl_basic_set_free(dom);
4822 isl_mat_free(M);
4823 sol->sol.error = 1;
4826 static void sol_for_add_wrap(struct isl_sol *sol,
4827 struct isl_basic_set *dom, struct isl_mat *M)
4829 sol_for_add((struct isl_sol_for *)sol, dom, M);
4832 static struct isl_sol_for *sol_for_init(struct isl_basic_map *bmap, int max,
4833 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4834 void *user),
4835 void *user)
4837 struct isl_sol_for *sol_for = NULL;
4838 isl_space *dom_dim;
4839 struct isl_basic_set *dom = NULL;
4841 sol_for = isl_calloc_type(bmap->ctx, struct isl_sol_for);
4842 if (!sol_for)
4843 goto error;
4845 dom_dim = isl_space_domain(isl_space_copy(bmap->dim));
4846 dom = isl_basic_set_universe(dom_dim);
4848 sol_for->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
4849 sol_for->sol.dec_level.callback.run = &sol_dec_level_wrap;
4850 sol_for->sol.dec_level.sol = &sol_for->sol;
4851 sol_for->fn = fn;
4852 sol_for->user = user;
4853 sol_for->sol.max = max;
4854 sol_for->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
4855 sol_for->sol.add = &sol_for_add_wrap;
4856 sol_for->sol.add_empty = NULL;
4857 sol_for->sol.free = &sol_for_free_wrap;
4859 sol_for->sol.context = isl_context_alloc(dom);
4860 if (!sol_for->sol.context)
4861 goto error;
4863 isl_basic_set_free(dom);
4864 return sol_for;
4865 error:
4866 isl_basic_set_free(dom);
4867 sol_for_free(sol_for);
4868 return NULL;
4871 static void sol_for_find_solutions(struct isl_sol_for *sol_for,
4872 struct isl_tab *tab)
4874 find_solutions_main(&sol_for->sol, tab);
4877 int isl_basic_map_foreach_lexopt(__isl_keep isl_basic_map *bmap, int max,
4878 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4879 void *user),
4880 void *user)
4882 struct isl_sol_for *sol_for = NULL;
4884 bmap = isl_basic_map_copy(bmap);
4885 bmap = isl_basic_map_detect_equalities(bmap);
4886 if (!bmap)
4887 return -1;
4889 sol_for = sol_for_init(bmap, max, fn, user);
4890 if (!sol_for)
4891 goto error;
4893 if (isl_basic_map_plain_is_empty(bmap))
4894 /* nothing */;
4895 else {
4896 struct isl_tab *tab;
4897 struct isl_context *context = sol_for->sol.context;
4898 tab = tab_for_lexmin(bmap,
4899 context->op->peek_basic_set(context), 1, max);
4900 tab = context->op->detect_nonnegative_parameters(context, tab);
4901 sol_for_find_solutions(sol_for, tab);
4902 if (sol_for->sol.error)
4903 goto error;
4906 sol_free(&sol_for->sol);
4907 isl_basic_map_free(bmap);
4908 return 0;
4909 error:
4910 sol_free(&sol_for->sol);
4911 isl_basic_map_free(bmap);
4912 return -1;
4915 int isl_basic_set_foreach_lexopt(__isl_keep isl_basic_set *bset, int max,
4916 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4917 void *user),
4918 void *user)
4920 return isl_basic_map_foreach_lexopt(bset, max, fn, user);
4923 /* Check if the given sequence of len variables starting at pos
4924 * represents a trivial (i.e., zero) solution.
4925 * The variables are assumed to be non-negative and to come in pairs,
4926 * with each pair representing a variable of unrestricted sign.
4927 * The solution is trivial if each such pair in the sequence consists
4928 * of two identical values, meaning that the variable being represented
4929 * has value zero.
4931 static int region_is_trivial(struct isl_tab *tab, int pos, int len)
4933 int i;
4935 if (len == 0)
4936 return 0;
4938 for (i = 0; i < len; i += 2) {
4939 int neg_row;
4940 int pos_row;
4942 neg_row = tab->var[pos + i].is_row ?
4943 tab->var[pos + i].index : -1;
4944 pos_row = tab->var[pos + i + 1].is_row ?
4945 tab->var[pos + i + 1].index : -1;
4947 if ((neg_row < 0 ||
4948 isl_int_is_zero(tab->mat->row[neg_row][1])) &&
4949 (pos_row < 0 ||
4950 isl_int_is_zero(tab->mat->row[pos_row][1])))
4951 continue;
4953 if (neg_row < 0 || pos_row < 0)
4954 return 0;
4955 if (isl_int_ne(tab->mat->row[neg_row][1],
4956 tab->mat->row[pos_row][1]))
4957 return 0;
4960 return 1;
4963 /* Return the index of the first trivial region or -1 if all regions
4964 * are non-trivial.
4966 static int first_trivial_region(struct isl_tab *tab,
4967 int n_region, struct isl_region *region)
4969 int i;
4971 for (i = 0; i < n_region; ++i) {
4972 if (region_is_trivial(tab, region[i].pos, region[i].len))
4973 return i;
4976 return -1;
4979 /* Check if the solution is optimal, i.e., whether the first
4980 * n_op entries are zero.
4982 static int is_optimal(__isl_keep isl_vec *sol, int n_op)
4984 int i;
4986 for (i = 0; i < n_op; ++i)
4987 if (!isl_int_is_zero(sol->el[1 + i]))
4988 return 0;
4989 return 1;
4992 /* Add constraints to "tab" that ensure that any solution is significantly
4993 * better that that represented by "sol". That is, find the first
4994 * relevant (within first n_op) non-zero coefficient and force it (along
4995 * with all previous coefficients) to be zero.
4996 * If the solution is already optimal (all relevant coefficients are zero),
4997 * then just mark the table as empty.
4999 static int force_better_solution(struct isl_tab *tab,
5000 __isl_keep isl_vec *sol, int n_op)
5002 int i;
5003 isl_ctx *ctx;
5004 isl_vec *v = NULL;
5006 if (!sol)
5007 return -1;
5009 for (i = 0; i < n_op; ++i)
5010 if (!isl_int_is_zero(sol->el[1 + i]))
5011 break;
5013 if (i == n_op) {
5014 if (isl_tab_mark_empty(tab) < 0)
5015 return -1;
5016 return 0;
5019 ctx = isl_vec_get_ctx(sol);
5020 v = isl_vec_alloc(ctx, 1 + tab->n_var);
5021 if (!v)
5022 return -1;
5024 for (; i >= 0; --i) {
5025 v = isl_vec_clr(v);
5026 isl_int_set_si(v->el[1 + i], -1);
5027 if (add_lexmin_eq(tab, v->el) < 0)
5028 goto error;
5031 isl_vec_free(v);
5032 return 0;
5033 error:
5034 isl_vec_free(v);
5035 return -1;
5038 struct isl_trivial {
5039 int update;
5040 int region;
5041 int side;
5042 struct isl_tab_undo *snap;
5045 /* Return the lexicographically smallest non-trivial solution of the
5046 * given ILP problem.
5048 * All variables are assumed to be non-negative.
5050 * n_op is the number of initial coordinates to optimize.
5051 * That is, once a solution has been found, we will only continue looking
5052 * for solution that result in significantly better values for those
5053 * initial coordinates. That is, we only continue looking for solutions
5054 * that increase the number of initial zeros in this sequence.
5056 * A solution is non-trivial, if it is non-trivial on each of the
5057 * specified regions. Each region represents a sequence of pairs
5058 * of variables. A solution is non-trivial on such a region if
5059 * at least one of these pairs consists of different values, i.e.,
5060 * such that the non-negative variable represented by the pair is non-zero.
5062 * Whenever a conflict is encountered, all constraints involved are
5063 * reported to the caller through a call to "conflict".
5065 * We perform a simple branch-and-bound backtracking search.
5066 * Each level in the search represents initially trivial region that is forced
5067 * to be non-trivial.
5068 * At each level we consider n cases, where n is the length of the region.
5069 * In terms of the n/2 variables of unrestricted signs being encoded by
5070 * the region, we consider the cases
5071 * x_0 >= 1
5072 * x_0 <= -1
5073 * x_0 = 0 and x_1 >= 1
5074 * x_0 = 0 and x_1 <= -1
5075 * x_0 = 0 and x_1 = 0 and x_2 >= 1
5076 * x_0 = 0 and x_1 = 0 and x_2 <= -1
5077 * ...
5078 * The cases are considered in this order, assuming that each pair
5079 * x_i_a x_i_b represents the value x_i_b - x_i_a.
5080 * That is, x_0 >= 1 is enforced by adding the constraint
5081 * x_0_b - x_0_a >= 1
5083 __isl_give isl_vec *isl_tab_basic_set_non_trivial_lexmin(
5084 __isl_take isl_basic_set *bset, int n_op, int n_region,
5085 struct isl_region *region,
5086 int (*conflict)(int con, void *user), void *user)
5088 int i, j;
5089 int r;
5090 isl_ctx *ctx;
5091 isl_vec *v = NULL;
5092 isl_vec *sol = NULL;
5093 struct isl_tab *tab;
5094 struct isl_trivial *triv = NULL;
5095 int level, init;
5097 if (!bset)
5098 return NULL;
5100 ctx = isl_basic_set_get_ctx(bset);
5101 sol = isl_vec_alloc(ctx, 0);
5103 tab = tab_for_lexmin(bset, NULL, 0, 0);
5104 if (!tab)
5105 goto error;
5106 tab->conflict = conflict;
5107 tab->conflict_user = user;
5109 v = isl_vec_alloc(ctx, 1 + tab->n_var);
5110 triv = isl_calloc_array(ctx, struct isl_trivial, n_region);
5111 if (!v || (n_region && !triv))
5112 goto error;
5114 level = 0;
5115 init = 1;
5117 while (level >= 0) {
5118 int side, base;
5120 if (init) {
5121 tab = cut_to_integer_lexmin(tab, CUT_ONE);
5122 if (!tab)
5123 goto error;
5124 if (tab->empty)
5125 goto backtrack;
5126 r = first_trivial_region(tab, n_region, region);
5127 if (r < 0) {
5128 for (i = 0; i < level; ++i)
5129 triv[i].update = 1;
5130 isl_vec_free(sol);
5131 sol = isl_tab_get_sample_value(tab);
5132 if (!sol)
5133 goto error;
5134 if (is_optimal(sol, n_op))
5135 break;
5136 goto backtrack;
5138 if (level >= n_region)
5139 isl_die(ctx, isl_error_internal,
5140 "nesting level too deep", goto error);
5141 if (isl_tab_extend_cons(tab,
5142 2 * region[r].len + 2 * n_op) < 0)
5143 goto error;
5144 triv[level].region = r;
5145 triv[level].side = 0;
5148 r = triv[level].region;
5149 side = triv[level].side;
5150 base = 2 * (side/2);
5152 if (side >= region[r].len) {
5153 backtrack:
5154 level--;
5155 init = 0;
5156 if (level >= 0)
5157 if (isl_tab_rollback(tab, triv[level].snap) < 0)
5158 goto error;
5159 continue;
5162 if (triv[level].update) {
5163 if (force_better_solution(tab, sol, n_op) < 0)
5164 goto error;
5165 triv[level].update = 0;
5168 if (side == base && base >= 2) {
5169 for (j = base - 2; j < base; ++j) {
5170 v = isl_vec_clr(v);
5171 isl_int_set_si(v->el[1 + region[r].pos + j], 1);
5172 if (add_lexmin_eq(tab, v->el) < 0)
5173 goto error;
5177 triv[level].snap = isl_tab_snap(tab);
5178 if (isl_tab_push_basis(tab) < 0)
5179 goto error;
5181 v = isl_vec_clr(v);
5182 isl_int_set_si(v->el[0], -1);
5183 isl_int_set_si(v->el[1 + region[r].pos + side], -1);
5184 isl_int_set_si(v->el[1 + region[r].pos + (side ^ 1)], 1);
5185 tab = add_lexmin_ineq(tab, v->el);
5187 triv[level].side++;
5188 level++;
5189 init = 1;
5192 free(triv);
5193 isl_vec_free(v);
5194 isl_tab_free(tab);
5195 isl_basic_set_free(bset);
5197 return sol;
5198 error:
5199 free(triv);
5200 isl_vec_free(v);
5201 isl_tab_free(tab);
5202 isl_basic_set_free(bset);
5203 isl_vec_free(sol);
5204 return NULL;
5207 /* Return the lexicographically smallest rational point in "bset",
5208 * assuming that all variables are non-negative.
5209 * If "bset" is empty, then return a zero-length vector.
5211 __isl_give isl_vec *isl_tab_basic_set_non_neg_lexmin(
5212 __isl_take isl_basic_set *bset)
5214 struct isl_tab *tab;
5215 isl_ctx *ctx = isl_basic_set_get_ctx(bset);
5216 isl_vec *sol;
5218 if (!bset)
5219 return NULL;
5221 tab = tab_for_lexmin(bset, NULL, 0, 0);
5222 if (!tab)
5223 goto error;
5224 if (tab->empty)
5225 sol = isl_vec_alloc(ctx, 0);
5226 else
5227 sol = isl_tab_get_sample_value(tab);
5228 isl_tab_free(tab);
5229 isl_basic_set_free(bset);
5230 return sol;
5231 error:
5232 isl_tab_free(tab);
5233 isl_basic_set_free(bset);
5234 return NULL;
5237 struct isl_sol_pma {
5238 struct isl_sol sol;
5239 isl_pw_multi_aff *pma;
5240 isl_set *empty;
5243 static void sol_pma_free(struct isl_sol_pma *sol_pma)
5245 if (!sol_pma)
5246 return;
5247 if (sol_pma->sol.context)
5248 sol_pma->sol.context->op->free(sol_pma->sol.context);
5249 isl_pw_multi_aff_free(sol_pma->pma);
5250 isl_set_free(sol_pma->empty);
5251 free(sol_pma);
5254 /* This function is called for parts of the context where there is
5255 * no solution, with "bset" corresponding to the context tableau.
5256 * Simply add the basic set to the set "empty".
5258 static void sol_pma_add_empty(struct isl_sol_pma *sol,
5259 __isl_take isl_basic_set *bset)
5261 if (!bset || !sol->empty)
5262 goto error;
5264 sol->empty = isl_set_grow(sol->empty, 1);
5265 bset = isl_basic_set_simplify(bset);
5266 bset = isl_basic_set_finalize(bset);
5267 sol->empty = isl_set_add_basic_set(sol->empty, bset);
5268 if (!sol->empty)
5269 sol->sol.error = 1;
5270 return;
5271 error:
5272 isl_basic_set_free(bset);
5273 sol->sol.error = 1;
5276 /* Given a basic map "dom" that represents the context and an affine
5277 * matrix "M" that maps the dimensions of the context to the
5278 * output variables, construct an isl_pw_multi_aff with a single
5279 * cell corresponding to "dom" and affine expressions copied from "M".
5281 static void sol_pma_add(struct isl_sol_pma *sol,
5282 __isl_take isl_basic_set *dom, __isl_take isl_mat *M)
5284 int i;
5285 isl_local_space *ls;
5286 isl_aff *aff;
5287 isl_multi_aff *maff;
5288 isl_pw_multi_aff *pma;
5290 maff = isl_multi_aff_alloc(isl_pw_multi_aff_get_space(sol->pma));
5291 ls = isl_basic_set_get_local_space(dom);
5292 for (i = 1; i < M->n_row; ++i) {
5293 aff = isl_aff_alloc(isl_local_space_copy(ls));
5294 if (aff) {
5295 isl_int_set(aff->v->el[0], M->row[0][0]);
5296 isl_seq_cpy(aff->v->el + 1, M->row[i], M->n_col);
5298 aff = isl_aff_normalize(aff);
5299 maff = isl_multi_aff_set_aff(maff, i - 1, aff);
5301 isl_local_space_free(ls);
5302 isl_mat_free(M);
5303 dom = isl_basic_set_simplify(dom);
5304 dom = isl_basic_set_finalize(dom);
5305 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(dom), maff);
5306 sol->pma = isl_pw_multi_aff_add_disjoint(sol->pma, pma);
5307 if (!sol->pma)
5308 sol->sol.error = 1;
5311 static void sol_pma_free_wrap(struct isl_sol *sol)
5313 sol_pma_free((struct isl_sol_pma *)sol);
5316 static void sol_pma_add_empty_wrap(struct isl_sol *sol,
5317 __isl_take isl_basic_set *bset)
5319 sol_pma_add_empty((struct isl_sol_pma *)sol, bset);
5322 static void sol_pma_add_wrap(struct isl_sol *sol,
5323 __isl_take isl_basic_set *dom, __isl_take isl_mat *M)
5325 sol_pma_add((struct isl_sol_pma *)sol, dom, M);
5328 /* Construct an isl_sol_pma structure for accumulating the solution.
5329 * If track_empty is set, then we also keep track of the parts
5330 * of the context where there is no solution.
5331 * If max is set, then we are solving a maximization, rather than
5332 * a minimization problem, which means that the variables in the
5333 * tableau have value "M - x" rather than "M + x".
5335 static struct isl_sol *sol_pma_init(__isl_keep isl_basic_map *bmap,
5336 __isl_take isl_basic_set *dom, int track_empty, int max)
5338 struct isl_sol_pma *sol_pma = NULL;
5340 if (!bmap)
5341 goto error;
5343 sol_pma = isl_calloc_type(bmap->ctx, struct isl_sol_pma);
5344 if (!sol_pma)
5345 goto error;
5347 sol_pma->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
5348 sol_pma->sol.dec_level.callback.run = &sol_dec_level_wrap;
5349 sol_pma->sol.dec_level.sol = &sol_pma->sol;
5350 sol_pma->sol.max = max;
5351 sol_pma->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
5352 sol_pma->sol.add = &sol_pma_add_wrap;
5353 sol_pma->sol.add_empty = track_empty ? &sol_pma_add_empty_wrap : NULL;
5354 sol_pma->sol.free = &sol_pma_free_wrap;
5355 sol_pma->pma = isl_pw_multi_aff_empty(isl_basic_map_get_space(bmap));
5356 if (!sol_pma->pma)
5357 goto error;
5359 sol_pma->sol.context = isl_context_alloc(dom);
5360 if (!sol_pma->sol.context)
5361 goto error;
5363 if (track_empty) {
5364 sol_pma->empty = isl_set_alloc_space(isl_basic_set_get_space(dom),
5365 1, ISL_SET_DISJOINT);
5366 if (!sol_pma->empty)
5367 goto error;
5370 isl_basic_set_free(dom);
5371 return &sol_pma->sol;
5372 error:
5373 isl_basic_set_free(dom);
5374 sol_pma_free(sol_pma);
5375 return NULL;
5378 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
5379 * some obvious symmetries.
5381 * We call basic_map_partial_lexopt_base and extract the results.
5383 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_base_pma(
5384 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5385 __isl_give isl_set **empty, int max)
5387 isl_pw_multi_aff *result = NULL;
5388 struct isl_sol *sol;
5389 struct isl_sol_pma *sol_pma;
5391 sol = basic_map_partial_lexopt_base(bmap, dom, empty, max,
5392 &sol_pma_init);
5393 if (!sol)
5394 return NULL;
5395 sol_pma = (struct isl_sol_pma *) sol;
5397 result = isl_pw_multi_aff_copy(sol_pma->pma);
5398 if (empty)
5399 *empty = isl_set_copy(sol_pma->empty);
5400 sol_free(&sol_pma->sol);
5401 return result;
5404 /* Given that the last input variable of "maff" represents the minimum
5405 * of some bounds, check whether we need to plug in the expression
5406 * of the minimum.
5408 * In particular, check if the last input variable appears in any
5409 * of the expressions in "maff".
5411 static int need_substitution(__isl_keep isl_multi_aff *maff)
5413 int i;
5414 unsigned pos;
5416 pos = isl_multi_aff_dim(maff, isl_dim_in) - 1;
5418 for (i = 0; i < maff->n; ++i)
5419 if (isl_aff_involves_dims(maff->p[i], isl_dim_in, pos, 1))
5420 return 1;
5422 return 0;
5425 /* Given a set of upper bounds on the last "input" variable m,
5426 * construct a piecewise affine expression that selects
5427 * the minimal upper bound to m, i.e.,
5428 * divide the space into cells where one
5429 * of the upper bounds is smaller than all the others and select
5430 * this upper bound on that cell.
5432 * In particular, if there are n bounds b_i, then the result
5433 * consists of n cell, each one of the form
5435 * b_i <= b_j for j > i
5436 * b_i < b_j for j < i
5438 * The affine expression on this cell is
5440 * b_i
5442 static __isl_give isl_pw_aff *set_minimum_pa(__isl_take isl_space *space,
5443 __isl_take isl_mat *var)
5445 int i;
5446 isl_aff *aff = NULL;
5447 isl_basic_set *bset = NULL;
5448 isl_pw_aff *paff = NULL;
5449 isl_space *pw_space;
5450 isl_local_space *ls = NULL;
5452 if (!space || !var)
5453 goto error;
5455 ls = isl_local_space_from_space(isl_space_copy(space));
5456 pw_space = isl_space_copy(space);
5457 pw_space = isl_space_from_domain(pw_space);
5458 pw_space = isl_space_add_dims(pw_space, isl_dim_out, 1);
5459 paff = isl_pw_aff_alloc_size(pw_space, var->n_row);
5461 for (i = 0; i < var->n_row; ++i) {
5462 isl_pw_aff *paff_i;
5464 aff = isl_aff_alloc(isl_local_space_copy(ls));
5465 bset = isl_basic_set_alloc_space(isl_space_copy(space), 0,
5466 0, var->n_row - 1);
5467 if (!aff || !bset)
5468 goto error;
5469 isl_int_set_si(aff->v->el[0], 1);
5470 isl_seq_cpy(aff->v->el + 1, var->row[i], var->n_col);
5471 isl_int_set_si(aff->v->el[1 + var->n_col], 0);
5472 bset = select_minimum(bset, var, i);
5473 paff_i = isl_pw_aff_alloc(isl_set_from_basic_set(bset), aff);
5474 paff = isl_pw_aff_add_disjoint(paff, paff_i);
5477 isl_local_space_free(ls);
5478 isl_space_free(space);
5479 isl_mat_free(var);
5480 return paff;
5481 error:
5482 isl_aff_free(aff);
5483 isl_basic_set_free(bset);
5484 isl_pw_aff_free(paff);
5485 isl_local_space_free(ls);
5486 isl_space_free(space);
5487 isl_mat_free(var);
5488 return NULL;
5491 /* Given a piecewise multi-affine expression of which the last input variable
5492 * is the minimum of the bounds in "cst", plug in the value of the minimum.
5493 * This minimum expression is given in "min_expr_pa".
5494 * The set "min_expr" contains the same information, but in the form of a set.
5495 * The variable is subsequently projected out.
5497 * The implementation is similar to those of "split" and "split_domain".
5498 * If the variable appears in a given expression, then minimum expression
5499 * is plugged in. Otherwise, if the variable appears in the constraints
5500 * and a split is required, then the domain is split. Otherwise, no split
5501 * is performed.
5503 static __isl_give isl_pw_multi_aff *split_domain_pma(
5504 __isl_take isl_pw_multi_aff *opt, __isl_take isl_pw_aff *min_expr_pa,
5505 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
5507 int n_in;
5508 int i;
5509 isl_space *space;
5510 isl_pw_multi_aff *res;
5512 if (!opt || !min_expr || !cst)
5513 goto error;
5515 n_in = isl_pw_multi_aff_dim(opt, isl_dim_in);
5516 space = isl_pw_multi_aff_get_space(opt);
5517 space = isl_space_drop_dims(space, isl_dim_in, n_in - 1, 1);
5518 res = isl_pw_multi_aff_empty(space);
5520 for (i = 0; i < opt->n; ++i) {
5521 isl_pw_multi_aff *pma;
5523 pma = isl_pw_multi_aff_alloc(isl_set_copy(opt->p[i].set),
5524 isl_multi_aff_copy(opt->p[i].maff));
5525 if (need_substitution(opt->p[i].maff))
5526 pma = isl_pw_multi_aff_substitute(pma,
5527 isl_dim_in, n_in - 1, min_expr_pa);
5528 else if (need_split_set(opt->p[i].set, cst))
5529 pma = isl_pw_multi_aff_intersect_domain(pma,
5530 isl_set_copy(min_expr));
5531 pma = isl_pw_multi_aff_project_out(pma,
5532 isl_dim_in, n_in - 1, 1);
5534 res = isl_pw_multi_aff_add_disjoint(res, pma);
5537 isl_pw_multi_aff_free(opt);
5538 isl_pw_aff_free(min_expr_pa);
5539 isl_set_free(min_expr);
5540 isl_mat_free(cst);
5541 return res;
5542 error:
5543 isl_pw_multi_aff_free(opt);
5544 isl_pw_aff_free(min_expr_pa);
5545 isl_set_free(min_expr);
5546 isl_mat_free(cst);
5547 return NULL;
5550 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_pma(
5551 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5552 __isl_give isl_set **empty, int max);
5554 /* This function is called from basic_map_partial_lexopt_symm.
5555 * The last variable of "bmap" and "dom" corresponds to the minimum
5556 * of the bounds in "cst". "map_space" is the space of the original
5557 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
5558 * is the space of the original domain.
5560 * We recursively call basic_map_partial_lexopt and then plug in
5561 * the definition of the minimum in the result.
5563 static __isl_give union isl_lex_res basic_map_partial_lexopt_symm_pma_core(
5564 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5565 __isl_give isl_set **empty, int max, __isl_take isl_mat *cst,
5566 __isl_take isl_space *map_space, __isl_take isl_space *set_space)
5568 isl_pw_multi_aff *opt;
5569 isl_pw_aff *min_expr_pa;
5570 isl_set *min_expr;
5571 union isl_lex_res res;
5573 min_expr = set_minimum(isl_basic_set_get_space(dom), isl_mat_copy(cst));
5574 min_expr_pa = set_minimum_pa(isl_basic_set_get_space(dom),
5575 isl_mat_copy(cst));
5577 opt = basic_map_partial_lexopt_pma(bmap, dom, empty, max);
5579 if (empty) {
5580 *empty = split(*empty,
5581 isl_set_copy(min_expr), isl_mat_copy(cst));
5582 *empty = isl_set_reset_space(*empty, set_space);
5585 opt = split_domain_pma(opt, min_expr_pa, min_expr, cst);
5586 opt = isl_pw_multi_aff_reset_space(opt, map_space);
5588 res.pma = opt;
5589 return res;
5592 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_symm_pma(
5593 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5594 __isl_give isl_set **empty, int max, int first, int second)
5596 return basic_map_partial_lexopt_symm(bmap, dom, empty, max,
5597 first, second, &basic_map_partial_lexopt_symm_pma_core).pma;
5600 /* Recursive part of isl_basic_map_partial_lexopt_pw_multi_aff, after detecting
5601 * equalities and removing redundant constraints.
5603 * We first check if there are any parallel constraints (left).
5604 * If not, we are in the base case.
5605 * If there are parallel constraints, we replace them by a single
5606 * constraint in basic_map_partial_lexopt_symm_pma and then call
5607 * this function recursively to look for more parallel constraints.
5609 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_pma(
5610 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5611 __isl_give isl_set **empty, int max)
5613 int par = 0;
5614 int first, second;
5616 if (!bmap)
5617 goto error;
5619 if (bmap->ctx->opt->pip_symmetry)
5620 par = parallel_constraints(bmap, &first, &second);
5621 if (par < 0)
5622 goto error;
5623 if (!par)
5624 return basic_map_partial_lexopt_base_pma(bmap, dom, empty, max);
5626 return basic_map_partial_lexopt_symm_pma(bmap, dom, empty, max,
5627 first, second);
5628 error:
5629 isl_basic_set_free(dom);
5630 isl_basic_map_free(bmap);
5631 return NULL;
5634 /* Compute the lexicographic minimum (or maximum if "max" is set)
5635 * of "bmap" over the domain "dom" and return the result as a piecewise
5636 * multi-affine expression.
5637 * If "empty" is not NULL, then *empty is assigned a set that
5638 * contains those parts of the domain where there is no solution.
5639 * If "bmap" is marked as rational (ISL_BASIC_MAP_RATIONAL),
5640 * then we compute the rational optimum. Otherwise, we compute
5641 * the integral optimum.
5643 * We perform some preprocessing. As the PILP solver does not
5644 * handle implicit equalities very well, we first make sure all
5645 * the equalities are explicitly available.
5647 * We also add context constraints to the basic map and remove
5648 * redundant constraints. This is only needed because of the
5649 * way we handle simple symmetries. In particular, we currently look
5650 * for symmetries on the constraints, before we set up the main tableau.
5651 * It is then no good to look for symmetries on possibly redundant constraints.
5653 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexopt_pw_multi_aff(
5654 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5655 __isl_give isl_set **empty, int max)
5657 if (empty)
5658 *empty = NULL;
5659 if (!bmap || !dom)
5660 goto error;
5662 isl_assert(bmap->ctx,
5663 isl_basic_map_compatible_domain(bmap, dom), goto error);
5665 if (isl_basic_set_dim(dom, isl_dim_all) == 0)
5666 return basic_map_partial_lexopt_pma(bmap, dom, empty, max);
5668 bmap = isl_basic_map_intersect_domain(bmap, isl_basic_set_copy(dom));
5669 bmap = isl_basic_map_detect_equalities(bmap);
5670 bmap = isl_basic_map_remove_redundancies(bmap);
5672 return basic_map_partial_lexopt_pma(bmap, dom, empty, max);
5673 error:
5674 isl_basic_set_free(dom);
5675 isl_basic_map_free(bmap);
5676 return NULL;