isl_basic_map_remove_redundancies: sort constraints
[isl.git] / isl_tab_pip.c
blob2f3d2719478004e60d0094aae7491f16ecb93f92
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 * This function assumes that at least two more rows and at least
1333 * two more elements in the constraint array are available in the tableau.
1335 static struct isl_tab *add_lexmin_valid_eq(struct isl_tab *tab, isl_int *eq)
1337 int i;
1338 int r;
1340 if (!tab)
1341 return NULL;
1342 r = isl_tab_add_row(tab, eq);
1343 if (r < 0)
1344 goto error;
1346 r = tab->con[r].index;
1347 i = last_var_col_or_int_par_col(tab, r);
1348 if (i < 0) {
1349 tab->con[r].is_nonneg = 1;
1350 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1351 goto error;
1352 isl_seq_neg(eq, eq, 1 + tab->n_var);
1353 r = isl_tab_add_row(tab, eq);
1354 if (r < 0)
1355 goto error;
1356 tab->con[r].is_nonneg = 1;
1357 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1358 goto error;
1359 } else {
1360 if (isl_tab_pivot(tab, r, i) < 0)
1361 goto error;
1362 if (isl_tab_kill_col(tab, i) < 0)
1363 goto error;
1364 tab->n_eq++;
1367 return tab;
1368 error:
1369 isl_tab_free(tab);
1370 return NULL;
1373 /* Check if the given row is a pure constant.
1375 static int is_constant(struct isl_tab *tab, int row)
1377 unsigned off = 2 + tab->M;
1379 return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1380 tab->n_col - tab->n_dead) == -1;
1383 /* Add an equality that may or may not be valid to the tableau.
1384 * If the resulting row is a pure constant, then it must be zero.
1385 * Otherwise, the resulting tableau is empty.
1387 * If the row is not a pure constant, then we add two inequalities,
1388 * each time checking that they can be satisfied.
1389 * In the end we try to use one of the two constraints to eliminate
1390 * a column.
1392 * This function assumes that at least two more rows and at least
1393 * two more elements in the constraint array are available in the tableau.
1395 static int add_lexmin_eq(struct isl_tab *tab, isl_int *eq) WARN_UNUSED;
1396 static int add_lexmin_eq(struct isl_tab *tab, isl_int *eq)
1398 int r1, r2;
1399 int row;
1400 struct isl_tab_undo *snap;
1402 if (!tab)
1403 return -1;
1404 snap = isl_tab_snap(tab);
1405 r1 = isl_tab_add_row(tab, eq);
1406 if (r1 < 0)
1407 return -1;
1408 tab->con[r1].is_nonneg = 1;
1409 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r1]) < 0)
1410 return -1;
1412 row = tab->con[r1].index;
1413 if (is_constant(tab, row)) {
1414 if (!isl_int_is_zero(tab->mat->row[row][1]) ||
1415 (tab->M && !isl_int_is_zero(tab->mat->row[row][2]))) {
1416 if (isl_tab_mark_empty(tab) < 0)
1417 return -1;
1418 return 0;
1420 if (isl_tab_rollback(tab, snap) < 0)
1421 return -1;
1422 return 0;
1425 if (restore_lexmin(tab) < 0)
1426 return -1;
1427 if (tab->empty)
1428 return 0;
1430 isl_seq_neg(eq, eq, 1 + tab->n_var);
1432 r2 = isl_tab_add_row(tab, eq);
1433 if (r2 < 0)
1434 return -1;
1435 tab->con[r2].is_nonneg = 1;
1436 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r2]) < 0)
1437 return -1;
1439 if (restore_lexmin(tab) < 0)
1440 return -1;
1441 if (tab->empty)
1442 return 0;
1444 if (!tab->con[r1].is_row) {
1445 if (isl_tab_kill_col(tab, tab->con[r1].index) < 0)
1446 return -1;
1447 } else if (!tab->con[r2].is_row) {
1448 if (isl_tab_kill_col(tab, tab->con[r2].index) < 0)
1449 return -1;
1452 if (tab->bmap) {
1453 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1454 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1455 return -1;
1456 isl_seq_neg(eq, eq, 1 + tab->n_var);
1457 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1458 isl_seq_neg(eq, eq, 1 + tab->n_var);
1459 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1460 return -1;
1461 if (!tab->bmap)
1462 return -1;
1465 return 0;
1468 /* Add an inequality to the tableau, resolving violations using
1469 * restore_lexmin.
1471 * This function assumes that at least one more row and at least
1472 * one more element in the constraint array are available in the tableau.
1474 static struct isl_tab *add_lexmin_ineq(struct isl_tab *tab, isl_int *ineq)
1476 int r;
1478 if (!tab)
1479 return NULL;
1480 if (tab->bmap) {
1481 tab->bmap = isl_basic_map_add_ineq(tab->bmap, ineq);
1482 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1483 goto error;
1484 if (!tab->bmap)
1485 goto error;
1487 r = isl_tab_add_row(tab, ineq);
1488 if (r < 0)
1489 goto error;
1490 tab->con[r].is_nonneg = 1;
1491 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1492 goto error;
1493 if (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;
1499 if (restore_lexmin(tab) < 0)
1500 goto error;
1501 if (!tab->empty && tab->con[r].is_row &&
1502 isl_tab_row_is_redundant(tab, tab->con[r].index))
1503 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1504 goto error;
1505 return tab;
1506 error:
1507 isl_tab_free(tab);
1508 return NULL;
1511 /* Check if the coefficients of the parameters are all integral.
1513 static int integer_parameter(struct isl_tab *tab, int row)
1515 int i;
1516 int col;
1517 unsigned off = 2 + tab->M;
1519 for (i = 0; i < tab->n_param; ++i) {
1520 /* Eliminated parameter */
1521 if (tab->var[i].is_row)
1522 continue;
1523 col = tab->var[i].index;
1524 if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
1525 tab->mat->row[row][0]))
1526 return 0;
1528 for (i = 0; i < tab->n_div; ++i) {
1529 if (tab->var[tab->n_var - tab->n_div + i].is_row)
1530 continue;
1531 col = tab->var[tab->n_var - tab->n_div + i].index;
1532 if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
1533 tab->mat->row[row][0]))
1534 return 0;
1536 return 1;
1539 /* Check if the coefficients of the non-parameter variables are all integral.
1541 static int integer_variable(struct isl_tab *tab, int row)
1543 int i;
1544 unsigned off = 2 + tab->M;
1546 for (i = tab->n_dead; i < tab->n_col; ++i) {
1547 if (tab->col_var[i] >= 0 &&
1548 (tab->col_var[i] < tab->n_param ||
1549 tab->col_var[i] >= tab->n_var - tab->n_div))
1550 continue;
1551 if (!isl_int_is_divisible_by(tab->mat->row[row][off + i],
1552 tab->mat->row[row][0]))
1553 return 0;
1555 return 1;
1558 /* Check if the constant term is integral.
1560 static int integer_constant(struct isl_tab *tab, int row)
1562 return isl_int_is_divisible_by(tab->mat->row[row][1],
1563 tab->mat->row[row][0]);
1566 #define I_CST 1 << 0
1567 #define I_PAR 1 << 1
1568 #define I_VAR 1 << 2
1570 /* Check for next (non-parameter) variable after "var" (first if var == -1)
1571 * that is non-integer and therefore requires a cut and return
1572 * the index of the variable.
1573 * For parametric tableaus, there are three parts in a row,
1574 * the constant, the coefficients of the parameters and the rest.
1575 * For each part, we check whether the coefficients in that part
1576 * are all integral and if so, set the corresponding flag in *f.
1577 * If the constant and the parameter part are integral, then the
1578 * current sample value is integral and no cut is required
1579 * (irrespective of whether the variable part is integral).
1581 static int next_non_integer_var(struct isl_tab *tab, int var, int *f)
1583 var = var < 0 ? tab->n_param : var + 1;
1585 for (; var < tab->n_var - tab->n_div; ++var) {
1586 int flags = 0;
1587 int row;
1588 if (!tab->var[var].is_row)
1589 continue;
1590 row = tab->var[var].index;
1591 if (integer_constant(tab, row))
1592 ISL_FL_SET(flags, I_CST);
1593 if (integer_parameter(tab, row))
1594 ISL_FL_SET(flags, I_PAR);
1595 if (ISL_FL_ISSET(flags, I_CST) && ISL_FL_ISSET(flags, I_PAR))
1596 continue;
1597 if (integer_variable(tab, row))
1598 ISL_FL_SET(flags, I_VAR);
1599 *f = flags;
1600 return var;
1602 return -1;
1605 /* Check for first (non-parameter) variable that is non-integer and
1606 * therefore requires a cut and return the corresponding row.
1607 * For parametric tableaus, there are three parts in a row,
1608 * the constant, the coefficients of the parameters and the rest.
1609 * For each part, we check whether the coefficients in that part
1610 * are all integral and if so, set the corresponding flag in *f.
1611 * If the constant and the parameter part are integral, then the
1612 * current sample value is integral and no cut is required
1613 * (irrespective of whether the variable part is integral).
1615 static int first_non_integer_row(struct isl_tab *tab, int *f)
1617 int var = next_non_integer_var(tab, -1, f);
1619 return var < 0 ? -1 : tab->var[var].index;
1622 /* Add a (non-parametric) cut to cut away the non-integral sample
1623 * value of the given row.
1625 * If the row is given by
1627 * m r = f + \sum_i a_i y_i
1629 * then the cut is
1631 * c = - {-f/m} + \sum_i {a_i/m} y_i >= 0
1633 * The big parameter, if any, is ignored, since it is assumed to be big
1634 * enough to be divisible by any integer.
1635 * If the tableau is actually a parametric tableau, then this function
1636 * is only called when all coefficients of the parameters are integral.
1637 * The cut therefore has zero coefficients for the parameters.
1639 * The current value is known to be negative, so row_sign, if it
1640 * exists, is set accordingly.
1642 * Return the row of the cut or -1.
1644 static int add_cut(struct isl_tab *tab, int row)
1646 int i;
1647 int r;
1648 isl_int *r_row;
1649 unsigned off = 2 + tab->M;
1651 if (isl_tab_extend_cons(tab, 1) < 0)
1652 return -1;
1653 r = isl_tab_allocate_con(tab);
1654 if (r < 0)
1655 return -1;
1657 r_row = tab->mat->row[tab->con[r].index];
1658 isl_int_set(r_row[0], tab->mat->row[row][0]);
1659 isl_int_neg(r_row[1], tab->mat->row[row][1]);
1660 isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
1661 isl_int_neg(r_row[1], r_row[1]);
1662 if (tab->M)
1663 isl_int_set_si(r_row[2], 0);
1664 for (i = 0; i < tab->n_col; ++i)
1665 isl_int_fdiv_r(r_row[off + i],
1666 tab->mat->row[row][off + i], tab->mat->row[row][0]);
1668 tab->con[r].is_nonneg = 1;
1669 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1670 return -1;
1671 if (tab->row_sign)
1672 tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
1674 return tab->con[r].index;
1677 #define CUT_ALL 1
1678 #define CUT_ONE 0
1680 /* Given a non-parametric tableau, add cuts until an integer
1681 * sample point is obtained or until the tableau is determined
1682 * to be integer infeasible.
1683 * As long as there is any non-integer value in the sample point,
1684 * we add appropriate cuts, if possible, for each of these
1685 * non-integer values and then resolve the violated
1686 * cut constraints using restore_lexmin.
1687 * If one of the corresponding rows is equal to an integral
1688 * combination of variables/constraints plus a non-integral constant,
1689 * then there is no way to obtain an integer point and we return
1690 * a tableau that is marked empty.
1691 * The parameter cutting_strategy controls the strategy used when adding cuts
1692 * to remove non-integer points. CUT_ALL adds all possible cuts
1693 * before continuing the search. CUT_ONE adds only one cut at a time.
1695 static struct isl_tab *cut_to_integer_lexmin(struct isl_tab *tab,
1696 int cutting_strategy)
1698 int var;
1699 int row;
1700 int flags;
1702 if (!tab)
1703 return NULL;
1704 if (tab->empty)
1705 return tab;
1707 while ((var = next_non_integer_var(tab, -1, &flags)) != -1) {
1708 do {
1709 if (ISL_FL_ISSET(flags, I_VAR)) {
1710 if (isl_tab_mark_empty(tab) < 0)
1711 goto error;
1712 return tab;
1714 row = tab->var[var].index;
1715 row = add_cut(tab, row);
1716 if (row < 0)
1717 goto error;
1718 if (cutting_strategy == CUT_ONE)
1719 break;
1720 } while ((var = next_non_integer_var(tab, var, &flags)) != -1);
1721 if (restore_lexmin(tab) < 0)
1722 goto error;
1723 if (tab->empty)
1724 break;
1726 return tab;
1727 error:
1728 isl_tab_free(tab);
1729 return NULL;
1732 /* Check whether all the currently active samples also satisfy the inequality
1733 * "ineq" (treated as an equality if eq is set).
1734 * Remove those samples that do not.
1736 static struct isl_tab *check_samples(struct isl_tab *tab, isl_int *ineq, int eq)
1738 int i;
1739 isl_int v;
1741 if (!tab)
1742 return NULL;
1744 isl_assert(tab->mat->ctx, tab->bmap, goto error);
1745 isl_assert(tab->mat->ctx, tab->samples, goto error);
1746 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, goto error);
1748 isl_int_init(v);
1749 for (i = tab->n_outside; i < tab->n_sample; ++i) {
1750 int sgn;
1751 isl_seq_inner_product(ineq, tab->samples->row[i],
1752 1 + tab->n_var, &v);
1753 sgn = isl_int_sgn(v);
1754 if (eq ? (sgn == 0) : (sgn >= 0))
1755 continue;
1756 tab = isl_tab_drop_sample(tab, i);
1757 if (!tab)
1758 break;
1760 isl_int_clear(v);
1762 return tab;
1763 error:
1764 isl_tab_free(tab);
1765 return NULL;
1768 /* Check whether the sample value of the tableau is finite,
1769 * i.e., either the tableau does not use a big parameter, or
1770 * all values of the variables are equal to the big parameter plus
1771 * some constant. This constant is the actual sample value.
1773 static int sample_is_finite(struct isl_tab *tab)
1775 int i;
1777 if (!tab->M)
1778 return 1;
1780 for (i = 0; i < tab->n_var; ++i) {
1781 int row;
1782 if (!tab->var[i].is_row)
1783 return 0;
1784 row = tab->var[i].index;
1785 if (isl_int_ne(tab->mat->row[row][0], tab->mat->row[row][2]))
1786 return 0;
1788 return 1;
1791 /* Check if the context tableau of sol has any integer points.
1792 * Leave tab in empty state if no integer point can be found.
1793 * If an integer point can be found and if moreover it is finite,
1794 * then it is added to the list of sample values.
1796 * This function is only called when none of the currently active sample
1797 * values satisfies the most recently added constraint.
1799 static struct isl_tab *check_integer_feasible(struct isl_tab *tab)
1801 struct isl_tab_undo *snap;
1803 if (!tab)
1804 return NULL;
1806 snap = isl_tab_snap(tab);
1807 if (isl_tab_push_basis(tab) < 0)
1808 goto error;
1810 tab = cut_to_integer_lexmin(tab, CUT_ALL);
1811 if (!tab)
1812 goto error;
1814 if (!tab->empty && sample_is_finite(tab)) {
1815 struct isl_vec *sample;
1817 sample = isl_tab_get_sample_value(tab);
1819 if (isl_tab_add_sample(tab, sample) < 0)
1820 goto error;
1823 if (!tab->empty && isl_tab_rollback(tab, snap) < 0)
1824 goto error;
1826 return tab;
1827 error:
1828 isl_tab_free(tab);
1829 return NULL;
1832 /* Check if any of the currently active sample values satisfies
1833 * the inequality "ineq" (an equality if eq is set).
1835 static int tab_has_valid_sample(struct isl_tab *tab, isl_int *ineq, int eq)
1837 int i;
1838 isl_int v;
1840 if (!tab)
1841 return -1;
1843 isl_assert(tab->mat->ctx, tab->bmap, return -1);
1844 isl_assert(tab->mat->ctx, tab->samples, return -1);
1845 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, return -1);
1847 isl_int_init(v);
1848 for (i = tab->n_outside; i < tab->n_sample; ++i) {
1849 int sgn;
1850 isl_seq_inner_product(ineq, tab->samples->row[i],
1851 1 + tab->n_var, &v);
1852 sgn = isl_int_sgn(v);
1853 if (eq ? (sgn == 0) : (sgn >= 0))
1854 break;
1856 isl_int_clear(v);
1858 return i < tab->n_sample;
1861 /* Add a div specified by "div" to the tableau "tab" and return
1862 * 1 if the div is obviously non-negative.
1864 static int context_tab_add_div(struct isl_tab *tab, struct isl_vec *div,
1865 int (*add_ineq)(void *user, isl_int *), void *user)
1867 int i;
1868 int r;
1869 struct isl_mat *samples;
1870 int nonneg;
1872 r = isl_tab_add_div(tab, div, add_ineq, user);
1873 if (r < 0)
1874 return -1;
1875 nonneg = tab->var[r].is_nonneg;
1876 tab->var[r].frozen = 1;
1878 samples = isl_mat_extend(tab->samples,
1879 tab->n_sample, 1 + tab->n_var);
1880 tab->samples = samples;
1881 if (!samples)
1882 return -1;
1883 for (i = tab->n_outside; i < samples->n_row; ++i) {
1884 isl_seq_inner_product(div->el + 1, samples->row[i],
1885 div->size - 1, &samples->row[i][samples->n_col - 1]);
1886 isl_int_fdiv_q(samples->row[i][samples->n_col - 1],
1887 samples->row[i][samples->n_col - 1], div->el[0]);
1890 return nonneg;
1893 /* Add a div specified by "div" to both the main tableau and
1894 * the context tableau. In case of the main tableau, we only
1895 * need to add an extra div. In the context tableau, we also
1896 * need to express the meaning of the div.
1897 * Return the index of the div or -1 if anything went wrong.
1899 static int add_div(struct isl_tab *tab, struct isl_context *context,
1900 struct isl_vec *div)
1902 int r;
1903 int nonneg;
1905 if ((nonneg = context->op->add_div(context, div)) < 0)
1906 goto error;
1908 if (!context->op->is_ok(context))
1909 goto error;
1911 if (isl_tab_extend_vars(tab, 1) < 0)
1912 goto error;
1913 r = isl_tab_allocate_var(tab);
1914 if (r < 0)
1915 goto error;
1916 if (nonneg)
1917 tab->var[r].is_nonneg = 1;
1918 tab->var[r].frozen = 1;
1919 tab->n_div++;
1921 return tab->n_div - 1;
1922 error:
1923 context->op->invalidate(context);
1924 return -1;
1927 static int find_div(struct isl_tab *tab, isl_int *div, isl_int denom)
1929 int i;
1930 unsigned total = isl_basic_map_total_dim(tab->bmap);
1932 for (i = 0; i < tab->bmap->n_div; ++i) {
1933 if (isl_int_ne(tab->bmap->div[i][0], denom))
1934 continue;
1935 if (!isl_seq_eq(tab->bmap->div[i] + 1, div, 1 + total))
1936 continue;
1937 return i;
1939 return -1;
1942 /* Return the index of a div that corresponds to "div".
1943 * We first check if we already have such a div and if not, we create one.
1945 static int get_div(struct isl_tab *tab, struct isl_context *context,
1946 struct isl_vec *div)
1948 int d;
1949 struct isl_tab *context_tab = context->op->peek_tab(context);
1951 if (!context_tab)
1952 return -1;
1954 d = find_div(context_tab, div->el + 1, div->el[0]);
1955 if (d != -1)
1956 return d;
1958 return add_div(tab, context, div);
1961 /* Add a parametric cut to cut away the non-integral sample value
1962 * of the give row.
1963 * Let a_i be the coefficients of the constant term and the parameters
1964 * and let b_i be the coefficients of the variables or constraints
1965 * in basis of the tableau.
1966 * Let q be the div q = floor(\sum_i {-a_i} y_i).
1968 * The cut is expressed as
1970 * c = \sum_i -{-a_i} y_i + \sum_i {b_i} x_i + q >= 0
1972 * If q did not already exist in the context tableau, then it is added first.
1973 * If q is in a column of the main tableau then the "+ q" can be accomplished
1974 * by setting the corresponding entry to the denominator of the constraint.
1975 * If q happens to be in a row of the main tableau, then the corresponding
1976 * row needs to be added instead (taking care of the denominators).
1977 * Note that this is very unlikely, but perhaps not entirely impossible.
1979 * The current value of the cut is known to be negative (or at least
1980 * non-positive), so row_sign is set accordingly.
1982 * Return the row of the cut or -1.
1984 static int add_parametric_cut(struct isl_tab *tab, int row,
1985 struct isl_context *context)
1987 struct isl_vec *div;
1988 int d;
1989 int i;
1990 int r;
1991 isl_int *r_row;
1992 int col;
1993 int n;
1994 unsigned off = 2 + tab->M;
1996 if (!context)
1997 return -1;
1999 div = get_row_parameter_div(tab, row);
2000 if (!div)
2001 return -1;
2003 n = tab->n_div;
2004 d = context->op->get_div(context, tab, div);
2005 isl_vec_free(div);
2006 if (d < 0)
2007 return -1;
2009 if (isl_tab_extend_cons(tab, 1) < 0)
2010 return -1;
2011 r = isl_tab_allocate_con(tab);
2012 if (r < 0)
2013 return -1;
2015 r_row = tab->mat->row[tab->con[r].index];
2016 isl_int_set(r_row[0], tab->mat->row[row][0]);
2017 isl_int_neg(r_row[1], tab->mat->row[row][1]);
2018 isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
2019 isl_int_neg(r_row[1], r_row[1]);
2020 if (tab->M)
2021 isl_int_set_si(r_row[2], 0);
2022 for (i = 0; i < tab->n_param; ++i) {
2023 if (tab->var[i].is_row)
2024 continue;
2025 col = tab->var[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_div; ++i) {
2032 if (tab->var[tab->n_var - tab->n_div + i].is_row)
2033 continue;
2034 col = tab->var[tab->n_var - tab->n_div + i].index;
2035 isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
2036 isl_int_fdiv_r(r_row[off + col], r_row[off + col],
2037 tab->mat->row[row][0]);
2038 isl_int_neg(r_row[off + col], r_row[off + col]);
2040 for (i = 0; i < tab->n_col; ++i) {
2041 if (tab->col_var[i] >= 0 &&
2042 (tab->col_var[i] < tab->n_param ||
2043 tab->col_var[i] >= tab->n_var - tab->n_div))
2044 continue;
2045 isl_int_fdiv_r(r_row[off + i],
2046 tab->mat->row[row][off + i], tab->mat->row[row][0]);
2048 if (tab->var[tab->n_var - tab->n_div + d].is_row) {
2049 isl_int gcd;
2050 int d_row = tab->var[tab->n_var - tab->n_div + d].index;
2051 isl_int_init(gcd);
2052 isl_int_gcd(gcd, tab->mat->row[d_row][0], r_row[0]);
2053 isl_int_divexact(r_row[0], r_row[0], gcd);
2054 isl_int_divexact(gcd, tab->mat->row[d_row][0], gcd);
2055 isl_seq_combine(r_row + 1, gcd, r_row + 1,
2056 r_row[0], tab->mat->row[d_row] + 1,
2057 off - 1 + tab->n_col);
2058 isl_int_mul(r_row[0], r_row[0], tab->mat->row[d_row][0]);
2059 isl_int_clear(gcd);
2060 } else {
2061 col = tab->var[tab->n_var - tab->n_div + d].index;
2062 isl_int_set(r_row[off + col], tab->mat->row[row][0]);
2065 tab->con[r].is_nonneg = 1;
2066 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
2067 return -1;
2068 if (tab->row_sign)
2069 tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
2071 row = tab->con[r].index;
2073 if (d >= n && context->op->detect_equalities(context, tab) < 0)
2074 return -1;
2076 return row;
2079 /* Construct a tableau for bmap that can be used for computing
2080 * the lexicographic minimum (or maximum) of bmap.
2081 * If not NULL, then dom is the domain where the minimum
2082 * should be computed. In this case, we set up a parametric
2083 * tableau with row signs (initialized to "unknown").
2084 * If M is set, then the tableau will use a big parameter.
2085 * If max is set, then a maximum should be computed instead of a minimum.
2086 * This means that for each variable x, the tableau will contain the variable
2087 * x' = M - x, rather than x' = M + x. This in turn means that the coefficient
2088 * of the variables in all constraints are negated prior to adding them
2089 * to the tableau.
2091 static struct isl_tab *tab_for_lexmin(struct isl_basic_map *bmap,
2092 struct isl_basic_set *dom, unsigned M, int max)
2094 int i;
2095 struct isl_tab *tab;
2096 unsigned n_var;
2097 unsigned o_var;
2099 tab = isl_tab_alloc(bmap->ctx, 2 * bmap->n_eq + bmap->n_ineq + 1,
2100 isl_basic_map_total_dim(bmap), M);
2101 if (!tab)
2102 return NULL;
2104 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
2105 if (dom) {
2106 tab->n_param = isl_basic_set_total_dim(dom) - dom->n_div;
2107 tab->n_div = dom->n_div;
2108 tab->row_sign = isl_calloc_array(bmap->ctx,
2109 enum isl_tab_row_sign, tab->mat->n_row);
2110 if (tab->mat->n_row && !tab->row_sign)
2111 goto error;
2113 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
2114 if (isl_tab_mark_empty(tab) < 0)
2115 goto error;
2116 return tab;
2119 for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
2120 tab->var[i].is_nonneg = 1;
2121 tab->var[i].frozen = 1;
2123 o_var = 1 + tab->n_param;
2124 n_var = tab->n_var - tab->n_param - tab->n_div;
2125 for (i = 0; i < bmap->n_eq; ++i) {
2126 if (max)
2127 isl_seq_neg(bmap->eq[i] + o_var,
2128 bmap->eq[i] + o_var, n_var);
2129 tab = add_lexmin_valid_eq(tab, bmap->eq[i]);
2130 if (max)
2131 isl_seq_neg(bmap->eq[i] + o_var,
2132 bmap->eq[i] + o_var, n_var);
2133 if (!tab || tab->empty)
2134 return tab;
2136 if (bmap->n_eq && restore_lexmin(tab) < 0)
2137 goto error;
2138 for (i = 0; i < bmap->n_ineq; ++i) {
2139 if (max)
2140 isl_seq_neg(bmap->ineq[i] + o_var,
2141 bmap->ineq[i] + o_var, n_var);
2142 tab = add_lexmin_ineq(tab, bmap->ineq[i]);
2143 if (max)
2144 isl_seq_neg(bmap->ineq[i] + o_var,
2145 bmap->ineq[i] + o_var, n_var);
2146 if (!tab || tab->empty)
2147 return tab;
2149 return tab;
2150 error:
2151 isl_tab_free(tab);
2152 return NULL;
2155 /* Given a main tableau where more than one row requires a split,
2156 * determine and return the "best" row to split on.
2158 * Given two rows in the main tableau, if the inequality corresponding
2159 * to the first row is redundant with respect to that of the second row
2160 * in the current tableau, then it is better to split on the second row,
2161 * since in the positive part, both rows will be positive.
2162 * (In the negative part a pivot will have to be performed and just about
2163 * anything can happen to the sign of the other row.)
2165 * As a simple heuristic, we therefore select the row that makes the most
2166 * of the other rows redundant.
2168 * Perhaps it would also be useful to look at the number of constraints
2169 * that conflict with any given constraint.
2171 * best is the best row so far (-1 when we have not found any row yet).
2172 * best_r is the number of other rows made redundant by row best.
2173 * When best is still -1, bset_r is meaningless, but it is initialized
2174 * to some arbitrary value (0) anyway. Without this redundant initialization
2175 * valgrind may warn about uninitialized memory accesses when isl
2176 * is compiled with some versions of gcc.
2178 static int best_split(struct isl_tab *tab, struct isl_tab *context_tab)
2180 struct isl_tab_undo *snap;
2181 int split;
2182 int row;
2183 int best = -1;
2184 int best_r = 0;
2186 if (isl_tab_extend_cons(context_tab, 2) < 0)
2187 return -1;
2189 snap = isl_tab_snap(context_tab);
2191 for (split = tab->n_redundant; split < tab->n_row; ++split) {
2192 struct isl_tab_undo *snap2;
2193 struct isl_vec *ineq = NULL;
2194 int r = 0;
2195 int ok;
2197 if (!isl_tab_var_from_row(tab, split)->is_nonneg)
2198 continue;
2199 if (tab->row_sign[split] != isl_tab_row_any)
2200 continue;
2202 ineq = get_row_parameter_ineq(tab, split);
2203 if (!ineq)
2204 return -1;
2205 ok = isl_tab_add_ineq(context_tab, ineq->el) >= 0;
2206 isl_vec_free(ineq);
2207 if (!ok)
2208 return -1;
2210 snap2 = isl_tab_snap(context_tab);
2212 for (row = tab->n_redundant; row < tab->n_row; ++row) {
2213 struct isl_tab_var *var;
2215 if (row == split)
2216 continue;
2217 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
2218 continue;
2219 if (tab->row_sign[row] != isl_tab_row_any)
2220 continue;
2222 ineq = get_row_parameter_ineq(tab, row);
2223 if (!ineq)
2224 return -1;
2225 ok = isl_tab_add_ineq(context_tab, ineq->el) >= 0;
2226 isl_vec_free(ineq);
2227 if (!ok)
2228 return -1;
2229 var = &context_tab->con[context_tab->n_con - 1];
2230 if (!context_tab->empty &&
2231 !isl_tab_min_at_most_neg_one(context_tab, var))
2232 r++;
2233 if (isl_tab_rollback(context_tab, snap2) < 0)
2234 return -1;
2236 if (best == -1 || r > best_r) {
2237 best = split;
2238 best_r = r;
2240 if (isl_tab_rollback(context_tab, snap) < 0)
2241 return -1;
2244 return best;
2247 static struct isl_basic_set *context_lex_peek_basic_set(
2248 struct isl_context *context)
2250 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2251 if (!clex->tab)
2252 return NULL;
2253 return isl_tab_peek_bset(clex->tab);
2256 static struct isl_tab *context_lex_peek_tab(struct isl_context *context)
2258 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2259 return clex->tab;
2262 static void context_lex_add_eq(struct isl_context *context, isl_int *eq,
2263 int check, int update)
2265 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2266 if (isl_tab_extend_cons(clex->tab, 2) < 0)
2267 goto error;
2268 if (add_lexmin_eq(clex->tab, eq) < 0)
2269 goto error;
2270 if (check) {
2271 int v = tab_has_valid_sample(clex->tab, eq, 1);
2272 if (v < 0)
2273 goto error;
2274 if (!v)
2275 clex->tab = check_integer_feasible(clex->tab);
2277 if (update)
2278 clex->tab = check_samples(clex->tab, eq, 1);
2279 return;
2280 error:
2281 isl_tab_free(clex->tab);
2282 clex->tab = NULL;
2285 static void context_lex_add_ineq(struct isl_context *context, isl_int *ineq,
2286 int check, int update)
2288 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2289 if (isl_tab_extend_cons(clex->tab, 1) < 0)
2290 goto error;
2291 clex->tab = add_lexmin_ineq(clex->tab, ineq);
2292 if (check) {
2293 int v = tab_has_valid_sample(clex->tab, ineq, 0);
2294 if (v < 0)
2295 goto error;
2296 if (!v)
2297 clex->tab = check_integer_feasible(clex->tab);
2299 if (update)
2300 clex->tab = check_samples(clex->tab, ineq, 0);
2301 return;
2302 error:
2303 isl_tab_free(clex->tab);
2304 clex->tab = NULL;
2307 static int context_lex_add_ineq_wrap(void *user, isl_int *ineq)
2309 struct isl_context *context = (struct isl_context *)user;
2310 context_lex_add_ineq(context, ineq, 0, 0);
2311 return context->op->is_ok(context) ? 0 : -1;
2314 /* Check which signs can be obtained by "ineq" on all the currently
2315 * active sample values. See row_sign for more information.
2317 static enum isl_tab_row_sign tab_ineq_sign(struct isl_tab *tab, isl_int *ineq,
2318 int strict)
2320 int i;
2321 int sgn;
2322 isl_int tmp;
2323 enum isl_tab_row_sign res = isl_tab_row_unknown;
2325 isl_assert(tab->mat->ctx, tab->samples, return isl_tab_row_unknown);
2326 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var,
2327 return isl_tab_row_unknown);
2329 isl_int_init(tmp);
2330 for (i = tab->n_outside; i < tab->n_sample; ++i) {
2331 isl_seq_inner_product(tab->samples->row[i], ineq,
2332 1 + tab->n_var, &tmp);
2333 sgn = isl_int_sgn(tmp);
2334 if (sgn > 0 || (sgn == 0 && strict)) {
2335 if (res == isl_tab_row_unknown)
2336 res = isl_tab_row_pos;
2337 if (res == isl_tab_row_neg)
2338 res = isl_tab_row_any;
2340 if (sgn < 0) {
2341 if (res == isl_tab_row_unknown)
2342 res = isl_tab_row_neg;
2343 if (res == isl_tab_row_pos)
2344 res = isl_tab_row_any;
2346 if (res == isl_tab_row_any)
2347 break;
2349 isl_int_clear(tmp);
2351 return res;
2354 static enum isl_tab_row_sign context_lex_ineq_sign(struct isl_context *context,
2355 isl_int *ineq, int strict)
2357 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2358 return tab_ineq_sign(clex->tab, ineq, strict);
2361 /* Check whether "ineq" can be added to the tableau without rendering
2362 * it infeasible.
2364 static int context_lex_test_ineq(struct isl_context *context, isl_int *ineq)
2366 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2367 struct isl_tab_undo *snap;
2368 int feasible;
2370 if (!clex->tab)
2371 return -1;
2373 if (isl_tab_extend_cons(clex->tab, 1) < 0)
2374 return -1;
2376 snap = isl_tab_snap(clex->tab);
2377 if (isl_tab_push_basis(clex->tab) < 0)
2378 return -1;
2379 clex->tab = add_lexmin_ineq(clex->tab, ineq);
2380 clex->tab = check_integer_feasible(clex->tab);
2381 if (!clex->tab)
2382 return -1;
2383 feasible = !clex->tab->empty;
2384 if (isl_tab_rollback(clex->tab, snap) < 0)
2385 return -1;
2387 return feasible;
2390 static int context_lex_get_div(struct isl_context *context, struct isl_tab *tab,
2391 struct isl_vec *div)
2393 return get_div(tab, context, div);
2396 /* Add a div specified by "div" to the context tableau and return
2397 * 1 if the div is obviously non-negative.
2398 * context_tab_add_div will always return 1, because all variables
2399 * in a isl_context_lex tableau are non-negative.
2400 * However, if we are using a big parameter in the context, then this only
2401 * reflects the non-negativity of the variable used to _encode_ the
2402 * div, i.e., div' = M + div, so we can't draw any conclusions.
2404 static int context_lex_add_div(struct isl_context *context, struct isl_vec *div)
2406 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2407 int nonneg;
2408 nonneg = context_tab_add_div(clex->tab, div,
2409 context_lex_add_ineq_wrap, context);
2410 if (nonneg < 0)
2411 return -1;
2412 if (clex->tab->M)
2413 return 0;
2414 return nonneg;
2417 static int context_lex_detect_equalities(struct isl_context *context,
2418 struct isl_tab *tab)
2420 return 0;
2423 static int context_lex_best_split(struct isl_context *context,
2424 struct isl_tab *tab)
2426 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2427 struct isl_tab_undo *snap;
2428 int r;
2430 snap = isl_tab_snap(clex->tab);
2431 if (isl_tab_push_basis(clex->tab) < 0)
2432 return -1;
2433 r = best_split(tab, clex->tab);
2435 if (r >= 0 && isl_tab_rollback(clex->tab, snap) < 0)
2436 return -1;
2438 return r;
2441 static int context_lex_is_empty(struct isl_context *context)
2443 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2444 if (!clex->tab)
2445 return -1;
2446 return clex->tab->empty;
2449 static void *context_lex_save(struct isl_context *context)
2451 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2452 struct isl_tab_undo *snap;
2454 snap = isl_tab_snap(clex->tab);
2455 if (isl_tab_push_basis(clex->tab) < 0)
2456 return NULL;
2457 if (isl_tab_save_samples(clex->tab) < 0)
2458 return NULL;
2460 return snap;
2463 static void context_lex_restore(struct isl_context *context, void *save)
2465 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2466 if (isl_tab_rollback(clex->tab, (struct isl_tab_undo *)save) < 0) {
2467 isl_tab_free(clex->tab);
2468 clex->tab = NULL;
2472 static void context_lex_discard(void *save)
2476 static int context_lex_is_ok(struct isl_context *context)
2478 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2479 return !!clex->tab;
2482 /* For each variable in the context tableau, check if the variable can
2483 * only attain non-negative values. If so, mark the parameter as non-negative
2484 * in the main tableau. This allows for a more direct identification of some
2485 * cases of violated constraints.
2487 static struct isl_tab *tab_detect_nonnegative_parameters(struct isl_tab *tab,
2488 struct isl_tab *context_tab)
2490 int i;
2491 struct isl_tab_undo *snap;
2492 struct isl_vec *ineq = NULL;
2493 struct isl_tab_var *var;
2494 int n;
2496 if (context_tab->n_var == 0)
2497 return tab;
2499 ineq = isl_vec_alloc(tab->mat->ctx, 1 + context_tab->n_var);
2500 if (!ineq)
2501 goto error;
2503 if (isl_tab_extend_cons(context_tab, 1) < 0)
2504 goto error;
2506 snap = isl_tab_snap(context_tab);
2508 n = 0;
2509 isl_seq_clr(ineq->el, ineq->size);
2510 for (i = 0; i < context_tab->n_var; ++i) {
2511 isl_int_set_si(ineq->el[1 + i], 1);
2512 if (isl_tab_add_ineq(context_tab, ineq->el) < 0)
2513 goto error;
2514 var = &context_tab->con[context_tab->n_con - 1];
2515 if (!context_tab->empty &&
2516 !isl_tab_min_at_most_neg_one(context_tab, var)) {
2517 int j = i;
2518 if (i >= tab->n_param)
2519 j = i - tab->n_param + tab->n_var - tab->n_div;
2520 tab->var[j].is_nonneg = 1;
2521 n++;
2523 isl_int_set_si(ineq->el[1 + i], 0);
2524 if (isl_tab_rollback(context_tab, snap) < 0)
2525 goto error;
2528 if (context_tab->M && n == context_tab->n_var) {
2529 context_tab->mat = isl_mat_drop_cols(context_tab->mat, 2, 1);
2530 context_tab->M = 0;
2533 isl_vec_free(ineq);
2534 return tab;
2535 error:
2536 isl_vec_free(ineq);
2537 isl_tab_free(tab);
2538 return NULL;
2541 static struct isl_tab *context_lex_detect_nonnegative_parameters(
2542 struct isl_context *context, struct isl_tab *tab)
2544 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2545 struct isl_tab_undo *snap;
2547 if (!tab)
2548 return NULL;
2550 snap = isl_tab_snap(clex->tab);
2551 if (isl_tab_push_basis(clex->tab) < 0)
2552 goto error;
2554 tab = tab_detect_nonnegative_parameters(tab, clex->tab);
2556 if (isl_tab_rollback(clex->tab, snap) < 0)
2557 goto error;
2559 return tab;
2560 error:
2561 isl_tab_free(tab);
2562 return NULL;
2565 static void context_lex_invalidate(struct isl_context *context)
2567 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2568 isl_tab_free(clex->tab);
2569 clex->tab = NULL;
2572 static void context_lex_free(struct isl_context *context)
2574 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2575 isl_tab_free(clex->tab);
2576 free(clex);
2579 struct isl_context_op isl_context_lex_op = {
2580 context_lex_detect_nonnegative_parameters,
2581 context_lex_peek_basic_set,
2582 context_lex_peek_tab,
2583 context_lex_add_eq,
2584 context_lex_add_ineq,
2585 context_lex_ineq_sign,
2586 context_lex_test_ineq,
2587 context_lex_get_div,
2588 context_lex_add_div,
2589 context_lex_detect_equalities,
2590 context_lex_best_split,
2591 context_lex_is_empty,
2592 context_lex_is_ok,
2593 context_lex_save,
2594 context_lex_restore,
2595 context_lex_discard,
2596 context_lex_invalidate,
2597 context_lex_free,
2600 static struct isl_tab *context_tab_for_lexmin(struct isl_basic_set *bset)
2602 struct isl_tab *tab;
2604 if (!bset)
2605 return NULL;
2606 tab = tab_for_lexmin((struct isl_basic_map *)bset, NULL, 1, 0);
2607 if (!tab)
2608 goto error;
2609 if (isl_tab_track_bset(tab, bset) < 0)
2610 goto error;
2611 tab = isl_tab_init_samples(tab);
2612 return tab;
2613 error:
2614 isl_basic_set_free(bset);
2615 return NULL;
2618 static struct isl_context *isl_context_lex_alloc(struct isl_basic_set *dom)
2620 struct isl_context_lex *clex;
2622 if (!dom)
2623 return NULL;
2625 clex = isl_alloc_type(dom->ctx, struct isl_context_lex);
2626 if (!clex)
2627 return NULL;
2629 clex->context.op = &isl_context_lex_op;
2631 clex->tab = context_tab_for_lexmin(isl_basic_set_copy(dom));
2632 if (restore_lexmin(clex->tab) < 0)
2633 goto error;
2634 clex->tab = check_integer_feasible(clex->tab);
2635 if (!clex->tab)
2636 goto error;
2638 return &clex->context;
2639 error:
2640 clex->context.op->free(&clex->context);
2641 return NULL;
2644 /* Representation of the context when using generalized basis reduction.
2646 * "shifted" contains the offsets of the unit hypercubes that lie inside the
2647 * context. Any rational point in "shifted" can therefore be rounded
2648 * up to an integer point in the context.
2649 * If the context is constrained by any equality, then "shifted" is not used
2650 * as it would be empty.
2652 struct isl_context_gbr {
2653 struct isl_context context;
2654 struct isl_tab *tab;
2655 struct isl_tab *shifted;
2656 struct isl_tab *cone;
2659 static struct isl_tab *context_gbr_detect_nonnegative_parameters(
2660 struct isl_context *context, struct isl_tab *tab)
2662 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2663 if (!tab)
2664 return NULL;
2665 return tab_detect_nonnegative_parameters(tab, cgbr->tab);
2668 static struct isl_basic_set *context_gbr_peek_basic_set(
2669 struct isl_context *context)
2671 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2672 if (!cgbr->tab)
2673 return NULL;
2674 return isl_tab_peek_bset(cgbr->tab);
2677 static struct isl_tab *context_gbr_peek_tab(struct isl_context *context)
2679 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2680 return cgbr->tab;
2683 /* Initialize the "shifted" tableau of the context, which
2684 * contains the constraints of the original tableau shifted
2685 * by the sum of all negative coefficients. This ensures
2686 * that any rational point in the shifted tableau can
2687 * be rounded up to yield an integer point in the original tableau.
2689 static void gbr_init_shifted(struct isl_context_gbr *cgbr)
2691 int i, j;
2692 struct isl_vec *cst;
2693 struct isl_basic_set *bset = isl_tab_peek_bset(cgbr->tab);
2694 unsigned dim = isl_basic_set_total_dim(bset);
2696 cst = isl_vec_alloc(cgbr->tab->mat->ctx, bset->n_ineq);
2697 if (!cst)
2698 return;
2700 for (i = 0; i < bset->n_ineq; ++i) {
2701 isl_int_set(cst->el[i], bset->ineq[i][0]);
2702 for (j = 0; j < dim; ++j) {
2703 if (!isl_int_is_neg(bset->ineq[i][1 + j]))
2704 continue;
2705 isl_int_add(bset->ineq[i][0], bset->ineq[i][0],
2706 bset->ineq[i][1 + j]);
2710 cgbr->shifted = isl_tab_from_basic_set(bset, 0);
2712 for (i = 0; i < bset->n_ineq; ++i)
2713 isl_int_set(bset->ineq[i][0], cst->el[i]);
2715 isl_vec_free(cst);
2718 /* Check if the shifted tableau is non-empty, and if so
2719 * use the sample point to construct an integer point
2720 * of the context tableau.
2722 static struct isl_vec *gbr_get_shifted_sample(struct isl_context_gbr *cgbr)
2724 struct isl_vec *sample;
2726 if (!cgbr->shifted)
2727 gbr_init_shifted(cgbr);
2728 if (!cgbr->shifted)
2729 return NULL;
2730 if (cgbr->shifted->empty)
2731 return isl_vec_alloc(cgbr->tab->mat->ctx, 0);
2733 sample = isl_tab_get_sample_value(cgbr->shifted);
2734 sample = isl_vec_ceil(sample);
2736 return sample;
2739 static struct isl_basic_set *drop_constant_terms(struct isl_basic_set *bset)
2741 int i;
2743 if (!bset)
2744 return NULL;
2746 for (i = 0; i < bset->n_eq; ++i)
2747 isl_int_set_si(bset->eq[i][0], 0);
2749 for (i = 0; i < bset->n_ineq; ++i)
2750 isl_int_set_si(bset->ineq[i][0], 0);
2752 return bset;
2755 static int use_shifted(struct isl_context_gbr *cgbr)
2757 if (!cgbr->tab)
2758 return 0;
2759 return cgbr->tab->bmap->n_eq == 0 && cgbr->tab->bmap->n_div == 0;
2762 static struct isl_vec *gbr_get_sample(struct isl_context_gbr *cgbr)
2764 struct isl_basic_set *bset;
2765 struct isl_basic_set *cone;
2767 if (isl_tab_sample_is_integer(cgbr->tab))
2768 return isl_tab_get_sample_value(cgbr->tab);
2770 if (use_shifted(cgbr)) {
2771 struct isl_vec *sample;
2773 sample = gbr_get_shifted_sample(cgbr);
2774 if (!sample || sample->size > 0)
2775 return sample;
2777 isl_vec_free(sample);
2780 if (!cgbr->cone) {
2781 bset = isl_tab_peek_bset(cgbr->tab);
2782 cgbr->cone = isl_tab_from_recession_cone(bset, 0);
2783 if (!cgbr->cone)
2784 return NULL;
2785 if (isl_tab_track_bset(cgbr->cone,
2786 isl_basic_set_copy(bset)) < 0)
2787 return NULL;
2789 if (isl_tab_detect_implicit_equalities(cgbr->cone) < 0)
2790 return NULL;
2792 if (cgbr->cone->n_dead == cgbr->cone->n_col) {
2793 struct isl_vec *sample;
2794 struct isl_tab_undo *snap;
2796 if (cgbr->tab->basis) {
2797 if (cgbr->tab->basis->n_col != 1 + cgbr->tab->n_var) {
2798 isl_mat_free(cgbr->tab->basis);
2799 cgbr->tab->basis = NULL;
2801 cgbr->tab->n_zero = 0;
2802 cgbr->tab->n_unbounded = 0;
2805 snap = isl_tab_snap(cgbr->tab);
2807 sample = isl_tab_sample(cgbr->tab);
2809 if (!sample || isl_tab_rollback(cgbr->tab, snap) < 0) {
2810 isl_vec_free(sample);
2811 return NULL;
2814 return sample;
2817 cone = isl_basic_set_dup(isl_tab_peek_bset(cgbr->cone));
2818 cone = drop_constant_terms(cone);
2819 cone = isl_basic_set_update_from_tab(cone, cgbr->cone);
2820 cone = isl_basic_set_underlying_set(cone);
2821 cone = isl_basic_set_gauss(cone, NULL);
2823 bset = isl_basic_set_dup(isl_tab_peek_bset(cgbr->tab));
2824 bset = isl_basic_set_update_from_tab(bset, cgbr->tab);
2825 bset = isl_basic_set_underlying_set(bset);
2826 bset = isl_basic_set_gauss(bset, NULL);
2828 return isl_basic_set_sample_with_cone(bset, cone);
2831 static void check_gbr_integer_feasible(struct isl_context_gbr *cgbr)
2833 struct isl_vec *sample;
2835 if (!cgbr->tab)
2836 return;
2838 if (cgbr->tab->empty)
2839 return;
2841 sample = gbr_get_sample(cgbr);
2842 if (!sample)
2843 goto error;
2845 if (sample->size == 0) {
2846 isl_vec_free(sample);
2847 if (isl_tab_mark_empty(cgbr->tab) < 0)
2848 goto error;
2849 return;
2852 if (isl_tab_add_sample(cgbr->tab, sample) < 0)
2853 goto error;
2855 return;
2856 error:
2857 isl_tab_free(cgbr->tab);
2858 cgbr->tab = NULL;
2861 static struct isl_tab *add_gbr_eq(struct isl_tab *tab, isl_int *eq)
2863 if (!tab)
2864 return NULL;
2866 if (isl_tab_extend_cons(tab, 2) < 0)
2867 goto error;
2869 if (isl_tab_add_eq(tab, eq) < 0)
2870 goto error;
2872 return tab;
2873 error:
2874 isl_tab_free(tab);
2875 return NULL;
2878 /* Add the equality described by "eq" to the context.
2879 * If "check" is set, then we check if the context is empty after
2880 * adding the equality.
2881 * If "update" is set, then we check if the samples are still valid.
2883 * We do not explicitly add shifted copies of the equality to
2884 * cgbr->shifted since they would conflict with each other.
2885 * Instead, we directly mark cgbr->shifted empty.
2887 static void context_gbr_add_eq(struct isl_context *context, isl_int *eq,
2888 int check, int update)
2890 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2892 cgbr->tab = add_gbr_eq(cgbr->tab, eq);
2894 if (cgbr->shifted && !cgbr->shifted->empty && use_shifted(cgbr)) {
2895 if (isl_tab_mark_empty(cgbr->shifted) < 0)
2896 goto error;
2899 if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
2900 if (isl_tab_extend_cons(cgbr->cone, 2) < 0)
2901 goto error;
2902 if (isl_tab_add_eq(cgbr->cone, eq) < 0)
2903 goto error;
2906 if (check) {
2907 int v = tab_has_valid_sample(cgbr->tab, eq, 1);
2908 if (v < 0)
2909 goto error;
2910 if (!v)
2911 check_gbr_integer_feasible(cgbr);
2913 if (update)
2914 cgbr->tab = check_samples(cgbr->tab, eq, 1);
2915 return;
2916 error:
2917 isl_tab_free(cgbr->tab);
2918 cgbr->tab = NULL;
2921 static void add_gbr_ineq(struct isl_context_gbr *cgbr, isl_int *ineq)
2923 if (!cgbr->tab)
2924 return;
2926 if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
2927 goto error;
2929 if (isl_tab_add_ineq(cgbr->tab, ineq) < 0)
2930 goto error;
2932 if (cgbr->shifted && !cgbr->shifted->empty && use_shifted(cgbr)) {
2933 int i;
2934 unsigned dim;
2935 dim = isl_basic_map_total_dim(cgbr->tab->bmap);
2937 if (isl_tab_extend_cons(cgbr->shifted, 1) < 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_add(ineq[0], ineq[0], ineq[1 + i]);
2946 if (isl_tab_add_ineq(cgbr->shifted, ineq) < 0)
2947 goto error;
2949 for (i = 0; i < dim; ++i) {
2950 if (!isl_int_is_neg(ineq[1 + i]))
2951 continue;
2952 isl_int_sub(ineq[0], ineq[0], ineq[1 + i]);
2956 if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
2957 if (isl_tab_extend_cons(cgbr->cone, 1) < 0)
2958 goto error;
2959 if (isl_tab_add_ineq(cgbr->cone, ineq) < 0)
2960 goto error;
2963 return;
2964 error:
2965 isl_tab_free(cgbr->tab);
2966 cgbr->tab = NULL;
2969 static void context_gbr_add_ineq(struct isl_context *context, isl_int *ineq,
2970 int check, int update)
2972 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2974 add_gbr_ineq(cgbr, ineq);
2975 if (!cgbr->tab)
2976 return;
2978 if (check) {
2979 int v = tab_has_valid_sample(cgbr->tab, ineq, 0);
2980 if (v < 0)
2981 goto error;
2982 if (!v)
2983 check_gbr_integer_feasible(cgbr);
2985 if (update)
2986 cgbr->tab = check_samples(cgbr->tab, ineq, 0);
2987 return;
2988 error:
2989 isl_tab_free(cgbr->tab);
2990 cgbr->tab = NULL;
2993 static int context_gbr_add_ineq_wrap(void *user, isl_int *ineq)
2995 struct isl_context *context = (struct isl_context *)user;
2996 context_gbr_add_ineq(context, ineq, 0, 0);
2997 return context->op->is_ok(context) ? 0 : -1;
3000 static enum isl_tab_row_sign context_gbr_ineq_sign(struct isl_context *context,
3001 isl_int *ineq, int strict)
3003 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3004 return tab_ineq_sign(cgbr->tab, ineq, strict);
3007 /* Check whether "ineq" can be added to the tableau without rendering
3008 * it infeasible.
3010 static int context_gbr_test_ineq(struct isl_context *context, isl_int *ineq)
3012 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3013 struct isl_tab_undo *snap;
3014 struct isl_tab_undo *shifted_snap = NULL;
3015 struct isl_tab_undo *cone_snap = NULL;
3016 int feasible;
3018 if (!cgbr->tab)
3019 return -1;
3021 if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
3022 return -1;
3024 snap = isl_tab_snap(cgbr->tab);
3025 if (cgbr->shifted)
3026 shifted_snap = isl_tab_snap(cgbr->shifted);
3027 if (cgbr->cone)
3028 cone_snap = isl_tab_snap(cgbr->cone);
3029 add_gbr_ineq(cgbr, ineq);
3030 check_gbr_integer_feasible(cgbr);
3031 if (!cgbr->tab)
3032 return -1;
3033 feasible = !cgbr->tab->empty;
3034 if (isl_tab_rollback(cgbr->tab, snap) < 0)
3035 return -1;
3036 if (shifted_snap) {
3037 if (isl_tab_rollback(cgbr->shifted, shifted_snap))
3038 return -1;
3039 } else if (cgbr->shifted) {
3040 isl_tab_free(cgbr->shifted);
3041 cgbr->shifted = NULL;
3043 if (cone_snap) {
3044 if (isl_tab_rollback(cgbr->cone, cone_snap))
3045 return -1;
3046 } else if (cgbr->cone) {
3047 isl_tab_free(cgbr->cone);
3048 cgbr->cone = NULL;
3051 return feasible;
3054 /* Return the column of the last of the variables associated to
3055 * a column that has a non-zero coefficient.
3056 * This function is called in a context where only coefficients
3057 * of parameters or divs can be non-zero.
3059 static int last_non_zero_var_col(struct isl_tab *tab, isl_int *p)
3061 int i;
3062 int col;
3064 if (tab->n_var == 0)
3065 return -1;
3067 for (i = tab->n_var - 1; i >= 0; --i) {
3068 if (i >= tab->n_param && i < tab->n_var - tab->n_div)
3069 continue;
3070 if (tab->var[i].is_row)
3071 continue;
3072 col = tab->var[i].index;
3073 if (!isl_int_is_zero(p[col]))
3074 return col;
3077 return -1;
3080 /* Look through all the recently added equalities in the context
3081 * to see if we can propagate any of them to the main tableau.
3083 * The newly added equalities in the context are encoded as pairs
3084 * of inequalities starting at inequality "first".
3086 * We tentatively add each of these equalities to the main tableau
3087 * and if this happens to result in a row with a final coefficient
3088 * that is one or negative one, we use it to kill a column
3089 * in the main tableau. Otherwise, we discard the tentatively
3090 * added row.
3092 * Return 0 on success and -1 on failure.
3094 static int propagate_equalities(struct isl_context_gbr *cgbr,
3095 struct isl_tab *tab, unsigned first)
3097 int i;
3098 struct isl_vec *eq = NULL;
3100 eq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
3101 if (!eq)
3102 goto error;
3104 if (isl_tab_extend_cons(tab, (cgbr->tab->bmap->n_ineq - first)/2) < 0)
3105 goto error;
3107 isl_seq_clr(eq->el + 1 + tab->n_param,
3108 tab->n_var - tab->n_param - tab->n_div);
3109 for (i = first; i < cgbr->tab->bmap->n_ineq; i += 2) {
3110 int j;
3111 int r;
3112 struct isl_tab_undo *snap;
3113 snap = isl_tab_snap(tab);
3115 isl_seq_cpy(eq->el, cgbr->tab->bmap->ineq[i], 1 + tab->n_param);
3116 isl_seq_cpy(eq->el + 1 + tab->n_var - tab->n_div,
3117 cgbr->tab->bmap->ineq[i] + 1 + tab->n_param,
3118 tab->n_div);
3120 r = isl_tab_add_row(tab, eq->el);
3121 if (r < 0)
3122 goto error;
3123 r = tab->con[r].index;
3124 j = last_non_zero_var_col(tab, tab->mat->row[r] + 2 + tab->M);
3125 if (j < 0 || j < tab->n_dead ||
3126 !isl_int_is_one(tab->mat->row[r][0]) ||
3127 (!isl_int_is_one(tab->mat->row[r][2 + tab->M + j]) &&
3128 !isl_int_is_negone(tab->mat->row[r][2 + tab->M + j]))) {
3129 if (isl_tab_rollback(tab, snap) < 0)
3130 goto error;
3131 continue;
3133 if (isl_tab_pivot(tab, r, j) < 0)
3134 goto error;
3135 if (isl_tab_kill_col(tab, j) < 0)
3136 goto error;
3138 if (restore_lexmin(tab) < 0)
3139 goto error;
3142 isl_vec_free(eq);
3144 return 0;
3145 error:
3146 isl_vec_free(eq);
3147 isl_tab_free(cgbr->tab);
3148 cgbr->tab = NULL;
3149 return -1;
3152 static int context_gbr_detect_equalities(struct isl_context *context,
3153 struct isl_tab *tab)
3155 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3156 unsigned n_ineq;
3158 if (!cgbr->cone) {
3159 struct isl_basic_set *bset = isl_tab_peek_bset(cgbr->tab);
3160 cgbr->cone = isl_tab_from_recession_cone(bset, 0);
3161 if (!cgbr->cone)
3162 goto error;
3163 if (isl_tab_track_bset(cgbr->cone,
3164 isl_basic_set_copy(bset)) < 0)
3165 goto error;
3167 if (isl_tab_detect_implicit_equalities(cgbr->cone) < 0)
3168 goto error;
3170 n_ineq = cgbr->tab->bmap->n_ineq;
3171 cgbr->tab = isl_tab_detect_equalities(cgbr->tab, cgbr->cone);
3172 if (!cgbr->tab)
3173 return -1;
3174 if (cgbr->tab->bmap->n_ineq > n_ineq &&
3175 propagate_equalities(cgbr, tab, n_ineq) < 0)
3176 return -1;
3178 return 0;
3179 error:
3180 isl_tab_free(cgbr->tab);
3181 cgbr->tab = NULL;
3182 return -1;
3185 static int context_gbr_get_div(struct isl_context *context, struct isl_tab *tab,
3186 struct isl_vec *div)
3188 return get_div(tab, context, div);
3191 static int context_gbr_add_div(struct isl_context *context, struct isl_vec *div)
3193 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3194 if (cgbr->cone) {
3195 int k;
3197 if (isl_tab_extend_cons(cgbr->cone, 3) < 0)
3198 return -1;
3199 if (isl_tab_extend_vars(cgbr->cone, 1) < 0)
3200 return -1;
3201 if (isl_tab_allocate_var(cgbr->cone) <0)
3202 return -1;
3204 cgbr->cone->bmap = isl_basic_map_extend_space(cgbr->cone->bmap,
3205 isl_basic_map_get_space(cgbr->cone->bmap), 1, 0, 2);
3206 k = isl_basic_map_alloc_div(cgbr->cone->bmap);
3207 if (k < 0)
3208 return -1;
3209 isl_seq_cpy(cgbr->cone->bmap->div[k], div->el, div->size);
3210 if (isl_tab_push(cgbr->cone, isl_tab_undo_bmap_div) < 0)
3211 return -1;
3213 return context_tab_add_div(cgbr->tab, div,
3214 context_gbr_add_ineq_wrap, context);
3217 static int context_gbr_best_split(struct isl_context *context,
3218 struct isl_tab *tab)
3220 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3221 struct isl_tab_undo *snap;
3222 int r;
3224 snap = isl_tab_snap(cgbr->tab);
3225 r = best_split(tab, cgbr->tab);
3227 if (r >= 0 && isl_tab_rollback(cgbr->tab, snap) < 0)
3228 return -1;
3230 return r;
3233 static int context_gbr_is_empty(struct isl_context *context)
3235 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3236 if (!cgbr->tab)
3237 return -1;
3238 return cgbr->tab->empty;
3241 struct isl_gbr_tab_undo {
3242 struct isl_tab_undo *tab_snap;
3243 struct isl_tab_undo *shifted_snap;
3244 struct isl_tab_undo *cone_snap;
3247 static void *context_gbr_save(struct isl_context *context)
3249 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3250 struct isl_gbr_tab_undo *snap;
3252 if (!cgbr->tab)
3253 return NULL;
3255 snap = isl_alloc_type(cgbr->tab->mat->ctx, struct isl_gbr_tab_undo);
3256 if (!snap)
3257 return NULL;
3259 snap->tab_snap = isl_tab_snap(cgbr->tab);
3260 if (isl_tab_save_samples(cgbr->tab) < 0)
3261 goto error;
3263 if (cgbr->shifted)
3264 snap->shifted_snap = isl_tab_snap(cgbr->shifted);
3265 else
3266 snap->shifted_snap = NULL;
3268 if (cgbr->cone)
3269 snap->cone_snap = isl_tab_snap(cgbr->cone);
3270 else
3271 snap->cone_snap = NULL;
3273 return snap;
3274 error:
3275 free(snap);
3276 return NULL;
3279 static void context_gbr_restore(struct isl_context *context, void *save)
3281 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3282 struct isl_gbr_tab_undo *snap = (struct isl_gbr_tab_undo *)save;
3283 if (!snap)
3284 goto error;
3285 if (isl_tab_rollback(cgbr->tab, snap->tab_snap) < 0)
3286 goto error;
3288 if (snap->shifted_snap) {
3289 if (isl_tab_rollback(cgbr->shifted, snap->shifted_snap) < 0)
3290 goto error;
3291 } else if (cgbr->shifted) {
3292 isl_tab_free(cgbr->shifted);
3293 cgbr->shifted = NULL;
3296 if (snap->cone_snap) {
3297 if (isl_tab_rollback(cgbr->cone, snap->cone_snap) < 0)
3298 goto error;
3299 } else if (cgbr->cone) {
3300 isl_tab_free(cgbr->cone);
3301 cgbr->cone = NULL;
3304 free(snap);
3306 return;
3307 error:
3308 free(snap);
3309 isl_tab_free(cgbr->tab);
3310 cgbr->tab = NULL;
3313 static void context_gbr_discard(void *save)
3315 struct isl_gbr_tab_undo *snap = (struct isl_gbr_tab_undo *)save;
3316 free(snap);
3319 static int context_gbr_is_ok(struct isl_context *context)
3321 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3322 return !!cgbr->tab;
3325 static void context_gbr_invalidate(struct isl_context *context)
3327 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3328 isl_tab_free(cgbr->tab);
3329 cgbr->tab = NULL;
3332 static void context_gbr_free(struct isl_context *context)
3334 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3335 isl_tab_free(cgbr->tab);
3336 isl_tab_free(cgbr->shifted);
3337 isl_tab_free(cgbr->cone);
3338 free(cgbr);
3341 struct isl_context_op isl_context_gbr_op = {
3342 context_gbr_detect_nonnegative_parameters,
3343 context_gbr_peek_basic_set,
3344 context_gbr_peek_tab,
3345 context_gbr_add_eq,
3346 context_gbr_add_ineq,
3347 context_gbr_ineq_sign,
3348 context_gbr_test_ineq,
3349 context_gbr_get_div,
3350 context_gbr_add_div,
3351 context_gbr_detect_equalities,
3352 context_gbr_best_split,
3353 context_gbr_is_empty,
3354 context_gbr_is_ok,
3355 context_gbr_save,
3356 context_gbr_restore,
3357 context_gbr_discard,
3358 context_gbr_invalidate,
3359 context_gbr_free,
3362 static struct isl_context *isl_context_gbr_alloc(struct isl_basic_set *dom)
3364 struct isl_context_gbr *cgbr;
3366 if (!dom)
3367 return NULL;
3369 cgbr = isl_calloc_type(dom->ctx, struct isl_context_gbr);
3370 if (!cgbr)
3371 return NULL;
3373 cgbr->context.op = &isl_context_gbr_op;
3375 cgbr->shifted = NULL;
3376 cgbr->cone = NULL;
3377 cgbr->tab = isl_tab_from_basic_set(dom, 1);
3378 cgbr->tab = isl_tab_init_samples(cgbr->tab);
3379 if (!cgbr->tab)
3380 goto error;
3381 check_gbr_integer_feasible(cgbr);
3383 return &cgbr->context;
3384 error:
3385 cgbr->context.op->free(&cgbr->context);
3386 return NULL;
3389 static struct isl_context *isl_context_alloc(struct isl_basic_set *dom)
3391 if (!dom)
3392 return NULL;
3394 if (dom->ctx->opt->context == ISL_CONTEXT_LEXMIN)
3395 return isl_context_lex_alloc(dom);
3396 else
3397 return isl_context_gbr_alloc(dom);
3400 /* Construct an isl_sol_map structure for accumulating the solution.
3401 * If track_empty is set, then we also keep track of the parts
3402 * of the context where there is no solution.
3403 * If max is set, then we are solving a maximization, rather than
3404 * a minimization problem, which means that the variables in the
3405 * tableau have value "M - x" rather than "M + x".
3407 static struct isl_sol *sol_map_init(struct isl_basic_map *bmap,
3408 struct isl_basic_set *dom, int track_empty, int max)
3410 struct isl_sol_map *sol_map = NULL;
3412 if (!bmap)
3413 goto error;
3415 sol_map = isl_calloc_type(bmap->ctx, struct isl_sol_map);
3416 if (!sol_map)
3417 goto error;
3419 sol_map->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
3420 sol_map->sol.dec_level.callback.run = &sol_dec_level_wrap;
3421 sol_map->sol.dec_level.sol = &sol_map->sol;
3422 sol_map->sol.max = max;
3423 sol_map->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
3424 sol_map->sol.add = &sol_map_add_wrap;
3425 sol_map->sol.add_empty = track_empty ? &sol_map_add_empty_wrap : NULL;
3426 sol_map->sol.free = &sol_map_free_wrap;
3427 sol_map->map = isl_map_alloc_space(isl_basic_map_get_space(bmap), 1,
3428 ISL_MAP_DISJOINT);
3429 if (!sol_map->map)
3430 goto error;
3432 sol_map->sol.context = isl_context_alloc(dom);
3433 if (!sol_map->sol.context)
3434 goto error;
3436 if (track_empty) {
3437 sol_map->empty = isl_set_alloc_space(isl_basic_set_get_space(dom),
3438 1, ISL_SET_DISJOINT);
3439 if (!sol_map->empty)
3440 goto error;
3443 isl_basic_set_free(dom);
3444 return &sol_map->sol;
3445 error:
3446 isl_basic_set_free(dom);
3447 sol_map_free(sol_map);
3448 return NULL;
3451 /* Check whether all coefficients of (non-parameter) variables
3452 * are non-positive, meaning that no pivots can be performed on the row.
3454 static int is_critical(struct isl_tab *tab, int row)
3456 int j;
3457 unsigned off = 2 + tab->M;
3459 for (j = tab->n_dead; j < tab->n_col; ++j) {
3460 if (tab->col_var[j] >= 0 &&
3461 (tab->col_var[j] < tab->n_param ||
3462 tab->col_var[j] >= tab->n_var - tab->n_div))
3463 continue;
3465 if (isl_int_is_pos(tab->mat->row[row][off + j]))
3466 return 0;
3469 return 1;
3472 /* Check whether the inequality represented by vec is strict over the integers,
3473 * i.e., there are no integer values satisfying the constraint with
3474 * equality. This happens if the gcd of the coefficients is not a divisor
3475 * of the constant term. If so, scale the constraint down by the gcd
3476 * of the coefficients.
3478 static int is_strict(struct isl_vec *vec)
3480 isl_int gcd;
3481 int strict = 0;
3483 isl_int_init(gcd);
3484 isl_seq_gcd(vec->el + 1, vec->size - 1, &gcd);
3485 if (!isl_int_is_one(gcd)) {
3486 strict = !isl_int_is_divisible_by(vec->el[0], gcd);
3487 isl_int_fdiv_q(vec->el[0], vec->el[0], gcd);
3488 isl_seq_scale_down(vec->el + 1, vec->el + 1, gcd, vec->size-1);
3490 isl_int_clear(gcd);
3492 return strict;
3495 /* Determine the sign of the given row of the main tableau.
3496 * The result is one of
3497 * isl_tab_row_pos: always non-negative; no pivot needed
3498 * isl_tab_row_neg: always non-positive; pivot
3499 * isl_tab_row_any: can be both positive and negative; split
3501 * We first handle some simple cases
3502 * - the row sign may be known already
3503 * - the row may be obviously non-negative
3504 * - the parametric constant may be equal to that of another row
3505 * for which we know the sign. This sign will be either "pos" or
3506 * "any". If it had been "neg" then we would have pivoted before.
3508 * If none of these cases hold, we check the value of the row for each
3509 * of the currently active samples. Based on the signs of these values
3510 * we make an initial determination of the sign of the row.
3512 * all zero -> unk(nown)
3513 * all non-negative -> pos
3514 * all non-positive -> neg
3515 * both negative and positive -> all
3517 * If we end up with "all", we are done.
3518 * Otherwise, we perform a check for positive and/or negative
3519 * values as follows.
3521 * samples neg unk pos
3522 * <0 ? Y N Y N
3523 * pos any pos
3524 * >0 ? Y N Y N
3525 * any neg any neg
3527 * There is no special sign for "zero", because we can usually treat zero
3528 * as either non-negative or non-positive, whatever works out best.
3529 * However, if the row is "critical", meaning that pivoting is impossible
3530 * then we don't want to limp zero with the non-positive case, because
3531 * then we we would lose the solution for those values of the parameters
3532 * where the value of the row is zero. Instead, we treat 0 as non-negative
3533 * ensuring a split if the row can attain both zero and negative values.
3534 * The same happens when the original constraint was one that could not
3535 * be satisfied with equality by any integer values of the parameters.
3536 * In this case, we normalize the constraint, but then a value of zero
3537 * for the normalized constraint is actually a positive value for the
3538 * original constraint, so again we need to treat zero as non-negative.
3539 * In both these cases, we have the following decision tree instead:
3541 * all non-negative -> pos
3542 * all negative -> neg
3543 * both negative and non-negative -> all
3545 * samples neg pos
3546 * <0 ? Y N
3547 * any pos
3548 * >=0 ? Y N
3549 * any neg
3551 static enum isl_tab_row_sign row_sign(struct isl_tab *tab,
3552 struct isl_sol *sol, int row)
3554 struct isl_vec *ineq = NULL;
3555 enum isl_tab_row_sign res = isl_tab_row_unknown;
3556 int critical;
3557 int strict;
3558 int row2;
3560 if (tab->row_sign[row] != isl_tab_row_unknown)
3561 return tab->row_sign[row];
3562 if (is_obviously_nonneg(tab, row))
3563 return isl_tab_row_pos;
3564 for (row2 = tab->n_redundant; row2 < tab->n_row; ++row2) {
3565 if (tab->row_sign[row2] == isl_tab_row_unknown)
3566 continue;
3567 if (identical_parameter_line(tab, row, row2))
3568 return tab->row_sign[row2];
3571 critical = is_critical(tab, row);
3573 ineq = get_row_parameter_ineq(tab, row);
3574 if (!ineq)
3575 goto error;
3577 strict = is_strict(ineq);
3579 res = sol->context->op->ineq_sign(sol->context, ineq->el,
3580 critical || strict);
3582 if (res == isl_tab_row_unknown || res == isl_tab_row_pos) {
3583 /* test for negative values */
3584 int feasible;
3585 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3586 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3588 feasible = sol->context->op->test_ineq(sol->context, ineq->el);
3589 if (feasible < 0)
3590 goto error;
3591 if (!feasible)
3592 res = isl_tab_row_pos;
3593 else
3594 res = (res == isl_tab_row_unknown) ? isl_tab_row_neg
3595 : isl_tab_row_any;
3596 if (res == isl_tab_row_neg) {
3597 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3598 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3602 if (res == isl_tab_row_neg) {
3603 /* test for positive values */
3604 int feasible;
3605 if (!critical && !strict)
3606 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3608 feasible = sol->context->op->test_ineq(sol->context, ineq->el);
3609 if (feasible < 0)
3610 goto error;
3611 if (feasible)
3612 res = isl_tab_row_any;
3615 isl_vec_free(ineq);
3616 return res;
3617 error:
3618 isl_vec_free(ineq);
3619 return isl_tab_row_unknown;
3622 static void find_solutions(struct isl_sol *sol, struct isl_tab *tab);
3624 /* Find solutions for values of the parameters that satisfy the given
3625 * inequality.
3627 * We currently take a snapshot of the context tableau that is reset
3628 * when we return from this function, while we make a copy of the main
3629 * tableau, leaving the original main tableau untouched.
3630 * These are fairly arbitrary choices. Making a copy also of the context
3631 * tableau would obviate the need to undo any changes made to it later,
3632 * while taking a snapshot of the main tableau could reduce memory usage.
3633 * If we were to switch to taking a snapshot of the main tableau,
3634 * we would have to keep in mind that we need to save the row signs
3635 * and that we need to do this before saving the current basis
3636 * such that the basis has been restore before we restore the row signs.
3638 static void find_in_pos(struct isl_sol *sol, struct isl_tab *tab, isl_int *ineq)
3640 void *saved;
3642 if (!sol->context)
3643 goto error;
3644 saved = sol->context->op->save(sol->context);
3646 tab = isl_tab_dup(tab);
3647 if (!tab)
3648 goto error;
3650 sol->context->op->add_ineq(sol->context, ineq, 0, 1);
3652 find_solutions(sol, tab);
3654 if (!sol->error)
3655 sol->context->op->restore(sol->context, saved);
3656 else
3657 sol->context->op->discard(saved);
3658 return;
3659 error:
3660 sol->error = 1;
3663 /* Record the absence of solutions for those values of the parameters
3664 * that do not satisfy the given inequality with equality.
3666 static void no_sol_in_strict(struct isl_sol *sol,
3667 struct isl_tab *tab, struct isl_vec *ineq)
3669 int empty;
3670 void *saved;
3672 if (!sol->context || sol->error)
3673 goto error;
3674 saved = sol->context->op->save(sol->context);
3676 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3678 sol->context->op->add_ineq(sol->context, ineq->el, 1, 0);
3679 if (!sol->context)
3680 goto error;
3682 empty = tab->empty;
3683 tab->empty = 1;
3684 sol_add(sol, tab);
3685 tab->empty = empty;
3687 isl_int_add_ui(ineq->el[0], ineq->el[0], 1);
3689 sol->context->op->restore(sol->context, saved);
3690 return;
3691 error:
3692 sol->error = 1;
3695 /* Reset all row variables that are marked to have a sign that may
3696 * be both positive and negative to have an unknown sign.
3698 static void reset_any_to_unknown(struct isl_tab *tab)
3700 int row;
3702 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3703 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
3704 continue;
3705 if (tab->row_sign[row] == isl_tab_row_any)
3706 tab->row_sign[row] = isl_tab_row_unknown;
3710 /* Compute the lexicographic minimum of the set represented by the main
3711 * tableau "tab" within the context "sol->context_tab".
3712 * On entry the sample value of the main tableau is lexicographically
3713 * less than or equal to this lexicographic minimum.
3714 * Pivots are performed until a feasible point is found, which is then
3715 * necessarily equal to the minimum, or until the tableau is found to
3716 * be infeasible. Some pivots may need to be performed for only some
3717 * feasible values of the context tableau. If so, the context tableau
3718 * is split into a part where the pivot is needed and a part where it is not.
3720 * Whenever we enter the main loop, the main tableau is such that no
3721 * "obvious" pivots need to be performed on it, where "obvious" means
3722 * that the given row can be seen to be negative without looking at
3723 * the context tableau. In particular, for non-parametric problems,
3724 * no pivots need to be performed on the main tableau.
3725 * The caller of find_solutions is responsible for making this property
3726 * hold prior to the first iteration of the loop, while restore_lexmin
3727 * is called before every other iteration.
3729 * Inside the main loop, we first examine the signs of the rows of
3730 * the main tableau within the context of the context tableau.
3731 * If we find a row that is always non-positive for all values of
3732 * the parameters satisfying the context tableau and negative for at
3733 * least one value of the parameters, we perform the appropriate pivot
3734 * and start over. An exception is the case where no pivot can be
3735 * performed on the row. In this case, we require that the sign of
3736 * the row is negative for all values of the parameters (rather than just
3737 * non-positive). This special case is handled inside row_sign, which
3738 * will say that the row can have any sign if it determines that it can
3739 * attain both negative and zero values.
3741 * If we can't find a row that always requires a pivot, but we can find
3742 * one or more rows that require a pivot for some values of the parameters
3743 * (i.e., the row can attain both positive and negative signs), then we split
3744 * the context tableau into two parts, one where we force the sign to be
3745 * non-negative and one where we force is to be negative.
3746 * The non-negative part is handled by a recursive call (through find_in_pos).
3747 * Upon returning from this call, we continue with the negative part and
3748 * perform the required pivot.
3750 * If no such rows can be found, all rows are non-negative and we have
3751 * found a (rational) feasible point. If we only wanted a rational point
3752 * then we are done.
3753 * Otherwise, we check if all values of the sample point of the tableau
3754 * are integral for the variables. If so, we have found the minimal
3755 * integral point and we are done.
3756 * If the sample point is not integral, then we need to make a distinction
3757 * based on whether the constant term is non-integral or the coefficients
3758 * of the parameters. Furthermore, in order to decide how to handle
3759 * the non-integrality, we also need to know whether the coefficients
3760 * of the other columns in the tableau are integral. This leads
3761 * to the following table. The first two rows do not correspond
3762 * to a non-integral sample point and are only mentioned for completeness.
3764 * constant parameters other
3766 * int int int |
3767 * int int rat | -> no problem
3769 * rat int int -> fail
3771 * rat int rat -> cut
3773 * int rat rat |
3774 * rat rat rat | -> parametric cut
3776 * int rat int |
3777 * rat rat int | -> split context
3779 * If the parametric constant is completely integral, then there is nothing
3780 * to be done. If the constant term is non-integral, but all the other
3781 * coefficient are integral, then there is nothing that can be done
3782 * and the tableau has no integral solution.
3783 * If, on the other hand, one or more of the other columns have rational
3784 * coefficients, but the parameter coefficients are all integral, then
3785 * we can perform a regular (non-parametric) cut.
3786 * Finally, if there is any parameter coefficient that is non-integral,
3787 * then we need to involve the context tableau. There are two cases here.
3788 * If at least one other column has a rational coefficient, then we
3789 * can perform a parametric cut in the main tableau by adding a new
3790 * integer division in the context tableau.
3791 * If all other columns have integral coefficients, then we need to
3792 * enforce that the rational combination of parameters (c + \sum a_i y_i)/m
3793 * is always integral. We do this by introducing an integer division
3794 * q = floor((c + \sum a_i y_i)/m) and stipulating that its argument should
3795 * always be integral in the context tableau, i.e., m q = c + \sum a_i y_i.
3796 * Since q is expressed in the tableau as
3797 * c + \sum a_i y_i - m q >= 0
3798 * -c - \sum a_i y_i + m q + m - 1 >= 0
3799 * it is sufficient to add the inequality
3800 * -c - \sum a_i y_i + m q >= 0
3801 * In the part of the context where this inequality does not hold, the
3802 * main tableau is marked as being empty.
3804 static void find_solutions(struct isl_sol *sol, struct isl_tab *tab)
3806 struct isl_context *context;
3807 int r;
3809 if (!tab || sol->error)
3810 goto error;
3812 context = sol->context;
3814 if (tab->empty)
3815 goto done;
3816 if (context->op->is_empty(context))
3817 goto done;
3819 for (r = 0; r >= 0 && tab && !tab->empty; r = restore_lexmin(tab)) {
3820 int flags;
3821 int row;
3822 enum isl_tab_row_sign sgn;
3823 int split = -1;
3824 int n_split = 0;
3826 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3827 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
3828 continue;
3829 sgn = row_sign(tab, sol, row);
3830 if (!sgn)
3831 goto error;
3832 tab->row_sign[row] = sgn;
3833 if (sgn == isl_tab_row_any)
3834 n_split++;
3835 if (sgn == isl_tab_row_any && split == -1)
3836 split = row;
3837 if (sgn == isl_tab_row_neg)
3838 break;
3840 if (row < tab->n_row)
3841 continue;
3842 if (split != -1) {
3843 struct isl_vec *ineq;
3844 if (n_split != 1)
3845 split = context->op->best_split(context, tab);
3846 if (split < 0)
3847 goto error;
3848 ineq = get_row_parameter_ineq(tab, split);
3849 if (!ineq)
3850 goto error;
3851 is_strict(ineq);
3852 reset_any_to_unknown(tab);
3853 tab->row_sign[split] = isl_tab_row_pos;
3854 sol_inc_level(sol);
3855 find_in_pos(sol, tab, ineq->el);
3856 tab->row_sign[split] = isl_tab_row_neg;
3857 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3858 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3859 if (!sol->error)
3860 context->op->add_ineq(context, ineq->el, 0, 1);
3861 isl_vec_free(ineq);
3862 if (sol->error)
3863 goto error;
3864 continue;
3866 if (tab->rational)
3867 break;
3868 row = first_non_integer_row(tab, &flags);
3869 if (row < 0)
3870 break;
3871 if (ISL_FL_ISSET(flags, I_PAR)) {
3872 if (ISL_FL_ISSET(flags, I_VAR)) {
3873 if (isl_tab_mark_empty(tab) < 0)
3874 goto error;
3875 break;
3877 row = add_cut(tab, row);
3878 } else if (ISL_FL_ISSET(flags, I_VAR)) {
3879 struct isl_vec *div;
3880 struct isl_vec *ineq;
3881 int d;
3882 div = get_row_split_div(tab, row);
3883 if (!div)
3884 goto error;
3885 d = context->op->get_div(context, tab, div);
3886 isl_vec_free(div);
3887 if (d < 0)
3888 goto error;
3889 ineq = ineq_for_div(context->op->peek_basic_set(context), d);
3890 if (!ineq)
3891 goto error;
3892 sol_inc_level(sol);
3893 no_sol_in_strict(sol, tab, ineq);
3894 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3895 context->op->add_ineq(context, ineq->el, 1, 1);
3896 isl_vec_free(ineq);
3897 if (sol->error || !context->op->is_ok(context))
3898 goto error;
3899 tab = set_row_cst_to_div(tab, row, d);
3900 if (context->op->is_empty(context))
3901 break;
3902 } else
3903 row = add_parametric_cut(tab, row, context);
3904 if (row < 0)
3905 goto error;
3907 if (r < 0)
3908 goto error;
3909 done:
3910 sol_add(sol, tab);
3911 isl_tab_free(tab);
3912 return;
3913 error:
3914 isl_tab_free(tab);
3915 sol->error = 1;
3918 /* Does "sol" contain a pair of partial solutions that could potentially
3919 * be merged?
3921 * We currently only check that "sol" is not in an error state
3922 * and that there are at least two partial solutions of which the final two
3923 * are defined at the same level.
3925 static int sol_has_mergeable_solutions(struct isl_sol *sol)
3927 if (sol->error)
3928 return 0;
3929 if (!sol->partial)
3930 return 0;
3931 if (!sol->partial->next)
3932 return 0;
3933 return sol->partial->level == sol->partial->next->level;
3936 /* Compute the lexicographic minimum of the set represented by the main
3937 * tableau "tab" within the context "sol->context_tab".
3939 * As a preprocessing step, we first transfer all the purely parametric
3940 * equalities from the main tableau to the context tableau, i.e.,
3941 * parameters that have been pivoted to a row.
3942 * These equalities are ignored by the main algorithm, because the
3943 * corresponding rows may not be marked as being non-negative.
3944 * In parts of the context where the added equality does not hold,
3945 * the main tableau is marked as being empty.
3947 * Before we embark on the actual computation, we save a copy
3948 * of the context. When we return, we check if there are any
3949 * partial solutions that can potentially be merged. If so,
3950 * we perform a rollback to the initial state of the context.
3951 * The merging of partial solutions happens inside calls to
3952 * sol_dec_level that are pushed onto the undo stack of the context.
3953 * If there are no partial solutions that can potentially be merged
3954 * then the rollback is skipped as it would just be wasted effort.
3956 static void find_solutions_main(struct isl_sol *sol, struct isl_tab *tab)
3958 int row;
3959 void *saved;
3961 if (!tab)
3962 goto error;
3964 sol->level = 0;
3966 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3967 int p;
3968 struct isl_vec *eq;
3970 if (tab->row_var[row] < 0)
3971 continue;
3972 if (tab->row_var[row] >= tab->n_param &&
3973 tab->row_var[row] < tab->n_var - tab->n_div)
3974 continue;
3975 if (tab->row_var[row] < tab->n_param)
3976 p = tab->row_var[row];
3977 else
3978 p = tab->row_var[row]
3979 + tab->n_param - (tab->n_var - tab->n_div);
3981 eq = isl_vec_alloc(tab->mat->ctx, 1+tab->n_param+tab->n_div);
3982 if (!eq)
3983 goto error;
3984 get_row_parameter_line(tab, row, eq->el);
3985 isl_int_neg(eq->el[1 + p], tab->mat->row[row][0]);
3986 eq = isl_vec_normalize(eq);
3988 sol_inc_level(sol);
3989 no_sol_in_strict(sol, tab, eq);
3991 isl_seq_neg(eq->el, eq->el, eq->size);
3992 sol_inc_level(sol);
3993 no_sol_in_strict(sol, tab, eq);
3994 isl_seq_neg(eq->el, eq->el, eq->size);
3996 sol->context->op->add_eq(sol->context, eq->el, 1, 1);
3998 isl_vec_free(eq);
4000 if (isl_tab_mark_redundant(tab, row) < 0)
4001 goto error;
4003 if (sol->context->op->is_empty(sol->context))
4004 break;
4006 row = tab->n_redundant - 1;
4009 saved = sol->context->op->save(sol->context);
4011 find_solutions(sol, tab);
4013 if (sol_has_mergeable_solutions(sol))
4014 sol->context->op->restore(sol->context, saved);
4015 else
4016 sol->context->op->discard(saved);
4018 sol->level = 0;
4019 sol_pop(sol);
4021 return;
4022 error:
4023 isl_tab_free(tab);
4024 sol->error = 1;
4027 /* Check if integer division "div" of "dom" also occurs in "bmap".
4028 * If so, return its position within the divs.
4029 * If not, return -1.
4031 static int find_context_div(struct isl_basic_map *bmap,
4032 struct isl_basic_set *dom, unsigned div)
4034 int i;
4035 unsigned b_dim = isl_space_dim(bmap->dim, isl_dim_all);
4036 unsigned d_dim = isl_space_dim(dom->dim, isl_dim_all);
4038 if (isl_int_is_zero(dom->div[div][0]))
4039 return -1;
4040 if (isl_seq_first_non_zero(dom->div[div] + 2 + d_dim, dom->n_div) != -1)
4041 return -1;
4043 for (i = 0; i < bmap->n_div; ++i) {
4044 if (isl_int_is_zero(bmap->div[i][0]))
4045 continue;
4046 if (isl_seq_first_non_zero(bmap->div[i] + 2 + d_dim,
4047 (b_dim - d_dim) + bmap->n_div) != -1)
4048 continue;
4049 if (isl_seq_eq(bmap->div[i], dom->div[div], 2 + d_dim))
4050 return i;
4052 return -1;
4055 /* The correspondence between the variables in the main tableau,
4056 * the context tableau, and the input map and domain is as follows.
4057 * The first n_param and the last n_div variables of the main tableau
4058 * form the variables of the context tableau.
4059 * In the basic map, these n_param variables correspond to the
4060 * parameters and the input dimensions. In the domain, they correspond
4061 * to the parameters and the set dimensions.
4062 * The n_div variables correspond to the integer divisions in the domain.
4063 * To ensure that everything lines up, we may need to copy some of the
4064 * integer divisions of the domain to the map. These have to be placed
4065 * in the same order as those in the context and they have to be placed
4066 * after any other integer divisions that the map may have.
4067 * This function performs the required reordering.
4069 static struct isl_basic_map *align_context_divs(struct isl_basic_map *bmap,
4070 struct isl_basic_set *dom)
4072 int i;
4073 int common = 0;
4074 int other;
4076 for (i = 0; i < dom->n_div; ++i)
4077 if (find_context_div(bmap, dom, i) != -1)
4078 common++;
4079 other = bmap->n_div - common;
4080 if (dom->n_div - common > 0) {
4081 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
4082 dom->n_div - common, 0, 0);
4083 if (!bmap)
4084 return NULL;
4086 for (i = 0; i < dom->n_div; ++i) {
4087 int pos = find_context_div(bmap, dom, i);
4088 if (pos < 0) {
4089 pos = isl_basic_map_alloc_div(bmap);
4090 if (pos < 0)
4091 goto error;
4092 isl_int_set_si(bmap->div[pos][0], 0);
4094 if (pos != other + i)
4095 isl_basic_map_swap_div(bmap, pos, other + i);
4097 return bmap;
4098 error:
4099 isl_basic_map_free(bmap);
4100 return NULL;
4103 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4104 * some obvious symmetries.
4106 * We make sure the divs in the domain are properly ordered,
4107 * because they will be added one by one in the given order
4108 * during the construction of the solution map.
4110 static struct isl_sol *basic_map_partial_lexopt_base(
4111 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4112 __isl_give isl_set **empty, int max,
4113 struct isl_sol *(*init)(__isl_keep isl_basic_map *bmap,
4114 __isl_take isl_basic_set *dom, int track_empty, int max))
4116 struct isl_tab *tab;
4117 struct isl_sol *sol = NULL;
4118 struct isl_context *context;
4120 if (dom->n_div) {
4121 dom = isl_basic_set_order_divs(dom);
4122 bmap = align_context_divs(bmap, dom);
4124 sol = init(bmap, dom, !!empty, max);
4125 if (!sol)
4126 goto error;
4128 context = sol->context;
4129 if (isl_basic_set_plain_is_empty(context->op->peek_basic_set(context)))
4130 /* nothing */;
4131 else if (isl_basic_map_plain_is_empty(bmap)) {
4132 if (sol->add_empty)
4133 sol->add_empty(sol,
4134 isl_basic_set_copy(context->op->peek_basic_set(context)));
4135 } else {
4136 tab = tab_for_lexmin(bmap,
4137 context->op->peek_basic_set(context), 1, max);
4138 tab = context->op->detect_nonnegative_parameters(context, tab);
4139 find_solutions_main(sol, tab);
4141 if (sol->error)
4142 goto error;
4144 isl_basic_map_free(bmap);
4145 return sol;
4146 error:
4147 sol_free(sol);
4148 isl_basic_map_free(bmap);
4149 return NULL;
4152 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4153 * some obvious symmetries.
4155 * We call basic_map_partial_lexopt_base and extract the results.
4157 static __isl_give isl_map *basic_map_partial_lexopt_base_map(
4158 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4159 __isl_give isl_set **empty, int max)
4161 isl_map *result = NULL;
4162 struct isl_sol *sol;
4163 struct isl_sol_map *sol_map;
4165 sol = basic_map_partial_lexopt_base(bmap, dom, empty, max,
4166 &sol_map_init);
4167 if (!sol)
4168 return NULL;
4169 sol_map = (struct isl_sol_map *) sol;
4171 result = isl_map_copy(sol_map->map);
4172 if (empty)
4173 *empty = isl_set_copy(sol_map->empty);
4174 sol_free(&sol_map->sol);
4175 return result;
4178 /* Structure used during detection of parallel constraints.
4179 * n_in: number of "input" variables: isl_dim_param + isl_dim_in
4180 * n_out: number of "output" variables: isl_dim_out + isl_dim_div
4181 * val: the coefficients of the output variables
4183 struct isl_constraint_equal_info {
4184 isl_basic_map *bmap;
4185 unsigned n_in;
4186 unsigned n_out;
4187 isl_int *val;
4190 /* Check whether the coefficients of the output variables
4191 * of the constraint in "entry" are equal to info->val.
4193 static int constraint_equal(const void *entry, const void *val)
4195 isl_int **row = (isl_int **)entry;
4196 const struct isl_constraint_equal_info *info = val;
4198 return isl_seq_eq((*row) + 1 + info->n_in, info->val, info->n_out);
4201 /* Check whether "bmap" has a pair of constraints that have
4202 * the same coefficients for the output variables.
4203 * Note that the coefficients of the existentially quantified
4204 * variables need to be zero since the existentially quantified
4205 * of the result are usually not the same as those of the input.
4206 * the isl_dim_out and isl_dim_div dimensions.
4207 * If so, return 1 and return the row indices of the two constraints
4208 * in *first and *second.
4210 static int parallel_constraints(__isl_keep isl_basic_map *bmap,
4211 int *first, int *second)
4213 int i;
4214 isl_ctx *ctx;
4215 struct isl_hash_table *table = NULL;
4216 struct isl_hash_table_entry *entry;
4217 struct isl_constraint_equal_info info;
4218 unsigned n_out;
4219 unsigned n_div;
4221 ctx = isl_basic_map_get_ctx(bmap);
4222 table = isl_hash_table_alloc(ctx, bmap->n_ineq);
4223 if (!table)
4224 goto error;
4226 info.n_in = isl_basic_map_dim(bmap, isl_dim_param) +
4227 isl_basic_map_dim(bmap, isl_dim_in);
4228 info.bmap = bmap;
4229 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4230 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4231 info.n_out = n_out + n_div;
4232 for (i = 0; i < bmap->n_ineq; ++i) {
4233 uint32_t hash;
4235 info.val = bmap->ineq[i] + 1 + info.n_in;
4236 if (isl_seq_first_non_zero(info.val, n_out) < 0)
4237 continue;
4238 if (isl_seq_first_non_zero(info.val + n_out, n_div) >= 0)
4239 continue;
4240 hash = isl_seq_get_hash(info.val, info.n_out);
4241 entry = isl_hash_table_find(ctx, table, hash,
4242 constraint_equal, &info, 1);
4243 if (!entry)
4244 goto error;
4245 if (entry->data)
4246 break;
4247 entry->data = &bmap->ineq[i];
4250 if (i < bmap->n_ineq) {
4251 *first = ((isl_int **)entry->data) - bmap->ineq;
4252 *second = i;
4255 isl_hash_table_free(ctx, table);
4257 return i < bmap->n_ineq;
4258 error:
4259 isl_hash_table_free(ctx, table);
4260 return -1;
4263 /* Given a set of upper bounds in "var", add constraints to "bset"
4264 * that make the i-th bound smallest.
4266 * In particular, if there are n bounds b_i, then add the constraints
4268 * b_i <= b_j for j > i
4269 * b_i < b_j for j < i
4271 static __isl_give isl_basic_set *select_minimum(__isl_take isl_basic_set *bset,
4272 __isl_keep isl_mat *var, int i)
4274 isl_ctx *ctx;
4275 int j, k;
4277 ctx = isl_mat_get_ctx(var);
4279 for (j = 0; j < var->n_row; ++j) {
4280 if (j == i)
4281 continue;
4282 k = isl_basic_set_alloc_inequality(bset);
4283 if (k < 0)
4284 goto error;
4285 isl_seq_combine(bset->ineq[k], ctx->one, var->row[j],
4286 ctx->negone, var->row[i], var->n_col);
4287 isl_int_set_si(bset->ineq[k][var->n_col], 0);
4288 if (j < i)
4289 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
4292 bset = isl_basic_set_finalize(bset);
4294 return bset;
4295 error:
4296 isl_basic_set_free(bset);
4297 return NULL;
4300 /* Given a set of upper bounds on the last "input" variable m,
4301 * construct a set that assigns the minimal upper bound to m, i.e.,
4302 * construct a set that divides the space into cells where one
4303 * of the upper bounds is smaller than all the others and assign
4304 * this upper bound to m.
4306 * In particular, if there are n bounds b_i, then the result
4307 * consists of n basic sets, each one of the form
4309 * m = b_i
4310 * b_i <= b_j for j > i
4311 * b_i < b_j for j < i
4313 static __isl_give isl_set *set_minimum(__isl_take isl_space *dim,
4314 __isl_take isl_mat *var)
4316 int i, k;
4317 isl_basic_set *bset = NULL;
4318 isl_set *set = NULL;
4320 if (!dim || !var)
4321 goto error;
4323 set = isl_set_alloc_space(isl_space_copy(dim),
4324 var->n_row, ISL_SET_DISJOINT);
4326 for (i = 0; i < var->n_row; ++i) {
4327 bset = isl_basic_set_alloc_space(isl_space_copy(dim), 0,
4328 1, var->n_row - 1);
4329 k = isl_basic_set_alloc_equality(bset);
4330 if (k < 0)
4331 goto error;
4332 isl_seq_cpy(bset->eq[k], var->row[i], var->n_col);
4333 isl_int_set_si(bset->eq[k][var->n_col], -1);
4334 bset = select_minimum(bset, var, i);
4335 set = isl_set_add_basic_set(set, bset);
4338 isl_space_free(dim);
4339 isl_mat_free(var);
4340 return set;
4341 error:
4342 isl_basic_set_free(bset);
4343 isl_set_free(set);
4344 isl_space_free(dim);
4345 isl_mat_free(var);
4346 return NULL;
4349 /* Given that the last input variable of "bmap" represents the minimum
4350 * of the bounds in "cst", check whether we need to split the domain
4351 * based on which bound attains the minimum.
4353 * A split is needed when the minimum appears in an integer division
4354 * or in an equality. Otherwise, it is only needed if it appears in
4355 * an upper bound that is different from the upper bounds on which it
4356 * is defined.
4358 static int need_split_basic_map(__isl_keep isl_basic_map *bmap,
4359 __isl_keep isl_mat *cst)
4361 int i, j;
4362 unsigned total;
4363 unsigned pos;
4365 pos = cst->n_col - 1;
4366 total = isl_basic_map_dim(bmap, isl_dim_all);
4368 for (i = 0; i < bmap->n_div; ++i)
4369 if (!isl_int_is_zero(bmap->div[i][2 + pos]))
4370 return 1;
4372 for (i = 0; i < bmap->n_eq; ++i)
4373 if (!isl_int_is_zero(bmap->eq[i][1 + pos]))
4374 return 1;
4376 for (i = 0; i < bmap->n_ineq; ++i) {
4377 if (isl_int_is_nonneg(bmap->ineq[i][1 + pos]))
4378 continue;
4379 if (!isl_int_is_negone(bmap->ineq[i][1 + pos]))
4380 return 1;
4381 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + pos + 1,
4382 total - pos - 1) >= 0)
4383 return 1;
4385 for (j = 0; j < cst->n_row; ++j)
4386 if (isl_seq_eq(bmap->ineq[i], cst->row[j], cst->n_col))
4387 break;
4388 if (j >= cst->n_row)
4389 return 1;
4392 return 0;
4395 /* Given that the last set variable of "bset" represents the minimum
4396 * of the bounds in "cst", check whether we need to split the domain
4397 * based on which bound attains the minimum.
4399 * We simply call need_split_basic_map here. This is safe because
4400 * the position of the minimum is computed from "cst" and not
4401 * from "bmap".
4403 static int need_split_basic_set(__isl_keep isl_basic_set *bset,
4404 __isl_keep isl_mat *cst)
4406 return need_split_basic_map((isl_basic_map *)bset, cst);
4409 /* Given that the last set variable of "set" represents the minimum
4410 * of the bounds in "cst", check whether we need to split the domain
4411 * based on which bound attains the minimum.
4413 static int need_split_set(__isl_keep isl_set *set, __isl_keep isl_mat *cst)
4415 int i;
4417 for (i = 0; i < set->n; ++i)
4418 if (need_split_basic_set(set->p[i], cst))
4419 return 1;
4421 return 0;
4424 /* Given a set of which the last set variable is the minimum
4425 * of the bounds in "cst", split each basic set in the set
4426 * in pieces where one of the bounds is (strictly) smaller than the others.
4427 * This subdivision is given in "min_expr".
4428 * The variable is subsequently projected out.
4430 * We only do the split when it is needed.
4431 * For example if the last input variable m = min(a,b) and the only
4432 * constraints in the given basic set are lower bounds on m,
4433 * i.e., l <= m = min(a,b), then we can simply project out m
4434 * to obtain l <= a and l <= b, without having to split on whether
4435 * m is equal to a or b.
4437 static __isl_give isl_set *split(__isl_take isl_set *empty,
4438 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
4440 int n_in;
4441 int i;
4442 isl_space *dim;
4443 isl_set *res;
4445 if (!empty || !min_expr || !cst)
4446 goto error;
4448 n_in = isl_set_dim(empty, isl_dim_set);
4449 dim = isl_set_get_space(empty);
4450 dim = isl_space_drop_dims(dim, isl_dim_set, n_in - 1, 1);
4451 res = isl_set_empty(dim);
4453 for (i = 0; i < empty->n; ++i) {
4454 isl_set *set;
4456 set = isl_set_from_basic_set(isl_basic_set_copy(empty->p[i]));
4457 if (need_split_basic_set(empty->p[i], cst))
4458 set = isl_set_intersect(set, isl_set_copy(min_expr));
4459 set = isl_set_remove_dims(set, isl_dim_set, n_in - 1, 1);
4461 res = isl_set_union_disjoint(res, set);
4464 isl_set_free(empty);
4465 isl_set_free(min_expr);
4466 isl_mat_free(cst);
4467 return res;
4468 error:
4469 isl_set_free(empty);
4470 isl_set_free(min_expr);
4471 isl_mat_free(cst);
4472 return NULL;
4475 /* Given a map of which the last input variable is the minimum
4476 * of the bounds in "cst", split each basic set in the set
4477 * in pieces where one of the bounds is (strictly) smaller than the others.
4478 * This subdivision is given in "min_expr".
4479 * The variable is subsequently projected out.
4481 * The implementation is essentially the same as that of "split".
4483 static __isl_give isl_map *split_domain(__isl_take isl_map *opt,
4484 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
4486 int n_in;
4487 int i;
4488 isl_space *dim;
4489 isl_map *res;
4491 if (!opt || !min_expr || !cst)
4492 goto error;
4494 n_in = isl_map_dim(opt, isl_dim_in);
4495 dim = isl_map_get_space(opt);
4496 dim = isl_space_drop_dims(dim, isl_dim_in, n_in - 1, 1);
4497 res = isl_map_empty(dim);
4499 for (i = 0; i < opt->n; ++i) {
4500 isl_map *map;
4502 map = isl_map_from_basic_map(isl_basic_map_copy(opt->p[i]));
4503 if (need_split_basic_map(opt->p[i], cst))
4504 map = isl_map_intersect_domain(map,
4505 isl_set_copy(min_expr));
4506 map = isl_map_remove_dims(map, isl_dim_in, n_in - 1, 1);
4508 res = isl_map_union_disjoint(res, map);
4511 isl_map_free(opt);
4512 isl_set_free(min_expr);
4513 isl_mat_free(cst);
4514 return res;
4515 error:
4516 isl_map_free(opt);
4517 isl_set_free(min_expr);
4518 isl_mat_free(cst);
4519 return NULL;
4522 static __isl_give isl_map *basic_map_partial_lexopt(
4523 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4524 __isl_give isl_set **empty, int max);
4526 union isl_lex_res {
4527 void *p;
4528 isl_map *map;
4529 isl_pw_multi_aff *pma;
4532 /* This function is called from basic_map_partial_lexopt_symm.
4533 * The last variable of "bmap" and "dom" corresponds to the minimum
4534 * of the bounds in "cst". "map_space" is the space of the original
4535 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
4536 * is the space of the original domain.
4538 * We recursively call basic_map_partial_lexopt and then plug in
4539 * the definition of the minimum in the result.
4541 static __isl_give union isl_lex_res basic_map_partial_lexopt_symm_map_core(
4542 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4543 __isl_give isl_set **empty, int max, __isl_take isl_mat *cst,
4544 __isl_take isl_space *map_space, __isl_take isl_space *set_space)
4546 isl_map *opt;
4547 isl_set *min_expr;
4548 union isl_lex_res res;
4550 min_expr = set_minimum(isl_basic_set_get_space(dom), isl_mat_copy(cst));
4552 opt = basic_map_partial_lexopt(bmap, dom, empty, max);
4554 if (empty) {
4555 *empty = split(*empty,
4556 isl_set_copy(min_expr), isl_mat_copy(cst));
4557 *empty = isl_set_reset_space(*empty, set_space);
4560 opt = split_domain(opt, min_expr, cst);
4561 opt = isl_map_reset_space(opt, map_space);
4563 res.map = opt;
4564 return res;
4567 /* Given a basic map with at least two parallel constraints (as found
4568 * by the function parallel_constraints), first look for more constraints
4569 * parallel to the two constraint and replace the found list of parallel
4570 * constraints by a single constraint with as "input" part the minimum
4571 * of the input parts of the list of constraints. Then, recursively call
4572 * basic_map_partial_lexopt (possibly finding more parallel constraints)
4573 * and plug in the definition of the minimum in the result.
4575 * More specifically, given a set of constraints
4577 * a x + b_i(p) >= 0
4579 * Replace this set by a single constraint
4581 * a x + u >= 0
4583 * with u a new parameter with constraints
4585 * u <= b_i(p)
4587 * Any solution to the new system is also a solution for the original system
4588 * since
4590 * a x >= -u >= -b_i(p)
4592 * Moreover, m = min_i(b_i(p)) satisfies the constraints on u and can
4593 * therefore be plugged into the solution.
4595 static union isl_lex_res basic_map_partial_lexopt_symm(
4596 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4597 __isl_give isl_set **empty, int max, int first, int second,
4598 __isl_give union isl_lex_res (*core)(__isl_take isl_basic_map *bmap,
4599 __isl_take isl_basic_set *dom,
4600 __isl_give isl_set **empty,
4601 int max, __isl_take isl_mat *cst,
4602 __isl_take isl_space *map_space,
4603 __isl_take isl_space *set_space))
4605 int i, n, k;
4606 int *list = NULL;
4607 unsigned n_in, n_out, n_div;
4608 isl_ctx *ctx;
4609 isl_vec *var = NULL;
4610 isl_mat *cst = NULL;
4611 isl_space *map_space, *set_space;
4612 union isl_lex_res res;
4614 map_space = isl_basic_map_get_space(bmap);
4615 set_space = empty ? isl_basic_set_get_space(dom) : NULL;
4617 n_in = isl_basic_map_dim(bmap, isl_dim_param) +
4618 isl_basic_map_dim(bmap, isl_dim_in);
4619 n_out = isl_basic_map_dim(bmap, isl_dim_all) - n_in;
4621 ctx = isl_basic_map_get_ctx(bmap);
4622 list = isl_alloc_array(ctx, int, bmap->n_ineq);
4623 var = isl_vec_alloc(ctx, n_out);
4624 if ((bmap->n_ineq && !list) || (n_out && !var))
4625 goto error;
4627 list[0] = first;
4628 list[1] = second;
4629 isl_seq_cpy(var->el, bmap->ineq[first] + 1 + n_in, n_out);
4630 for (i = second + 1, n = 2; i < bmap->n_ineq; ++i) {
4631 if (isl_seq_eq(var->el, bmap->ineq[i] + 1 + n_in, n_out))
4632 list[n++] = i;
4635 cst = isl_mat_alloc(ctx, n, 1 + n_in);
4636 if (!cst)
4637 goto error;
4639 for (i = 0; i < n; ++i)
4640 isl_seq_cpy(cst->row[i], bmap->ineq[list[i]], 1 + n_in);
4642 bmap = isl_basic_map_cow(bmap);
4643 if (!bmap)
4644 goto error;
4645 for (i = n - 1; i >= 0; --i)
4646 if (isl_basic_map_drop_inequality(bmap, list[i]) < 0)
4647 goto error;
4649 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, 1);
4650 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
4651 k = isl_basic_map_alloc_inequality(bmap);
4652 if (k < 0)
4653 goto error;
4654 isl_seq_clr(bmap->ineq[k], 1 + n_in);
4655 isl_int_set_si(bmap->ineq[k][1 + n_in], 1);
4656 isl_seq_cpy(bmap->ineq[k] + 1 + n_in + 1, var->el, n_out);
4657 bmap = isl_basic_map_finalize(bmap);
4659 n_div = isl_basic_set_dim(dom, isl_dim_div);
4660 dom = isl_basic_set_add_dims(dom, isl_dim_set, 1);
4661 dom = isl_basic_set_extend_constraints(dom, 0, n);
4662 for (i = 0; i < n; ++i) {
4663 k = isl_basic_set_alloc_inequality(dom);
4664 if (k < 0)
4665 goto error;
4666 isl_seq_cpy(dom->ineq[k], cst->row[i], 1 + n_in);
4667 isl_int_set_si(dom->ineq[k][1 + n_in], -1);
4668 isl_seq_clr(dom->ineq[k] + 1 + n_in + 1, n_div);
4671 isl_vec_free(var);
4672 free(list);
4674 return core(bmap, dom, empty, max, cst, map_space, set_space);
4675 error:
4676 isl_space_free(map_space);
4677 isl_space_free(set_space);
4678 isl_mat_free(cst);
4679 isl_vec_free(var);
4680 free(list);
4681 isl_basic_set_free(dom);
4682 isl_basic_map_free(bmap);
4683 res.p = NULL;
4684 return res;
4687 static __isl_give isl_map *basic_map_partial_lexopt_symm_map(
4688 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4689 __isl_give isl_set **empty, int max, int first, int second)
4691 return basic_map_partial_lexopt_symm(bmap, dom, empty, max,
4692 first, second, &basic_map_partial_lexopt_symm_map_core).map;
4695 /* Recursive part of isl_tab_basic_map_partial_lexopt, after detecting
4696 * equalities and removing redundant constraints.
4698 * We first check if there are any parallel constraints (left).
4699 * If not, we are in the base case.
4700 * If there are parallel constraints, we replace them by a single
4701 * constraint in basic_map_partial_lexopt_symm and then call
4702 * this function recursively to look for more parallel constraints.
4704 static __isl_give isl_map *basic_map_partial_lexopt(
4705 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4706 __isl_give isl_set **empty, int max)
4708 int par = 0;
4709 int first, second;
4711 if (!bmap)
4712 goto error;
4714 if (bmap->ctx->opt->pip_symmetry)
4715 par = parallel_constraints(bmap, &first, &second);
4716 if (par < 0)
4717 goto error;
4718 if (!par)
4719 return basic_map_partial_lexopt_base_map(bmap, dom, empty, max);
4721 return basic_map_partial_lexopt_symm_map(bmap, dom, empty, max,
4722 first, second);
4723 error:
4724 isl_basic_set_free(dom);
4725 isl_basic_map_free(bmap);
4726 return NULL;
4729 /* Compute the lexicographic minimum (or maximum if "max" is set)
4730 * of "bmap" over the domain "dom" and return the result as a map.
4731 * If "empty" is not NULL, then *empty is assigned a set that
4732 * contains those parts of the domain where there is no solution.
4733 * If "bmap" is marked as rational (ISL_BASIC_MAP_RATIONAL),
4734 * then we compute the rational optimum. Otherwise, we compute
4735 * the integral optimum.
4737 * We perform some preprocessing. As the PILP solver does not
4738 * handle implicit equalities very well, we first make sure all
4739 * the equalities are explicitly available.
4741 * We also add context constraints to the basic map and remove
4742 * redundant constraints. This is only needed because of the
4743 * way we handle simple symmetries. In particular, we currently look
4744 * for symmetries on the constraints, before we set up the main tableau.
4745 * It is then no good to look for symmetries on possibly redundant constraints.
4747 struct isl_map *isl_tab_basic_map_partial_lexopt(
4748 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4749 struct isl_set **empty, int max)
4751 if (empty)
4752 *empty = NULL;
4753 if (!bmap || !dom)
4754 goto error;
4756 isl_assert(bmap->ctx,
4757 isl_basic_map_compatible_domain(bmap, dom), goto error);
4759 if (isl_basic_set_dim(dom, isl_dim_all) == 0)
4760 return basic_map_partial_lexopt(bmap, dom, empty, max);
4762 bmap = isl_basic_map_intersect_domain(bmap, isl_basic_set_copy(dom));
4763 bmap = isl_basic_map_detect_equalities(bmap);
4764 bmap = isl_basic_map_remove_redundancies(bmap);
4766 return basic_map_partial_lexopt(bmap, dom, empty, max);
4767 error:
4768 isl_basic_set_free(dom);
4769 isl_basic_map_free(bmap);
4770 return NULL;
4773 struct isl_sol_for {
4774 struct isl_sol sol;
4775 int (*fn)(__isl_take isl_basic_set *dom,
4776 __isl_take isl_aff_list *list, void *user);
4777 void *user;
4780 static void sol_for_free(struct isl_sol_for *sol_for)
4782 if (!sol_for)
4783 return;
4784 if (sol_for->sol.context)
4785 sol_for->sol.context->op->free(sol_for->sol.context);
4786 free(sol_for);
4789 static void sol_for_free_wrap(struct isl_sol *sol)
4791 sol_for_free((struct isl_sol_for *)sol);
4794 /* Add the solution identified by the tableau and the context tableau.
4796 * See documentation of sol_add for more details.
4798 * Instead of constructing a basic map, this function calls a user
4799 * defined function with the current context as a basic set and
4800 * a list of affine expressions representing the relation between
4801 * the input and output. The space over which the affine expressions
4802 * are defined is the same as that of the domain. The number of
4803 * affine expressions in the list is equal to the number of output variables.
4805 static void sol_for_add(struct isl_sol_for *sol,
4806 struct isl_basic_set *dom, struct isl_mat *M)
4808 int i;
4809 isl_ctx *ctx;
4810 isl_local_space *ls;
4811 isl_aff *aff;
4812 isl_aff_list *list;
4814 if (sol->sol.error || !dom || !M)
4815 goto error;
4817 ctx = isl_basic_set_get_ctx(dom);
4818 ls = isl_basic_set_get_local_space(dom);
4819 list = isl_aff_list_alloc(ctx, M->n_row - 1);
4820 for (i = 1; i < M->n_row; ++i) {
4821 aff = isl_aff_alloc(isl_local_space_copy(ls));
4822 if (aff) {
4823 isl_int_set(aff->v->el[0], M->row[0][0]);
4824 isl_seq_cpy(aff->v->el + 1, M->row[i], M->n_col);
4826 aff = isl_aff_normalize(aff);
4827 list = isl_aff_list_add(list, aff);
4829 isl_local_space_free(ls);
4831 dom = isl_basic_set_finalize(dom);
4833 if (sol->fn(isl_basic_set_copy(dom), list, sol->user) < 0)
4834 goto error;
4836 isl_basic_set_free(dom);
4837 isl_mat_free(M);
4838 return;
4839 error:
4840 isl_basic_set_free(dom);
4841 isl_mat_free(M);
4842 sol->sol.error = 1;
4845 static void sol_for_add_wrap(struct isl_sol *sol,
4846 struct isl_basic_set *dom, struct isl_mat *M)
4848 sol_for_add((struct isl_sol_for *)sol, dom, M);
4851 static struct isl_sol_for *sol_for_init(struct isl_basic_map *bmap, int max,
4852 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4853 void *user),
4854 void *user)
4856 struct isl_sol_for *sol_for = NULL;
4857 isl_space *dom_dim;
4858 struct isl_basic_set *dom = NULL;
4860 sol_for = isl_calloc_type(bmap->ctx, struct isl_sol_for);
4861 if (!sol_for)
4862 goto error;
4864 dom_dim = isl_space_domain(isl_space_copy(bmap->dim));
4865 dom = isl_basic_set_universe(dom_dim);
4867 sol_for->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
4868 sol_for->sol.dec_level.callback.run = &sol_dec_level_wrap;
4869 sol_for->sol.dec_level.sol = &sol_for->sol;
4870 sol_for->fn = fn;
4871 sol_for->user = user;
4872 sol_for->sol.max = max;
4873 sol_for->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
4874 sol_for->sol.add = &sol_for_add_wrap;
4875 sol_for->sol.add_empty = NULL;
4876 sol_for->sol.free = &sol_for_free_wrap;
4878 sol_for->sol.context = isl_context_alloc(dom);
4879 if (!sol_for->sol.context)
4880 goto error;
4882 isl_basic_set_free(dom);
4883 return sol_for;
4884 error:
4885 isl_basic_set_free(dom);
4886 sol_for_free(sol_for);
4887 return NULL;
4890 static void sol_for_find_solutions(struct isl_sol_for *sol_for,
4891 struct isl_tab *tab)
4893 find_solutions_main(&sol_for->sol, tab);
4896 int isl_basic_map_foreach_lexopt(__isl_keep isl_basic_map *bmap, int max,
4897 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4898 void *user),
4899 void *user)
4901 struct isl_sol_for *sol_for = NULL;
4903 bmap = isl_basic_map_copy(bmap);
4904 bmap = isl_basic_map_detect_equalities(bmap);
4905 if (!bmap)
4906 return -1;
4908 sol_for = sol_for_init(bmap, max, fn, user);
4909 if (!sol_for)
4910 goto error;
4912 if (isl_basic_map_plain_is_empty(bmap))
4913 /* nothing */;
4914 else {
4915 struct isl_tab *tab;
4916 struct isl_context *context = sol_for->sol.context;
4917 tab = tab_for_lexmin(bmap,
4918 context->op->peek_basic_set(context), 1, max);
4919 tab = context->op->detect_nonnegative_parameters(context, tab);
4920 sol_for_find_solutions(sol_for, tab);
4921 if (sol_for->sol.error)
4922 goto error;
4925 sol_free(&sol_for->sol);
4926 isl_basic_map_free(bmap);
4927 return 0;
4928 error:
4929 sol_free(&sol_for->sol);
4930 isl_basic_map_free(bmap);
4931 return -1;
4934 int isl_basic_set_foreach_lexopt(__isl_keep isl_basic_set *bset, int max,
4935 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4936 void *user),
4937 void *user)
4939 return isl_basic_map_foreach_lexopt(bset, max, fn, user);
4942 /* Check if the given sequence of len variables starting at pos
4943 * represents a trivial (i.e., zero) solution.
4944 * The variables are assumed to be non-negative and to come in pairs,
4945 * with each pair representing a variable of unrestricted sign.
4946 * The solution is trivial if each such pair in the sequence consists
4947 * of two identical values, meaning that the variable being represented
4948 * has value zero.
4950 static int region_is_trivial(struct isl_tab *tab, int pos, int len)
4952 int i;
4954 if (len == 0)
4955 return 0;
4957 for (i = 0; i < len; i += 2) {
4958 int neg_row;
4959 int pos_row;
4961 neg_row = tab->var[pos + i].is_row ?
4962 tab->var[pos + i].index : -1;
4963 pos_row = tab->var[pos + i + 1].is_row ?
4964 tab->var[pos + i + 1].index : -1;
4966 if ((neg_row < 0 ||
4967 isl_int_is_zero(tab->mat->row[neg_row][1])) &&
4968 (pos_row < 0 ||
4969 isl_int_is_zero(tab->mat->row[pos_row][1])))
4970 continue;
4972 if (neg_row < 0 || pos_row < 0)
4973 return 0;
4974 if (isl_int_ne(tab->mat->row[neg_row][1],
4975 tab->mat->row[pos_row][1]))
4976 return 0;
4979 return 1;
4982 /* Return the index of the first trivial region or -1 if all regions
4983 * are non-trivial.
4985 static int first_trivial_region(struct isl_tab *tab,
4986 int n_region, struct isl_region *region)
4988 int i;
4990 for (i = 0; i < n_region; ++i) {
4991 if (region_is_trivial(tab, region[i].pos, region[i].len))
4992 return i;
4995 return -1;
4998 /* Check if the solution is optimal, i.e., whether the first
4999 * n_op entries are zero.
5001 static int is_optimal(__isl_keep isl_vec *sol, int n_op)
5003 int i;
5005 for (i = 0; i < n_op; ++i)
5006 if (!isl_int_is_zero(sol->el[1 + i]))
5007 return 0;
5008 return 1;
5011 /* Add constraints to "tab" that ensure that any solution is significantly
5012 * better than that represented by "sol". That is, find the first
5013 * relevant (within first n_op) non-zero coefficient and force it (along
5014 * with all previous coefficients) to be zero.
5015 * If the solution is already optimal (all relevant coefficients are zero),
5016 * then just mark the table as empty.
5018 * This function assumes that at least 2 * n_op more rows and at least
5019 * 2 * n_op more elements in the constraint array are available in the tableau.
5021 static int force_better_solution(struct isl_tab *tab,
5022 __isl_keep isl_vec *sol, int n_op)
5024 int i;
5025 isl_ctx *ctx;
5026 isl_vec *v = NULL;
5028 if (!sol)
5029 return -1;
5031 for (i = 0; i < n_op; ++i)
5032 if (!isl_int_is_zero(sol->el[1 + i]))
5033 break;
5035 if (i == n_op) {
5036 if (isl_tab_mark_empty(tab) < 0)
5037 return -1;
5038 return 0;
5041 ctx = isl_vec_get_ctx(sol);
5042 v = isl_vec_alloc(ctx, 1 + tab->n_var);
5043 if (!v)
5044 return -1;
5046 for (; i >= 0; --i) {
5047 v = isl_vec_clr(v);
5048 isl_int_set_si(v->el[1 + i], -1);
5049 if (add_lexmin_eq(tab, v->el) < 0)
5050 goto error;
5053 isl_vec_free(v);
5054 return 0;
5055 error:
5056 isl_vec_free(v);
5057 return -1;
5060 struct isl_trivial {
5061 int update;
5062 int region;
5063 int side;
5064 struct isl_tab_undo *snap;
5067 /* Return the lexicographically smallest non-trivial solution of the
5068 * given ILP problem.
5070 * All variables are assumed to be non-negative.
5072 * n_op is the number of initial coordinates to optimize.
5073 * That is, once a solution has been found, we will only continue looking
5074 * for solution that result in significantly better values for those
5075 * initial coordinates. That is, we only continue looking for solutions
5076 * that increase the number of initial zeros in this sequence.
5078 * A solution is non-trivial, if it is non-trivial on each of the
5079 * specified regions. Each region represents a sequence of pairs
5080 * of variables. A solution is non-trivial on such a region if
5081 * at least one of these pairs consists of different values, i.e.,
5082 * such that the non-negative variable represented by the pair is non-zero.
5084 * Whenever a conflict is encountered, all constraints involved are
5085 * reported to the caller through a call to "conflict".
5087 * We perform a simple branch-and-bound backtracking search.
5088 * Each level in the search represents initially trivial region that is forced
5089 * to be non-trivial.
5090 * At each level we consider n cases, where n is the length of the region.
5091 * In terms of the n/2 variables of unrestricted signs being encoded by
5092 * the region, we consider the cases
5093 * x_0 >= 1
5094 * x_0 <= -1
5095 * x_0 = 0 and x_1 >= 1
5096 * x_0 = 0 and x_1 <= -1
5097 * x_0 = 0 and x_1 = 0 and x_2 >= 1
5098 * x_0 = 0 and x_1 = 0 and x_2 <= -1
5099 * ...
5100 * The cases are considered in this order, assuming that each pair
5101 * x_i_a x_i_b represents the value x_i_b - x_i_a.
5102 * That is, x_0 >= 1 is enforced by adding the constraint
5103 * x_0_b - x_0_a >= 1
5105 __isl_give isl_vec *isl_tab_basic_set_non_trivial_lexmin(
5106 __isl_take isl_basic_set *bset, int n_op, int n_region,
5107 struct isl_region *region,
5108 int (*conflict)(int con, void *user), void *user)
5110 int i, j;
5111 int r;
5112 isl_ctx *ctx;
5113 isl_vec *v = NULL;
5114 isl_vec *sol = NULL;
5115 struct isl_tab *tab;
5116 struct isl_trivial *triv = NULL;
5117 int level, init;
5119 if (!bset)
5120 return NULL;
5122 ctx = isl_basic_set_get_ctx(bset);
5123 sol = isl_vec_alloc(ctx, 0);
5125 tab = tab_for_lexmin(bset, NULL, 0, 0);
5126 if (!tab)
5127 goto error;
5128 tab->conflict = conflict;
5129 tab->conflict_user = user;
5131 v = isl_vec_alloc(ctx, 1 + tab->n_var);
5132 triv = isl_calloc_array(ctx, struct isl_trivial, n_region);
5133 if (!v || (n_region && !triv))
5134 goto error;
5136 level = 0;
5137 init = 1;
5139 while (level >= 0) {
5140 int side, base;
5142 if (init) {
5143 tab = cut_to_integer_lexmin(tab, CUT_ONE);
5144 if (!tab)
5145 goto error;
5146 if (tab->empty)
5147 goto backtrack;
5148 r = first_trivial_region(tab, n_region, region);
5149 if (r < 0) {
5150 for (i = 0; i < level; ++i)
5151 triv[i].update = 1;
5152 isl_vec_free(sol);
5153 sol = isl_tab_get_sample_value(tab);
5154 if (!sol)
5155 goto error;
5156 if (is_optimal(sol, n_op))
5157 break;
5158 goto backtrack;
5160 if (level >= n_region)
5161 isl_die(ctx, isl_error_internal,
5162 "nesting level too deep", goto error);
5163 if (isl_tab_extend_cons(tab,
5164 2 * region[r].len + 2 * n_op) < 0)
5165 goto error;
5166 triv[level].region = r;
5167 triv[level].side = 0;
5170 r = triv[level].region;
5171 side = triv[level].side;
5172 base = 2 * (side/2);
5174 if (side >= region[r].len) {
5175 backtrack:
5176 level--;
5177 init = 0;
5178 if (level >= 0)
5179 if (isl_tab_rollback(tab, triv[level].snap) < 0)
5180 goto error;
5181 continue;
5184 if (triv[level].update) {
5185 if (force_better_solution(tab, sol, n_op) < 0)
5186 goto error;
5187 triv[level].update = 0;
5190 if (side == base && base >= 2) {
5191 for (j = base - 2; j < base; ++j) {
5192 v = isl_vec_clr(v);
5193 isl_int_set_si(v->el[1 + region[r].pos + j], 1);
5194 if (add_lexmin_eq(tab, v->el) < 0)
5195 goto error;
5199 triv[level].snap = isl_tab_snap(tab);
5200 if (isl_tab_push_basis(tab) < 0)
5201 goto error;
5203 v = isl_vec_clr(v);
5204 isl_int_set_si(v->el[0], -1);
5205 isl_int_set_si(v->el[1 + region[r].pos + side], -1);
5206 isl_int_set_si(v->el[1 + region[r].pos + (side ^ 1)], 1);
5207 tab = add_lexmin_ineq(tab, v->el);
5209 triv[level].side++;
5210 level++;
5211 init = 1;
5214 free(triv);
5215 isl_vec_free(v);
5216 isl_tab_free(tab);
5217 isl_basic_set_free(bset);
5219 return sol;
5220 error:
5221 free(triv);
5222 isl_vec_free(v);
5223 isl_tab_free(tab);
5224 isl_basic_set_free(bset);
5225 isl_vec_free(sol);
5226 return NULL;
5229 /* Wrapper for a tableau that is used for computing
5230 * the lexicographically smallest rational point of a non-negative set.
5231 * This point is represented by the sample value of "tab",
5232 * unless "tab" is empty.
5234 struct isl_tab_lexmin {
5235 isl_ctx *ctx;
5236 struct isl_tab *tab;
5239 /* Free "tl" and return NULL.
5241 __isl_null isl_tab_lexmin *isl_tab_lexmin_free(__isl_take isl_tab_lexmin *tl)
5243 if (!tl)
5244 return NULL;
5245 isl_ctx_deref(tl->ctx);
5246 isl_tab_free(tl->tab);
5247 free(tl);
5249 return NULL;
5252 /* Construct an isl_tab_lexmin for computing
5253 * the lexicographically smallest rational point in "bset",
5254 * assuming that all variables are non-negative.
5256 __isl_give isl_tab_lexmin *isl_tab_lexmin_from_basic_set(
5257 __isl_take isl_basic_set *bset)
5259 isl_ctx *ctx;
5260 isl_tab_lexmin *tl;
5262 if (!bset)
5263 return NULL;
5265 ctx = isl_basic_set_get_ctx(bset);
5266 tl = isl_calloc_type(ctx, struct isl_tab_lexmin);
5267 if (!tl)
5268 goto error;
5269 tl->ctx = ctx;
5270 isl_ctx_ref(ctx);
5271 tl->tab = tab_for_lexmin(bset, NULL, 0, 0);
5272 isl_basic_set_free(bset);
5273 if (!tl->tab)
5274 return isl_tab_lexmin_free(tl);
5275 return tl;
5276 error:
5277 isl_basic_set_free(bset);
5278 isl_tab_lexmin_free(tl);
5279 return NULL;
5282 /* Return the dimension of the set represented by "tl".
5284 int isl_tab_lexmin_dim(__isl_keep isl_tab_lexmin *tl)
5286 return tl ? tl->tab->n_var : -1;
5289 /* Add the equality with coefficients "eq" to "tl", updating the optimal
5290 * solution if needed.
5291 * The equality is added as two opposite inequality constraints.
5293 __isl_give isl_tab_lexmin *isl_tab_lexmin_add_eq(__isl_take isl_tab_lexmin *tl,
5294 isl_int *eq)
5296 unsigned n_var;
5298 if (!tl || !eq)
5299 return isl_tab_lexmin_free(tl);
5301 if (isl_tab_extend_cons(tl->tab, 2) < 0)
5302 return isl_tab_lexmin_free(tl);
5303 n_var = tl->tab->n_var;
5304 isl_seq_neg(eq, eq, 1 + n_var);
5305 tl->tab = add_lexmin_ineq(tl->tab, eq);
5306 isl_seq_neg(eq, eq, 1 + n_var);
5307 tl->tab = add_lexmin_ineq(tl->tab, eq);
5309 if (!tl->tab)
5310 return isl_tab_lexmin_free(tl);
5312 return tl;
5315 /* Return the lexicographically smallest rational point in the basic set
5316 * from which "tl" was constructed.
5317 * If the original input was empty, then return a zero-length vector.
5319 __isl_give isl_vec *isl_tab_lexmin_get_solution(__isl_keep isl_tab_lexmin *tl)
5321 if (!tl)
5322 return NULL;
5323 if (tl->tab->empty)
5324 return isl_vec_alloc(tl->ctx, 0);
5325 else
5326 return isl_tab_get_sample_value(tl->tab);
5329 /* Return the lexicographically smallest rational point in "bset",
5330 * assuming that all variables are non-negative.
5331 * If "bset" is empty, then return a zero-length vector.
5333 __isl_give isl_vec *isl_tab_basic_set_non_neg_lexmin(
5334 __isl_take isl_basic_set *bset)
5336 isl_tab_lexmin *tl;
5337 isl_vec *sol;
5339 tl = isl_tab_lexmin_from_basic_set(bset);
5340 sol = isl_tab_lexmin_get_solution(tl);
5341 isl_tab_lexmin_free(tl);
5342 return sol;
5345 struct isl_sol_pma {
5346 struct isl_sol sol;
5347 isl_pw_multi_aff *pma;
5348 isl_set *empty;
5351 static void sol_pma_free(struct isl_sol_pma *sol_pma)
5353 if (!sol_pma)
5354 return;
5355 if (sol_pma->sol.context)
5356 sol_pma->sol.context->op->free(sol_pma->sol.context);
5357 isl_pw_multi_aff_free(sol_pma->pma);
5358 isl_set_free(sol_pma->empty);
5359 free(sol_pma);
5362 /* This function is called for parts of the context where there is
5363 * no solution, with "bset" corresponding to the context tableau.
5364 * Simply add the basic set to the set "empty".
5366 static void sol_pma_add_empty(struct isl_sol_pma *sol,
5367 __isl_take isl_basic_set *bset)
5369 if (!bset || !sol->empty)
5370 goto error;
5372 sol->empty = isl_set_grow(sol->empty, 1);
5373 bset = isl_basic_set_simplify(bset);
5374 bset = isl_basic_set_finalize(bset);
5375 sol->empty = isl_set_add_basic_set(sol->empty, bset);
5376 if (!sol->empty)
5377 sol->sol.error = 1;
5378 return;
5379 error:
5380 isl_basic_set_free(bset);
5381 sol->sol.error = 1;
5384 /* Given a basic map "dom" that represents the context and an affine
5385 * matrix "M" that maps the dimensions of the context to the
5386 * output variables, construct an isl_pw_multi_aff with a single
5387 * cell corresponding to "dom" and affine expressions copied from "M".
5389 static void sol_pma_add(struct isl_sol_pma *sol,
5390 __isl_take isl_basic_set *dom, __isl_take isl_mat *M)
5392 int i;
5393 isl_local_space *ls;
5394 isl_aff *aff;
5395 isl_multi_aff *maff;
5396 isl_pw_multi_aff *pma;
5398 maff = isl_multi_aff_alloc(isl_pw_multi_aff_get_space(sol->pma));
5399 ls = isl_basic_set_get_local_space(dom);
5400 for (i = 1; i < M->n_row; ++i) {
5401 aff = isl_aff_alloc(isl_local_space_copy(ls));
5402 if (aff) {
5403 isl_int_set(aff->v->el[0], M->row[0][0]);
5404 isl_seq_cpy(aff->v->el + 1, M->row[i], M->n_col);
5406 aff = isl_aff_normalize(aff);
5407 maff = isl_multi_aff_set_aff(maff, i - 1, aff);
5409 isl_local_space_free(ls);
5410 isl_mat_free(M);
5411 dom = isl_basic_set_simplify(dom);
5412 dom = isl_basic_set_finalize(dom);
5413 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(dom), maff);
5414 sol->pma = isl_pw_multi_aff_add_disjoint(sol->pma, pma);
5415 if (!sol->pma)
5416 sol->sol.error = 1;
5419 static void sol_pma_free_wrap(struct isl_sol *sol)
5421 sol_pma_free((struct isl_sol_pma *)sol);
5424 static void sol_pma_add_empty_wrap(struct isl_sol *sol,
5425 __isl_take isl_basic_set *bset)
5427 sol_pma_add_empty((struct isl_sol_pma *)sol, bset);
5430 static void sol_pma_add_wrap(struct isl_sol *sol,
5431 __isl_take isl_basic_set *dom, __isl_take isl_mat *M)
5433 sol_pma_add((struct isl_sol_pma *)sol, dom, M);
5436 /* Construct an isl_sol_pma structure for accumulating the solution.
5437 * If track_empty is set, then we also keep track of the parts
5438 * of the context where there is no solution.
5439 * If max is set, then we are solving a maximization, rather than
5440 * a minimization problem, which means that the variables in the
5441 * tableau have value "M - x" rather than "M + x".
5443 static struct isl_sol *sol_pma_init(__isl_keep isl_basic_map *bmap,
5444 __isl_take isl_basic_set *dom, int track_empty, int max)
5446 struct isl_sol_pma *sol_pma = NULL;
5448 if (!bmap)
5449 goto error;
5451 sol_pma = isl_calloc_type(bmap->ctx, struct isl_sol_pma);
5452 if (!sol_pma)
5453 goto error;
5455 sol_pma->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
5456 sol_pma->sol.dec_level.callback.run = &sol_dec_level_wrap;
5457 sol_pma->sol.dec_level.sol = &sol_pma->sol;
5458 sol_pma->sol.max = max;
5459 sol_pma->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
5460 sol_pma->sol.add = &sol_pma_add_wrap;
5461 sol_pma->sol.add_empty = track_empty ? &sol_pma_add_empty_wrap : NULL;
5462 sol_pma->sol.free = &sol_pma_free_wrap;
5463 sol_pma->pma = isl_pw_multi_aff_empty(isl_basic_map_get_space(bmap));
5464 if (!sol_pma->pma)
5465 goto error;
5467 sol_pma->sol.context = isl_context_alloc(dom);
5468 if (!sol_pma->sol.context)
5469 goto error;
5471 if (track_empty) {
5472 sol_pma->empty = isl_set_alloc_space(isl_basic_set_get_space(dom),
5473 1, ISL_SET_DISJOINT);
5474 if (!sol_pma->empty)
5475 goto error;
5478 isl_basic_set_free(dom);
5479 return &sol_pma->sol;
5480 error:
5481 isl_basic_set_free(dom);
5482 sol_pma_free(sol_pma);
5483 return NULL;
5486 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
5487 * some obvious symmetries.
5489 * We call basic_map_partial_lexopt_base and extract the results.
5491 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_base_pma(
5492 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5493 __isl_give isl_set **empty, int max)
5495 isl_pw_multi_aff *result = NULL;
5496 struct isl_sol *sol;
5497 struct isl_sol_pma *sol_pma;
5499 sol = basic_map_partial_lexopt_base(bmap, dom, empty, max,
5500 &sol_pma_init);
5501 if (!sol)
5502 return NULL;
5503 sol_pma = (struct isl_sol_pma *) sol;
5505 result = isl_pw_multi_aff_copy(sol_pma->pma);
5506 if (empty)
5507 *empty = isl_set_copy(sol_pma->empty);
5508 sol_free(&sol_pma->sol);
5509 return result;
5512 /* Given that the last input variable of "maff" represents the minimum
5513 * of some bounds, check whether we need to plug in the expression
5514 * of the minimum.
5516 * In particular, check if the last input variable appears in any
5517 * of the expressions in "maff".
5519 static int need_substitution(__isl_keep isl_multi_aff *maff)
5521 int i;
5522 unsigned pos;
5524 pos = isl_multi_aff_dim(maff, isl_dim_in) - 1;
5526 for (i = 0; i < maff->n; ++i)
5527 if (isl_aff_involves_dims(maff->p[i], isl_dim_in, pos, 1))
5528 return 1;
5530 return 0;
5533 /* Given a set of upper bounds on the last "input" variable m,
5534 * construct a piecewise affine expression that selects
5535 * the minimal upper bound to m, i.e.,
5536 * divide the space into cells where one
5537 * of the upper bounds is smaller than all the others and select
5538 * this upper bound on that cell.
5540 * In particular, if there are n bounds b_i, then the result
5541 * consists of n cell, each one of the form
5543 * b_i <= b_j for j > i
5544 * b_i < b_j for j < i
5546 * The affine expression on this cell is
5548 * b_i
5550 static __isl_give isl_pw_aff *set_minimum_pa(__isl_take isl_space *space,
5551 __isl_take isl_mat *var)
5553 int i;
5554 isl_aff *aff = NULL;
5555 isl_basic_set *bset = NULL;
5556 isl_pw_aff *paff = NULL;
5557 isl_space *pw_space;
5558 isl_local_space *ls = NULL;
5560 if (!space || !var)
5561 goto error;
5563 ls = isl_local_space_from_space(isl_space_copy(space));
5564 pw_space = isl_space_copy(space);
5565 pw_space = isl_space_from_domain(pw_space);
5566 pw_space = isl_space_add_dims(pw_space, isl_dim_out, 1);
5567 paff = isl_pw_aff_alloc_size(pw_space, var->n_row);
5569 for (i = 0; i < var->n_row; ++i) {
5570 isl_pw_aff *paff_i;
5572 aff = isl_aff_alloc(isl_local_space_copy(ls));
5573 bset = isl_basic_set_alloc_space(isl_space_copy(space), 0,
5574 0, var->n_row - 1);
5575 if (!aff || !bset)
5576 goto error;
5577 isl_int_set_si(aff->v->el[0], 1);
5578 isl_seq_cpy(aff->v->el + 1, var->row[i], var->n_col);
5579 isl_int_set_si(aff->v->el[1 + var->n_col], 0);
5580 bset = select_minimum(bset, var, i);
5581 paff_i = isl_pw_aff_alloc(isl_set_from_basic_set(bset), aff);
5582 paff = isl_pw_aff_add_disjoint(paff, paff_i);
5585 isl_local_space_free(ls);
5586 isl_space_free(space);
5587 isl_mat_free(var);
5588 return paff;
5589 error:
5590 isl_aff_free(aff);
5591 isl_basic_set_free(bset);
5592 isl_pw_aff_free(paff);
5593 isl_local_space_free(ls);
5594 isl_space_free(space);
5595 isl_mat_free(var);
5596 return NULL;
5599 /* Given a piecewise multi-affine expression of which the last input variable
5600 * is the minimum of the bounds in "cst", plug in the value of the minimum.
5601 * This minimum expression is given in "min_expr_pa".
5602 * The set "min_expr" contains the same information, but in the form of a set.
5603 * The variable is subsequently projected out.
5605 * The implementation is similar to those of "split" and "split_domain".
5606 * If the variable appears in a given expression, then minimum expression
5607 * is plugged in. Otherwise, if the variable appears in the constraints
5608 * and a split is required, then the domain is split. Otherwise, no split
5609 * is performed.
5611 static __isl_give isl_pw_multi_aff *split_domain_pma(
5612 __isl_take isl_pw_multi_aff *opt, __isl_take isl_pw_aff *min_expr_pa,
5613 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
5615 int n_in;
5616 int i;
5617 isl_space *space;
5618 isl_pw_multi_aff *res;
5620 if (!opt || !min_expr || !cst)
5621 goto error;
5623 n_in = isl_pw_multi_aff_dim(opt, isl_dim_in);
5624 space = isl_pw_multi_aff_get_space(opt);
5625 space = isl_space_drop_dims(space, isl_dim_in, n_in - 1, 1);
5626 res = isl_pw_multi_aff_empty(space);
5628 for (i = 0; i < opt->n; ++i) {
5629 isl_pw_multi_aff *pma;
5631 pma = isl_pw_multi_aff_alloc(isl_set_copy(opt->p[i].set),
5632 isl_multi_aff_copy(opt->p[i].maff));
5633 if (need_substitution(opt->p[i].maff))
5634 pma = isl_pw_multi_aff_substitute(pma,
5635 isl_dim_in, n_in - 1, min_expr_pa);
5636 else if (need_split_set(opt->p[i].set, cst))
5637 pma = isl_pw_multi_aff_intersect_domain(pma,
5638 isl_set_copy(min_expr));
5639 pma = isl_pw_multi_aff_project_out(pma,
5640 isl_dim_in, n_in - 1, 1);
5642 res = isl_pw_multi_aff_add_disjoint(res, pma);
5645 isl_pw_multi_aff_free(opt);
5646 isl_pw_aff_free(min_expr_pa);
5647 isl_set_free(min_expr);
5648 isl_mat_free(cst);
5649 return res;
5650 error:
5651 isl_pw_multi_aff_free(opt);
5652 isl_pw_aff_free(min_expr_pa);
5653 isl_set_free(min_expr);
5654 isl_mat_free(cst);
5655 return NULL;
5658 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_pma(
5659 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5660 __isl_give isl_set **empty, int max);
5662 /* This function is called from basic_map_partial_lexopt_symm.
5663 * The last variable of "bmap" and "dom" corresponds to the minimum
5664 * of the bounds in "cst". "map_space" is the space of the original
5665 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
5666 * is the space of the original domain.
5668 * We recursively call basic_map_partial_lexopt and then plug in
5669 * the definition of the minimum in the result.
5671 static __isl_give union isl_lex_res basic_map_partial_lexopt_symm_pma_core(
5672 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5673 __isl_give isl_set **empty, int max, __isl_take isl_mat *cst,
5674 __isl_take isl_space *map_space, __isl_take isl_space *set_space)
5676 isl_pw_multi_aff *opt;
5677 isl_pw_aff *min_expr_pa;
5678 isl_set *min_expr;
5679 union isl_lex_res res;
5681 min_expr = set_minimum(isl_basic_set_get_space(dom), isl_mat_copy(cst));
5682 min_expr_pa = set_minimum_pa(isl_basic_set_get_space(dom),
5683 isl_mat_copy(cst));
5685 opt = basic_map_partial_lexopt_pma(bmap, dom, empty, max);
5687 if (empty) {
5688 *empty = split(*empty,
5689 isl_set_copy(min_expr), isl_mat_copy(cst));
5690 *empty = isl_set_reset_space(*empty, set_space);
5693 opt = split_domain_pma(opt, min_expr_pa, min_expr, cst);
5694 opt = isl_pw_multi_aff_reset_space(opt, map_space);
5696 res.pma = opt;
5697 return res;
5700 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_symm_pma(
5701 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5702 __isl_give isl_set **empty, int max, int first, int second)
5704 return basic_map_partial_lexopt_symm(bmap, dom, empty, max,
5705 first, second, &basic_map_partial_lexopt_symm_pma_core).pma;
5708 /* Recursive part of isl_basic_map_partial_lexopt_pw_multi_aff, after detecting
5709 * equalities and removing redundant constraints.
5711 * We first check if there are any parallel constraints (left).
5712 * If not, we are in the base case.
5713 * If there are parallel constraints, we replace them by a single
5714 * constraint in basic_map_partial_lexopt_symm_pma and then call
5715 * this function recursively to look for more parallel constraints.
5717 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_pma(
5718 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5719 __isl_give isl_set **empty, int max)
5721 int par = 0;
5722 int first, second;
5724 if (!bmap)
5725 goto error;
5727 if (bmap->ctx->opt->pip_symmetry)
5728 par = parallel_constraints(bmap, &first, &second);
5729 if (par < 0)
5730 goto error;
5731 if (!par)
5732 return basic_map_partial_lexopt_base_pma(bmap, dom, empty, max);
5734 return basic_map_partial_lexopt_symm_pma(bmap, dom, empty, max,
5735 first, second);
5736 error:
5737 isl_basic_set_free(dom);
5738 isl_basic_map_free(bmap);
5739 return NULL;
5742 /* Compute the lexicographic minimum (or maximum if "max" is set)
5743 * of "bmap" over the domain "dom" and return the result as a piecewise
5744 * multi-affine expression.
5745 * If "empty" is not NULL, then *empty is assigned a set that
5746 * contains those parts of the domain where there is no solution.
5747 * If "bmap" is marked as rational (ISL_BASIC_MAP_RATIONAL),
5748 * then we compute the rational optimum. Otherwise, we compute
5749 * the integral optimum.
5751 * We perform some preprocessing. As the PILP solver does not
5752 * handle implicit equalities very well, we first make sure all
5753 * the equalities are explicitly available.
5755 * We also add context constraints to the basic map and remove
5756 * redundant constraints. This is only needed because of the
5757 * way we handle simple symmetries. In particular, we currently look
5758 * for symmetries on the constraints, before we set up the main tableau.
5759 * It is then no good to look for symmetries on possibly redundant constraints.
5761 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexopt_pw_multi_aff(
5762 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5763 __isl_give isl_set **empty, int max)
5765 if (empty)
5766 *empty = NULL;
5767 if (!bmap || !dom)
5768 goto error;
5770 isl_assert(bmap->ctx,
5771 isl_basic_map_compatible_domain(bmap, dom), goto error);
5773 if (isl_basic_set_dim(dom, isl_dim_all) == 0)
5774 return basic_map_partial_lexopt_pma(bmap, dom, empty, max);
5776 bmap = isl_basic_map_intersect_domain(bmap, isl_basic_set_copy(dom));
5777 bmap = isl_basic_map_detect_equalities(bmap);
5778 bmap = isl_basic_map_remove_redundancies(bmap);
5780 return basic_map_partial_lexopt_pma(bmap, dom, empty, max);
5781 error:
5782 isl_basic_set_free(dom);
5783 isl_basic_map_free(bmap);
5784 return NULL;