isl_*_partial_lex{min,max}_pw_multi_aff: fix handling of existentials
[isl.git] / isl_tab_pip.c
blob40aeac53758bd949278967fb8e9d265cd358a07b
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2016 Sven Verdoolaege
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
14 #include <isl_ctx_private.h>
15 #include "isl_map_private.h"
16 #include <isl_seq.h>
17 #include "isl_tab.h"
18 #include "isl_sample.h"
19 #include <isl_mat_private.h>
20 #include <isl_vec_private.h>
21 #include <isl_aff_private.h>
22 #include <isl_constraint_private.h>
23 #include <isl_options_private.h>
24 #include <isl_config.h>
27 * The implementation of parametric integer linear programming in this file
28 * was inspired by the paper "Parametric Integer Programming" and the
29 * report "Solving systems of affine (in)equalities" by Paul Feautrier
30 * (and others).
32 * The strategy used for obtaining a feasible solution is different
33 * from the one used in isl_tab.c. In particular, in isl_tab.c,
34 * upon finding a constraint that is not yet satisfied, we pivot
35 * in a row that increases the constant term of the row holding the
36 * constraint, making sure the sample solution remains feasible
37 * for all the constraints it already satisfied.
38 * Here, we always pivot in the row holding the constraint,
39 * choosing a column that induces the lexicographically smallest
40 * increment to the sample solution.
42 * By starting out from a sample value that is lexicographically
43 * smaller than any integer point in the problem space, the first
44 * feasible integer sample point we find will also be the lexicographically
45 * smallest. If all variables can be assumed to be non-negative,
46 * then the initial sample value may be chosen equal to zero.
47 * However, we will not make this assumption. Instead, we apply
48 * the "big parameter" trick. Any variable x is then not directly
49 * used in the tableau, but instead it is represented by another
50 * variable x' = M + x, where M is an arbitrarily large (positive)
51 * value. x' is therefore always non-negative, whatever the value of x.
52 * Taking as initial sample value x' = 0 corresponds to x = -M,
53 * which is always smaller than any possible value of x.
55 * The big parameter trick is used in the main tableau and
56 * also in the context tableau if isl_context_lex is used.
57 * In this case, each tableaus has its own big parameter.
58 * Before doing any real work, we check if all the parameters
59 * happen to be non-negative. If so, we drop the column corresponding
60 * to M from the initial context tableau.
61 * If isl_context_gbr is used, then the big parameter trick is only
62 * used in the main tableau.
65 struct isl_context;
66 struct isl_context_op {
67 /* detect nonnegative parameters in context and mark them in tab */
68 struct isl_tab *(*detect_nonnegative_parameters)(
69 struct isl_context *context, struct isl_tab *tab);
70 /* return temporary reference to basic set representation of context */
71 struct isl_basic_set *(*peek_basic_set)(struct isl_context *context);
72 /* return temporary reference to tableau representation of context */
73 struct isl_tab *(*peek_tab)(struct isl_context *context);
74 /* add equality; check is 1 if eq may not be valid;
75 * update is 1 if we may want to call ineq_sign on context later.
77 void (*add_eq)(struct isl_context *context, isl_int *eq,
78 int check, int update);
79 /* add inequality; check is 1 if ineq may not be valid;
80 * update is 1 if we may want to call ineq_sign on context later.
82 void (*add_ineq)(struct isl_context *context, isl_int *ineq,
83 int check, int update);
84 /* check sign of ineq based on previous information.
85 * strict is 1 if saturation should be treated as a positive sign.
87 enum isl_tab_row_sign (*ineq_sign)(struct isl_context *context,
88 isl_int *ineq, int strict);
89 /* check if inequality maintains feasibility */
90 int (*test_ineq)(struct isl_context *context, isl_int *ineq);
91 /* return index of a div that corresponds to "div" */
92 int (*get_div)(struct isl_context *context, struct isl_tab *tab,
93 struct isl_vec *div);
94 /* insert div "div" to context at "pos" and return non-negativity */
95 isl_bool (*insert_div)(struct isl_context *context, int pos,
96 __isl_keep isl_vec *div);
97 int (*detect_equalities)(struct isl_context *context,
98 struct isl_tab *tab);
99 /* return row index of "best" split */
100 int (*best_split)(struct isl_context *context, struct isl_tab *tab);
101 /* check if context has already been determined to be empty */
102 int (*is_empty)(struct isl_context *context);
103 /* check if context is still usable */
104 int (*is_ok)(struct isl_context *context);
105 /* save a copy/snapshot of context */
106 void *(*save)(struct isl_context *context);
107 /* restore saved context */
108 void (*restore)(struct isl_context *context, void *);
109 /* discard saved context */
110 void (*discard)(void *);
111 /* invalidate context */
112 void (*invalidate)(struct isl_context *context);
113 /* free context */
114 __isl_null struct isl_context *(*free)(struct isl_context *context);
117 /* Shared parts of context representation.
119 * "n_unknown" is the number of final unknown integer divisions
120 * in the input domain.
122 struct isl_context {
123 struct isl_context_op *op;
124 int n_unknown;
127 struct isl_context_lex {
128 struct isl_context context;
129 struct isl_tab *tab;
132 /* A stack (linked list) of solutions of subtrees of the search space.
134 * "M" describes the solution in terms of the dimensions of "dom".
135 * The number of columns of "M" is one more than the total number
136 * of dimensions of "dom".
138 * If "M" is NULL, then there is no solution on "dom".
140 struct isl_partial_sol {
141 int level;
142 struct isl_basic_set *dom;
143 struct isl_mat *M;
145 struct isl_partial_sol *next;
148 struct isl_sol;
149 struct isl_sol_callback {
150 struct isl_tab_callback callback;
151 struct isl_sol *sol;
154 /* isl_sol is an interface for constructing a solution to
155 * a parametric integer linear programming problem.
156 * Every time the algorithm reaches a state where a solution
157 * can be read off from the tableau (including cases where the tableau
158 * is empty), the function "add" is called on the isl_sol passed
159 * to find_solutions_main.
161 * The context tableau is owned by isl_sol and is updated incrementally.
163 * There are currently two implementations of this interface,
164 * isl_sol_map, which simply collects the solutions in an isl_map
165 * and (optionally) the parts of the context where there is no solution
166 * in an isl_set, and
167 * isl_sol_for, which calls a user-defined function for each part of
168 * the solution.
170 struct isl_sol {
171 int error;
172 int rational;
173 int level;
174 int max;
175 int n_out;
176 struct isl_context *context;
177 struct isl_partial_sol *partial;
178 void (*add)(struct isl_sol *sol,
179 struct isl_basic_set *dom, struct isl_mat *M);
180 void (*add_empty)(struct isl_sol *sol, struct isl_basic_set *bset);
181 void (*free)(struct isl_sol *sol);
182 struct isl_sol_callback dec_level;
185 static void sol_free(struct isl_sol *sol)
187 struct isl_partial_sol *partial, *next;
188 if (!sol)
189 return;
190 for (partial = sol->partial; partial; partial = next) {
191 next = partial->next;
192 isl_basic_set_free(partial->dom);
193 isl_mat_free(partial->M);
194 free(partial);
196 sol->free(sol);
199 /* Push a partial solution represented by a domain and mapping M
200 * onto the stack of partial solutions.
202 static void sol_push_sol(struct isl_sol *sol,
203 struct isl_basic_set *dom, struct isl_mat *M)
205 struct isl_partial_sol *partial;
207 if (sol->error || !dom)
208 goto error;
210 partial = isl_alloc_type(dom->ctx, struct isl_partial_sol);
211 if (!partial)
212 goto error;
214 partial->level = sol->level;
215 partial->dom = dom;
216 partial->M = M;
217 partial->next = sol->partial;
219 sol->partial = partial;
221 return;
222 error:
223 isl_basic_set_free(dom);
224 isl_mat_free(M);
225 sol->error = 1;
228 /* Pop one partial solution from the partial solution stack and
229 * pass it on to sol->add or sol->add_empty.
231 static void sol_pop_one(struct isl_sol *sol)
233 struct isl_partial_sol *partial;
235 partial = sol->partial;
236 sol->partial = partial->next;
238 if (partial->M)
239 sol->add(sol, partial->dom, partial->M);
240 else
241 sol->add_empty(sol, partial->dom);
242 free(partial);
245 /* Return a fresh copy of the domain represented by the context tableau.
247 static struct isl_basic_set *sol_domain(struct isl_sol *sol)
249 struct isl_basic_set *bset;
251 if (sol->error)
252 return NULL;
254 bset = isl_basic_set_dup(sol->context->op->peek_basic_set(sol->context));
255 bset = isl_basic_set_update_from_tab(bset,
256 sol->context->op->peek_tab(sol->context));
258 return bset;
261 /* Check whether two partial solutions have the same mapping, where n_div
262 * is the number of divs that the two partial solutions have in common.
264 static int same_solution(struct isl_partial_sol *s1, struct isl_partial_sol *s2,
265 unsigned n_div)
267 int i;
268 unsigned dim;
270 if (!s1->M != !s2->M)
271 return 0;
272 if (!s1->M)
273 return 1;
275 dim = isl_basic_set_total_dim(s1->dom) - s1->dom->n_div;
277 for (i = 0; i < s1->M->n_row; ++i) {
278 if (isl_seq_first_non_zero(s1->M->row[i]+1+dim+n_div,
279 s1->M->n_col-1-dim-n_div) != -1)
280 return 0;
281 if (isl_seq_first_non_zero(s2->M->row[i]+1+dim+n_div,
282 s2->M->n_col-1-dim-n_div) != -1)
283 return 0;
284 if (!isl_seq_eq(s1->M->row[i], s2->M->row[i], 1+dim+n_div))
285 return 0;
287 return 1;
290 /* Pop all solutions from the partial solution stack that were pushed onto
291 * the stack at levels that are deeper than the current level.
292 * If the two topmost elements on the stack have the same level
293 * and represent the same solution, then their domains are combined.
294 * This combined domain is the same as the current context domain
295 * as sol_pop is called each time we move back to a higher level.
296 * If the outer level (0) has been reached, then all partial solutions
297 * at the current level are also popped off.
299 static void sol_pop(struct isl_sol *sol)
301 struct isl_partial_sol *partial;
302 unsigned n_div;
304 if (sol->error)
305 return;
307 partial = sol->partial;
308 if (!partial)
309 return;
311 if (partial->level == 0 && sol->level == 0) {
312 for (partial = sol->partial; partial; partial = sol->partial)
313 sol_pop_one(sol);
314 return;
317 if (partial->level <= sol->level)
318 return;
320 if (partial->next && partial->next->level == partial->level) {
321 n_div = isl_basic_set_dim(
322 sol->context->op->peek_basic_set(sol->context),
323 isl_dim_div);
325 if (!same_solution(partial, partial->next, n_div)) {
326 sol_pop_one(sol);
327 sol_pop_one(sol);
328 } else {
329 struct isl_basic_set *bset;
330 isl_mat *M;
331 unsigned n;
333 n = isl_basic_set_dim(partial->next->dom, isl_dim_div);
334 n -= n_div;
335 bset = sol_domain(sol);
336 isl_basic_set_free(partial->next->dom);
337 partial->next->dom = bset;
338 M = partial->next->M;
339 if (M) {
340 M = isl_mat_drop_cols(M, M->n_col - n, n);
341 partial->next->M = M;
342 if (!M)
343 goto error;
345 partial->next->level = sol->level;
347 if (!bset)
348 goto error;
350 sol->partial = partial->next;
351 isl_basic_set_free(partial->dom);
352 isl_mat_free(partial->M);
353 free(partial);
355 } else
356 sol_pop_one(sol);
358 if (sol->level == 0) {
359 for (partial = sol->partial; partial; partial = sol->partial)
360 sol_pop_one(sol);
361 return;
364 if (0)
365 error: sol->error = 1;
368 static void sol_dec_level(struct isl_sol *sol)
370 if (sol->error)
371 return;
373 sol->level--;
375 sol_pop(sol);
378 static int sol_dec_level_wrap(struct isl_tab_callback *cb)
380 struct isl_sol_callback *callback = (struct isl_sol_callback *)cb;
382 sol_dec_level(callback->sol);
384 return callback->sol->error ? -1 : 0;
387 /* Move down to next level and push callback onto context tableau
388 * to decrease the level again when it gets rolled back across
389 * the current state. That is, dec_level will be called with
390 * the context tableau in the same state as it is when inc_level
391 * is called.
393 static void sol_inc_level(struct isl_sol *sol)
395 struct isl_tab *tab;
397 if (sol->error)
398 return;
400 sol->level++;
401 tab = sol->context->op->peek_tab(sol->context);
402 if (isl_tab_push_callback(tab, &sol->dec_level.callback) < 0)
403 sol->error = 1;
406 static void scale_rows(struct isl_mat *mat, isl_int m, int n_row)
408 int i;
410 if (isl_int_is_one(m))
411 return;
413 for (i = 0; i < n_row; ++i)
414 isl_seq_scale(mat->row[i], mat->row[i], m, mat->n_col);
417 /* Add the solution identified by the tableau and the context tableau.
419 * The layout of the variables is as follows.
420 * tab->n_var is equal to the total number of variables in the input
421 * map (including divs that were copied from the context)
422 * + the number of extra divs constructed
423 * Of these, the first tab->n_param and the last tab->n_div variables
424 * correspond to the variables in the context, i.e.,
425 * tab->n_param + tab->n_div = context_tab->n_var
426 * tab->n_param is equal to the number of parameters and input
427 * dimensions in the input map
428 * tab->n_div is equal to the number of divs in the context
430 * If there is no solution, then call add_empty with a basic set
431 * that corresponds to the context tableau. (If add_empty is NULL,
432 * then do nothing).
434 * If there is a solution, then first construct a matrix that maps
435 * all dimensions of the context to the output variables, i.e.,
436 * the output dimensions in the input map.
437 * The divs in the input map (if any) that do not correspond to any
438 * div in the context do not appear in the solution.
439 * The algorithm will make sure that they have an integer value,
440 * but these values themselves are of no interest.
441 * We have to be careful not to drop or rearrange any divs in the
442 * context because that would change the meaning of the matrix.
444 * To extract the value of the output variables, it should be noted
445 * that we always use a big parameter M in the main tableau and so
446 * the variable stored in this tableau is not an output variable x itself, but
447 * x' = M + x (in case of minimization)
448 * or
449 * x' = M - x (in case of maximization)
450 * If x' appears in a column, then its optimal value is zero,
451 * which means that the optimal value of x is an unbounded number
452 * (-M for minimization and M for maximization).
453 * We currently assume that the output dimensions in the original map
454 * are bounded, so this cannot occur.
455 * Similarly, when x' appears in a row, then the coefficient of M in that
456 * row is necessarily 1.
457 * If the row in the tableau represents
458 * d x' = c + d M + e(y)
459 * then, in case of minimization, the corresponding row in the matrix
460 * will be
461 * a c + a e(y)
462 * with a d = m, the (updated) common denominator of the matrix.
463 * In case of maximization, the row will be
464 * -a c - a e(y)
466 static void sol_add(struct isl_sol *sol, struct isl_tab *tab)
468 struct isl_basic_set *bset = NULL;
469 struct isl_mat *mat = NULL;
470 unsigned off;
471 int row;
472 isl_int m;
474 if (sol->error || !tab)
475 goto error;
477 if (tab->empty && !sol->add_empty)
478 return;
479 if (sol->context->op->is_empty(sol->context))
480 return;
482 bset = sol_domain(sol);
484 if (tab->empty) {
485 sol_push_sol(sol, bset, NULL);
486 return;
489 off = 2 + tab->M;
491 mat = isl_mat_alloc(tab->mat->ctx, 1 + sol->n_out,
492 1 + tab->n_param + tab->n_div);
493 if (!mat)
494 goto error;
496 isl_int_init(m);
498 isl_seq_clr(mat->row[0] + 1, mat->n_col - 1);
499 isl_int_set_si(mat->row[0][0], 1);
500 for (row = 0; row < sol->n_out; ++row) {
501 int i = tab->n_param + row;
502 int r, j;
504 isl_seq_clr(mat->row[1 + row], mat->n_col);
505 if (!tab->var[i].is_row) {
506 if (tab->M)
507 isl_die(mat->ctx, isl_error_invalid,
508 "unbounded optimum", goto error2);
509 continue;
512 r = tab->var[i].index;
513 if (tab->M &&
514 isl_int_ne(tab->mat->row[r][2], tab->mat->row[r][0]))
515 isl_die(mat->ctx, isl_error_invalid,
516 "unbounded optimum", goto error2);
517 isl_int_gcd(m, mat->row[0][0], tab->mat->row[r][0]);
518 isl_int_divexact(m, tab->mat->row[r][0], m);
519 scale_rows(mat, m, 1 + row);
520 isl_int_divexact(m, mat->row[0][0], tab->mat->row[r][0]);
521 isl_int_mul(mat->row[1 + row][0], m, tab->mat->row[r][1]);
522 for (j = 0; j < tab->n_param; ++j) {
523 int col;
524 if (tab->var[j].is_row)
525 continue;
526 col = tab->var[j].index;
527 isl_int_mul(mat->row[1 + row][1 + j], m,
528 tab->mat->row[r][off + col]);
530 for (j = 0; j < tab->n_div; ++j) {
531 int col;
532 if (tab->var[tab->n_var - tab->n_div+j].is_row)
533 continue;
534 col = tab->var[tab->n_var - tab->n_div+j].index;
535 isl_int_mul(mat->row[1 + row][1 + tab->n_param + j], m,
536 tab->mat->row[r][off + col]);
538 if (sol->max)
539 isl_seq_neg(mat->row[1 + row], mat->row[1 + row],
540 mat->n_col);
543 isl_int_clear(m);
545 sol_push_sol(sol, bset, mat);
546 return;
547 error2:
548 isl_int_clear(m);
549 error:
550 isl_basic_set_free(bset);
551 isl_mat_free(mat);
552 sol->error = 1;
555 struct isl_sol_map {
556 struct isl_sol sol;
557 struct isl_map *map;
558 struct isl_set *empty;
561 static void sol_map_free(struct isl_sol_map *sol_map)
563 if (!sol_map)
564 return;
565 if (sol_map->sol.context)
566 sol_map->sol.context->op->free(sol_map->sol.context);
567 isl_map_free(sol_map->map);
568 isl_set_free(sol_map->empty);
569 free(sol_map);
572 static void sol_map_free_wrap(struct isl_sol *sol)
574 sol_map_free((struct isl_sol_map *)sol);
577 /* This function is called for parts of the context where there is
578 * no solution, with "bset" corresponding to the context tableau.
579 * Simply add the basic set to the set "empty".
581 static void sol_map_add_empty(struct isl_sol_map *sol,
582 struct isl_basic_set *bset)
584 if (!bset || !sol->empty)
585 goto error;
587 sol->empty = isl_set_grow(sol->empty, 1);
588 bset = isl_basic_set_simplify(bset);
589 bset = isl_basic_set_finalize(bset);
590 sol->empty = isl_set_add_basic_set(sol->empty, isl_basic_set_copy(bset));
591 if (!sol->empty)
592 goto error;
593 isl_basic_set_free(bset);
594 return;
595 error:
596 isl_basic_set_free(bset);
597 sol->sol.error = 1;
600 static void sol_map_add_empty_wrap(struct isl_sol *sol,
601 struct isl_basic_set *bset)
603 sol_map_add_empty((struct isl_sol_map *)sol, bset);
606 /* Given a basic map "dom" that represents the context and an affine
607 * matrix "M" that maps the dimensions of the context to the
608 * output variables, construct a basic map with the same parameters
609 * and divs as the context, the dimensions of the context as input
610 * dimensions and a number of output dimensions that is equal to
611 * the number of output dimensions in the input map.
613 * The constraints and divs of the context are simply copied
614 * from "dom". For each row
615 * x = c + e(y)
616 * an equality
617 * c + e(y) - d x = 0
618 * is added, with d the common denominator of M.
620 static void sol_map_add(struct isl_sol_map *sol,
621 struct isl_basic_set *dom, struct isl_mat *M)
623 int i;
624 struct isl_basic_map *bmap = NULL;
625 unsigned n_eq;
626 unsigned n_ineq;
627 unsigned nparam;
628 unsigned total;
629 unsigned n_div;
630 unsigned n_out;
632 if (sol->sol.error || !dom || !M)
633 goto error;
635 n_out = sol->sol.n_out;
636 n_eq = dom->n_eq + n_out;
637 n_ineq = dom->n_ineq;
638 n_div = dom->n_div;
639 nparam = isl_basic_set_total_dim(dom) - n_div;
640 total = isl_map_dim(sol->map, isl_dim_all);
641 bmap = isl_basic_map_alloc_space(isl_map_get_space(sol->map),
642 n_div, n_eq, 2 * n_div + n_ineq);
643 if (!bmap)
644 goto error;
645 if (sol->sol.rational)
646 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
647 for (i = 0; i < dom->n_div; ++i) {
648 int k = isl_basic_map_alloc_div(bmap);
649 if (k < 0)
650 goto error;
651 isl_seq_cpy(bmap->div[k], dom->div[i], 1 + 1 + nparam);
652 isl_seq_clr(bmap->div[k] + 1 + 1 + nparam, total - nparam);
653 isl_seq_cpy(bmap->div[k] + 1 + 1 + total,
654 dom->div[i] + 1 + 1 + nparam, i);
656 for (i = 0; i < dom->n_eq; ++i) {
657 int k = isl_basic_map_alloc_equality(bmap);
658 if (k < 0)
659 goto error;
660 isl_seq_cpy(bmap->eq[k], dom->eq[i], 1 + nparam);
661 isl_seq_clr(bmap->eq[k] + 1 + nparam, total - nparam);
662 isl_seq_cpy(bmap->eq[k] + 1 + total,
663 dom->eq[i] + 1 + nparam, n_div);
665 for (i = 0; i < dom->n_ineq; ++i) {
666 int k = isl_basic_map_alloc_inequality(bmap);
667 if (k < 0)
668 goto error;
669 isl_seq_cpy(bmap->ineq[k], dom->ineq[i], 1 + nparam);
670 isl_seq_clr(bmap->ineq[k] + 1 + nparam, total - nparam);
671 isl_seq_cpy(bmap->ineq[k] + 1 + total,
672 dom->ineq[i] + 1 + nparam, n_div);
674 for (i = 0; i < M->n_row - 1; ++i) {
675 int k = isl_basic_map_alloc_equality(bmap);
676 if (k < 0)
677 goto error;
678 isl_seq_cpy(bmap->eq[k], M->row[1 + i], 1 + nparam);
679 isl_seq_clr(bmap->eq[k] + 1 + nparam, n_out);
680 isl_int_neg(bmap->eq[k][1 + nparam + i], M->row[0][0]);
681 isl_seq_cpy(bmap->eq[k] + 1 + nparam + n_out,
682 M->row[1 + i] + 1 + nparam, n_div);
684 bmap = isl_basic_map_simplify(bmap);
685 bmap = isl_basic_map_finalize(bmap);
686 sol->map = isl_map_grow(sol->map, 1);
687 sol->map = isl_map_add_basic_map(sol->map, bmap);
688 isl_basic_set_free(dom);
689 isl_mat_free(M);
690 if (!sol->map)
691 sol->sol.error = 1;
692 return;
693 error:
694 isl_basic_set_free(dom);
695 isl_mat_free(M);
696 isl_basic_map_free(bmap);
697 sol->sol.error = 1;
700 static void sol_map_add_wrap(struct isl_sol *sol,
701 struct isl_basic_set *dom, struct isl_mat *M)
703 sol_map_add((struct isl_sol_map *)sol, dom, M);
707 /* Store the "parametric constant" of row "row" of tableau "tab" in "line",
708 * i.e., the constant term and the coefficients of all variables that
709 * appear in the context tableau.
710 * Note that the coefficient of the big parameter M is NOT copied.
711 * The context tableau may not have a big parameter and even when it
712 * does, it is a different big parameter.
714 static void get_row_parameter_line(struct isl_tab *tab, int row, isl_int *line)
716 int i;
717 unsigned off = 2 + tab->M;
719 isl_int_set(line[0], tab->mat->row[row][1]);
720 for (i = 0; i < tab->n_param; ++i) {
721 if (tab->var[i].is_row)
722 isl_int_set_si(line[1 + i], 0);
723 else {
724 int col = tab->var[i].index;
725 isl_int_set(line[1 + i], tab->mat->row[row][off + col]);
728 for (i = 0; i < tab->n_div; ++i) {
729 if (tab->var[tab->n_var - tab->n_div + i].is_row)
730 isl_int_set_si(line[1 + tab->n_param + i], 0);
731 else {
732 int col = tab->var[tab->n_var - tab->n_div + i].index;
733 isl_int_set(line[1 + tab->n_param + i],
734 tab->mat->row[row][off + col]);
739 /* Check if rows "row1" and "row2" have identical "parametric constants",
740 * as explained above.
741 * In this case, we also insist that the coefficients of the big parameter
742 * be the same as the values of the constants will only be the same
743 * if these coefficients are also the same.
745 static int identical_parameter_line(struct isl_tab *tab, int row1, int row2)
747 int i;
748 unsigned off = 2 + tab->M;
750 if (isl_int_ne(tab->mat->row[row1][1], tab->mat->row[row2][1]))
751 return 0;
753 if (tab->M && isl_int_ne(tab->mat->row[row1][2],
754 tab->mat->row[row2][2]))
755 return 0;
757 for (i = 0; i < tab->n_param + tab->n_div; ++i) {
758 int pos = i < tab->n_param ? i :
759 tab->n_var - tab->n_div + i - tab->n_param;
760 int col;
762 if (tab->var[pos].is_row)
763 continue;
764 col = tab->var[pos].index;
765 if (isl_int_ne(tab->mat->row[row1][off + col],
766 tab->mat->row[row2][off + col]))
767 return 0;
769 return 1;
772 /* Return an inequality that expresses that the "parametric constant"
773 * should be non-negative.
774 * This function is only called when the coefficient of the big parameter
775 * is equal to zero.
777 static struct isl_vec *get_row_parameter_ineq(struct isl_tab *tab, int row)
779 struct isl_vec *ineq;
781 ineq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_param + tab->n_div);
782 if (!ineq)
783 return NULL;
785 get_row_parameter_line(tab, row, ineq->el);
786 if (ineq)
787 ineq = isl_vec_normalize(ineq);
789 return ineq;
792 /* Normalize a div expression of the form
794 * [(g*f(x) + c)/(g * m)]
796 * with c the constant term and f(x) the remaining coefficients, to
798 * [(f(x) + [c/g])/m]
800 static void normalize_div(__isl_keep isl_vec *div)
802 isl_ctx *ctx = isl_vec_get_ctx(div);
803 int len = div->size - 2;
805 isl_seq_gcd(div->el + 2, len, &ctx->normalize_gcd);
806 isl_int_gcd(ctx->normalize_gcd, ctx->normalize_gcd, div->el[0]);
808 if (isl_int_is_one(ctx->normalize_gcd))
809 return;
811 isl_int_divexact(div->el[0], div->el[0], ctx->normalize_gcd);
812 isl_int_fdiv_q(div->el[1], div->el[1], ctx->normalize_gcd);
813 isl_seq_scale_down(div->el + 2, div->el + 2, ctx->normalize_gcd, len);
816 /* Return a integer division for use in a parametric cut based on the given row.
817 * In particular, let the parametric constant of the row be
819 * \sum_i a_i y_i
821 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
822 * The div returned is equal to
824 * floor(\sum_i {-a_i} y_i) = floor((\sum_i (-a_i mod d) y_i)/d)
826 static struct isl_vec *get_row_parameter_div(struct isl_tab *tab, int row)
828 struct isl_vec *div;
830 div = isl_vec_alloc(tab->mat->ctx, 1 + 1 + tab->n_param + tab->n_div);
831 if (!div)
832 return NULL;
834 isl_int_set(div->el[0], tab->mat->row[row][0]);
835 get_row_parameter_line(tab, row, div->el + 1);
836 isl_seq_neg(div->el + 1, div->el + 1, div->size - 1);
837 normalize_div(div);
838 isl_seq_fdiv_r(div->el + 1, div->el + 1, div->el[0], div->size - 1);
840 return div;
843 /* Return a integer division for use in transferring an integrality constraint
844 * to the context.
845 * In particular, let the parametric constant of the row be
847 * \sum_i a_i y_i
849 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
850 * The the returned div is equal to
852 * floor(\sum_i {a_i} y_i) = floor((\sum_i (a_i mod d) y_i)/d)
854 static struct isl_vec *get_row_split_div(struct isl_tab *tab, int row)
856 struct isl_vec *div;
858 div = isl_vec_alloc(tab->mat->ctx, 1 + 1 + tab->n_param + tab->n_div);
859 if (!div)
860 return NULL;
862 isl_int_set(div->el[0], tab->mat->row[row][0]);
863 get_row_parameter_line(tab, row, div->el + 1);
864 normalize_div(div);
865 isl_seq_fdiv_r(div->el + 1, div->el + 1, div->el[0], div->size - 1);
867 return div;
870 /* Construct and return an inequality that expresses an upper bound
871 * on the given div.
872 * In particular, if the div is given by
874 * d = floor(e/m)
876 * then the inequality expresses
878 * m d <= e
880 static struct isl_vec *ineq_for_div(struct isl_basic_set *bset, unsigned div)
882 unsigned total;
883 unsigned div_pos;
884 struct isl_vec *ineq;
886 if (!bset)
887 return NULL;
889 total = isl_basic_set_total_dim(bset);
890 div_pos = 1 + total - bset->n_div + div;
892 ineq = isl_vec_alloc(bset->ctx, 1 + total);
893 if (!ineq)
894 return NULL;
896 isl_seq_cpy(ineq->el, bset->div[div] + 1, 1 + total);
897 isl_int_neg(ineq->el[div_pos], bset->div[div][0]);
898 return ineq;
901 /* Given a row in the tableau and a div that was created
902 * using get_row_split_div and that has been constrained to equality, i.e.,
904 * d = floor(\sum_i {a_i} y_i) = \sum_i {a_i} y_i
906 * replace the expression "\sum_i {a_i} y_i" in the row by d,
907 * i.e., we subtract "\sum_i {a_i} y_i" and add 1 d.
908 * The coefficients of the non-parameters in the tableau have been
909 * verified to be integral. We can therefore simply replace coefficient b
910 * by floor(b). For the coefficients of the parameters we have
911 * floor(a_i) = a_i - {a_i}, while for the other coefficients, we have
912 * floor(b) = b.
914 static struct isl_tab *set_row_cst_to_div(struct isl_tab *tab, int row, int div)
916 isl_seq_fdiv_q(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
917 tab->mat->row[row][0], 1 + tab->M + tab->n_col);
919 isl_int_set_si(tab->mat->row[row][0], 1);
921 if (tab->var[tab->n_var - tab->n_div + div].is_row) {
922 int drow = tab->var[tab->n_var - tab->n_div + div].index;
924 isl_assert(tab->mat->ctx,
925 isl_int_is_one(tab->mat->row[drow][0]), goto error);
926 isl_seq_combine(tab->mat->row[row] + 1,
927 tab->mat->ctx->one, tab->mat->row[row] + 1,
928 tab->mat->ctx->one, tab->mat->row[drow] + 1,
929 1 + tab->M + tab->n_col);
930 } else {
931 int dcol = tab->var[tab->n_var - tab->n_div + div].index;
933 isl_int_add_ui(tab->mat->row[row][2 + tab->M + dcol],
934 tab->mat->row[row][2 + tab->M + dcol], 1);
937 return tab;
938 error:
939 isl_tab_free(tab);
940 return NULL;
943 /* Check if the (parametric) constant of the given row is obviously
944 * negative, meaning that we don't need to consult the context tableau.
945 * If there is a big parameter and its coefficient is non-zero,
946 * then this coefficient determines the outcome.
947 * Otherwise, we check whether the constant is negative and
948 * all non-zero coefficients of parameters are negative and
949 * belong to non-negative parameters.
951 static int is_obviously_neg(struct isl_tab *tab, int row)
953 int i;
954 int col;
955 unsigned off = 2 + tab->M;
957 if (tab->M) {
958 if (isl_int_is_pos(tab->mat->row[row][2]))
959 return 0;
960 if (isl_int_is_neg(tab->mat->row[row][2]))
961 return 1;
964 if (isl_int_is_nonneg(tab->mat->row[row][1]))
965 return 0;
966 for (i = 0; i < tab->n_param; ++i) {
967 /* Eliminated parameter */
968 if (tab->var[i].is_row)
969 continue;
970 col = tab->var[i].index;
971 if (isl_int_is_zero(tab->mat->row[row][off + col]))
972 continue;
973 if (!tab->var[i].is_nonneg)
974 return 0;
975 if (isl_int_is_pos(tab->mat->row[row][off + col]))
976 return 0;
978 for (i = 0; i < tab->n_div; ++i) {
979 if (tab->var[tab->n_var - tab->n_div + i].is_row)
980 continue;
981 col = tab->var[tab->n_var - tab->n_div + i].index;
982 if (isl_int_is_zero(tab->mat->row[row][off + col]))
983 continue;
984 if (!tab->var[tab->n_var - tab->n_div + i].is_nonneg)
985 return 0;
986 if (isl_int_is_pos(tab->mat->row[row][off + col]))
987 return 0;
989 return 1;
992 /* Check if the (parametric) constant of the given row is obviously
993 * non-negative, meaning that we don't need to consult the context tableau.
994 * If there is a big parameter and its coefficient is non-zero,
995 * then this coefficient determines the outcome.
996 * Otherwise, we check whether the constant is non-negative and
997 * all non-zero coefficients of parameters are positive and
998 * belong to non-negative parameters.
1000 static int is_obviously_nonneg(struct isl_tab *tab, int row)
1002 int i;
1003 int col;
1004 unsigned off = 2 + tab->M;
1006 if (tab->M) {
1007 if (isl_int_is_pos(tab->mat->row[row][2]))
1008 return 1;
1009 if (isl_int_is_neg(tab->mat->row[row][2]))
1010 return 0;
1013 if (isl_int_is_neg(tab->mat->row[row][1]))
1014 return 0;
1015 for (i = 0; i < tab->n_param; ++i) {
1016 /* Eliminated parameter */
1017 if (tab->var[i].is_row)
1018 continue;
1019 col = tab->var[i].index;
1020 if (isl_int_is_zero(tab->mat->row[row][off + col]))
1021 continue;
1022 if (!tab->var[i].is_nonneg)
1023 return 0;
1024 if (isl_int_is_neg(tab->mat->row[row][off + col]))
1025 return 0;
1027 for (i = 0; i < tab->n_div; ++i) {
1028 if (tab->var[tab->n_var - tab->n_div + i].is_row)
1029 continue;
1030 col = tab->var[tab->n_var - tab->n_div + i].index;
1031 if (isl_int_is_zero(tab->mat->row[row][off + col]))
1032 continue;
1033 if (!tab->var[tab->n_var - tab->n_div + i].is_nonneg)
1034 return 0;
1035 if (isl_int_is_neg(tab->mat->row[row][off + col]))
1036 return 0;
1038 return 1;
1041 /* Given a row r and two columns, return the column that would
1042 * lead to the lexicographically smallest increment in the sample
1043 * solution when leaving the basis in favor of the row.
1044 * Pivoting with column c will increment the sample value by a non-negative
1045 * constant times a_{V,c}/a_{r,c}, with a_{V,c} the elements of column c
1046 * corresponding to the non-parametric variables.
1047 * If variable v appears in a column c_v, the a_{v,c} = 1 iff c = c_v,
1048 * with all other entries in this virtual row equal to zero.
1049 * If variable v appears in a row, then a_{v,c} is the element in column c
1050 * of that row.
1052 * Let v be the first variable with a_{v,c1}/a_{r,c1} != a_{v,c2}/a_{r,c2}.
1053 * Then if a_{v,c1}/a_{r,c1} < a_{v,c2}/a_{r,c2}, i.e.,
1054 * a_{v,c2} a_{r,c1} - a_{v,c1} a_{r,c2} > 0, c1 results in the minimal
1055 * increment. Otherwise, it's c2.
1057 static int lexmin_col_pair(struct isl_tab *tab,
1058 int row, int col1, int col2, isl_int tmp)
1060 int i;
1061 isl_int *tr;
1063 tr = tab->mat->row[row] + 2 + tab->M;
1065 for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
1066 int s1, s2;
1067 isl_int *r;
1069 if (!tab->var[i].is_row) {
1070 if (tab->var[i].index == col1)
1071 return col2;
1072 if (tab->var[i].index == col2)
1073 return col1;
1074 continue;
1077 if (tab->var[i].index == row)
1078 continue;
1080 r = tab->mat->row[tab->var[i].index] + 2 + tab->M;
1081 s1 = isl_int_sgn(r[col1]);
1082 s2 = isl_int_sgn(r[col2]);
1083 if (s1 == 0 && s2 == 0)
1084 continue;
1085 if (s1 < s2)
1086 return col1;
1087 if (s2 < s1)
1088 return col2;
1090 isl_int_mul(tmp, r[col2], tr[col1]);
1091 isl_int_submul(tmp, r[col1], tr[col2]);
1092 if (isl_int_is_pos(tmp))
1093 return col1;
1094 if (isl_int_is_neg(tmp))
1095 return col2;
1097 return -1;
1100 /* Given a row in the tableau, find and return the column that would
1101 * result in the lexicographically smallest, but positive, increment
1102 * in the sample point.
1103 * If there is no such column, then return tab->n_col.
1104 * If anything goes wrong, return -1.
1106 static int lexmin_pivot_col(struct isl_tab *tab, int row)
1108 int j;
1109 int col = tab->n_col;
1110 isl_int *tr;
1111 isl_int tmp;
1113 tr = tab->mat->row[row] + 2 + tab->M;
1115 isl_int_init(tmp);
1117 for (j = tab->n_dead; j < tab->n_col; ++j) {
1118 if (tab->col_var[j] >= 0 &&
1119 (tab->col_var[j] < tab->n_param ||
1120 tab->col_var[j] >= tab->n_var - tab->n_div))
1121 continue;
1123 if (!isl_int_is_pos(tr[j]))
1124 continue;
1126 if (col == tab->n_col)
1127 col = j;
1128 else
1129 col = lexmin_col_pair(tab, row, col, j, tmp);
1130 isl_assert(tab->mat->ctx, col >= 0, goto error);
1133 isl_int_clear(tmp);
1134 return col;
1135 error:
1136 isl_int_clear(tmp);
1137 return -1;
1140 /* Return the first known violated constraint, i.e., a non-negative
1141 * constraint that currently has an either obviously negative value
1142 * or a previously determined to be negative value.
1144 * If any constraint has a negative coefficient for the big parameter,
1145 * if any, then we return one of these first.
1147 static int first_neg(struct isl_tab *tab)
1149 int row;
1151 if (tab->M)
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 (!isl_int_is_neg(tab->mat->row[row][2]))
1156 continue;
1157 if (tab->row_sign)
1158 tab->row_sign[row] = isl_tab_row_neg;
1159 return row;
1161 for (row = tab->n_redundant; row < tab->n_row; ++row) {
1162 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
1163 continue;
1164 if (tab->row_sign) {
1165 if (tab->row_sign[row] == 0 &&
1166 is_obviously_neg(tab, row))
1167 tab->row_sign[row] = isl_tab_row_neg;
1168 if (tab->row_sign[row] != isl_tab_row_neg)
1169 continue;
1170 } else if (!is_obviously_neg(tab, row))
1171 continue;
1172 return row;
1174 return -1;
1177 /* Check whether the invariant that all columns are lexico-positive
1178 * is satisfied. This function is not called from the current code
1179 * but is useful during debugging.
1181 static void check_lexpos(struct isl_tab *tab) __attribute__ ((unused));
1182 static void check_lexpos(struct isl_tab *tab)
1184 unsigned off = 2 + tab->M;
1185 int col;
1186 int var;
1187 int row;
1189 for (col = tab->n_dead; col < tab->n_col; ++col) {
1190 if (tab->col_var[col] >= 0 &&
1191 (tab->col_var[col] < tab->n_param ||
1192 tab->col_var[col] >= tab->n_var - tab->n_div))
1193 continue;
1194 for (var = tab->n_param; var < tab->n_var - tab->n_div; ++var) {
1195 if (!tab->var[var].is_row) {
1196 if (tab->var[var].index == col)
1197 break;
1198 else
1199 continue;
1201 row = tab->var[var].index;
1202 if (isl_int_is_zero(tab->mat->row[row][off + col]))
1203 continue;
1204 if (isl_int_is_pos(tab->mat->row[row][off + col]))
1205 break;
1206 fprintf(stderr, "lexneg column %d (row %d)\n",
1207 col, row);
1209 if (var >= tab->n_var - tab->n_div)
1210 fprintf(stderr, "zero column %d\n", col);
1214 /* Report to the caller that the given constraint is part of an encountered
1215 * conflict.
1217 static int report_conflicting_constraint(struct isl_tab *tab, int con)
1219 return tab->conflict(con, tab->conflict_user);
1222 /* Given a conflicting row in the tableau, report all constraints
1223 * involved in the row to the caller. That is, the row itself
1224 * (if it represents a constraint) and all constraint columns with
1225 * non-zero (and therefore negative) coefficients.
1227 static int report_conflict(struct isl_tab *tab, int row)
1229 int j;
1230 isl_int *tr;
1232 if (!tab->conflict)
1233 return 0;
1235 if (tab->row_var[row] < 0 &&
1236 report_conflicting_constraint(tab, ~tab->row_var[row]) < 0)
1237 return -1;
1239 tr = tab->mat->row[row] + 2 + tab->M;
1241 for (j = tab->n_dead; j < tab->n_col; ++j) {
1242 if (tab->col_var[j] >= 0 &&
1243 (tab->col_var[j] < tab->n_param ||
1244 tab->col_var[j] >= tab->n_var - tab->n_div))
1245 continue;
1247 if (!isl_int_is_neg(tr[j]))
1248 continue;
1250 if (tab->col_var[j] < 0 &&
1251 report_conflicting_constraint(tab, ~tab->col_var[j]) < 0)
1252 return -1;
1255 return 0;
1258 /* Resolve all known or obviously violated constraints through pivoting.
1259 * In particular, as long as we can find any violated constraint, we
1260 * look for a pivoting column that would result in the lexicographically
1261 * smallest increment in the sample point. If there is no such column
1262 * then the tableau is infeasible.
1264 static int restore_lexmin(struct isl_tab *tab) WARN_UNUSED;
1265 static int restore_lexmin(struct isl_tab *tab)
1267 int row, col;
1269 if (!tab)
1270 return -1;
1271 if (tab->empty)
1272 return 0;
1273 while ((row = first_neg(tab)) != -1) {
1274 col = lexmin_pivot_col(tab, row);
1275 if (col >= tab->n_col) {
1276 if (report_conflict(tab, row) < 0)
1277 return -1;
1278 if (isl_tab_mark_empty(tab) < 0)
1279 return -1;
1280 return 0;
1282 if (col < 0)
1283 return -1;
1284 if (isl_tab_pivot(tab, row, col) < 0)
1285 return -1;
1287 return 0;
1290 /* Given a row that represents an equality, look for an appropriate
1291 * pivoting column.
1292 * In particular, if there are any non-zero coefficients among
1293 * the non-parameter variables, then we take the last of these
1294 * variables. Eliminating this variable in terms of the other
1295 * variables and/or parameters does not influence the property
1296 * that all column in the initial tableau are lexicographically
1297 * positive. The row corresponding to the eliminated variable
1298 * will only have non-zero entries below the diagonal of the
1299 * initial tableau. That is, we transform
1301 * I I
1302 * 1 into a
1303 * I I
1305 * If there is no such non-parameter variable, then we are dealing with
1306 * pure parameter equality and we pick any parameter with coefficient 1 or -1
1307 * for elimination. This will ensure that the eliminated parameter
1308 * always has an integer value whenever all the other parameters are integral.
1309 * If there is no such parameter then we return -1.
1311 static int last_var_col_or_int_par_col(struct isl_tab *tab, int row)
1313 unsigned off = 2 + tab->M;
1314 int i;
1316 for (i = tab->n_var - tab->n_div - 1; i >= 0 && i >= tab->n_param; --i) {
1317 int col;
1318 if (tab->var[i].is_row)
1319 continue;
1320 col = tab->var[i].index;
1321 if (col <= tab->n_dead)
1322 continue;
1323 if (!isl_int_is_zero(tab->mat->row[row][off + col]))
1324 return col;
1326 for (i = tab->n_dead; i < tab->n_col; ++i) {
1327 if (isl_int_is_one(tab->mat->row[row][off + i]))
1328 return i;
1329 if (isl_int_is_negone(tab->mat->row[row][off + i]))
1330 return i;
1332 return -1;
1335 /* Add an equality that is known to be valid to the tableau.
1336 * We first check if we can eliminate a variable or a parameter.
1337 * If not, we add the equality as two inequalities.
1338 * In this case, the equality was a pure parameter equality and there
1339 * is no need to resolve any constraint violations.
1341 * This function assumes that at least two more rows and at least
1342 * two more elements in the constraint array are available in the tableau.
1344 static struct isl_tab *add_lexmin_valid_eq(struct isl_tab *tab, isl_int *eq)
1346 int i;
1347 int r;
1349 if (!tab)
1350 return NULL;
1351 r = isl_tab_add_row(tab, eq);
1352 if (r < 0)
1353 goto error;
1355 r = tab->con[r].index;
1356 i = last_var_col_or_int_par_col(tab, r);
1357 if (i < 0) {
1358 tab->con[r].is_nonneg = 1;
1359 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1360 goto error;
1361 isl_seq_neg(eq, eq, 1 + tab->n_var);
1362 r = isl_tab_add_row(tab, eq);
1363 if (r < 0)
1364 goto error;
1365 tab->con[r].is_nonneg = 1;
1366 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1367 goto error;
1368 } else {
1369 if (isl_tab_pivot(tab, r, i) < 0)
1370 goto error;
1371 if (isl_tab_kill_col(tab, i) < 0)
1372 goto error;
1373 tab->n_eq++;
1376 return tab;
1377 error:
1378 isl_tab_free(tab);
1379 return NULL;
1382 /* Check if the given row is a pure constant.
1384 static int is_constant(struct isl_tab *tab, int row)
1386 unsigned off = 2 + tab->M;
1388 return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1389 tab->n_col - tab->n_dead) == -1;
1392 /* Add an equality that may or may not be valid to the tableau.
1393 * If the resulting row is a pure constant, then it must be zero.
1394 * Otherwise, the resulting tableau is empty.
1396 * If the row is not a pure constant, then we add two inequalities,
1397 * each time checking that they can be satisfied.
1398 * In the end we try to use one of the two constraints to eliminate
1399 * a column.
1401 * This function assumes that at least two more rows and at least
1402 * two more elements in the constraint array are available in the tableau.
1404 static int add_lexmin_eq(struct isl_tab *tab, isl_int *eq) WARN_UNUSED;
1405 static int add_lexmin_eq(struct isl_tab *tab, isl_int *eq)
1407 int r1, r2;
1408 int row;
1409 struct isl_tab_undo *snap;
1411 if (!tab)
1412 return -1;
1413 snap = isl_tab_snap(tab);
1414 r1 = isl_tab_add_row(tab, eq);
1415 if (r1 < 0)
1416 return -1;
1417 tab->con[r1].is_nonneg = 1;
1418 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r1]) < 0)
1419 return -1;
1421 row = tab->con[r1].index;
1422 if (is_constant(tab, row)) {
1423 if (!isl_int_is_zero(tab->mat->row[row][1]) ||
1424 (tab->M && !isl_int_is_zero(tab->mat->row[row][2]))) {
1425 if (isl_tab_mark_empty(tab) < 0)
1426 return -1;
1427 return 0;
1429 if (isl_tab_rollback(tab, snap) < 0)
1430 return -1;
1431 return 0;
1434 if (restore_lexmin(tab) < 0)
1435 return -1;
1436 if (tab->empty)
1437 return 0;
1439 isl_seq_neg(eq, eq, 1 + tab->n_var);
1441 r2 = isl_tab_add_row(tab, eq);
1442 if (r2 < 0)
1443 return -1;
1444 tab->con[r2].is_nonneg = 1;
1445 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r2]) < 0)
1446 return -1;
1448 if (restore_lexmin(tab) < 0)
1449 return -1;
1450 if (tab->empty)
1451 return 0;
1453 if (!tab->con[r1].is_row) {
1454 if (isl_tab_kill_col(tab, tab->con[r1].index) < 0)
1455 return -1;
1456 } else if (!tab->con[r2].is_row) {
1457 if (isl_tab_kill_col(tab, tab->con[r2].index) < 0)
1458 return -1;
1461 if (tab->bmap) {
1462 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1463 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1464 return -1;
1465 isl_seq_neg(eq, eq, 1 + tab->n_var);
1466 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1467 isl_seq_neg(eq, eq, 1 + tab->n_var);
1468 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1469 return -1;
1470 if (!tab->bmap)
1471 return -1;
1474 return 0;
1477 /* Add an inequality to the tableau, resolving violations using
1478 * restore_lexmin.
1480 * This function assumes that at least one more row and at least
1481 * one more element in the constraint array are available in the tableau.
1483 static struct isl_tab *add_lexmin_ineq(struct isl_tab *tab, isl_int *ineq)
1485 int r;
1487 if (!tab)
1488 return NULL;
1489 if (tab->bmap) {
1490 tab->bmap = isl_basic_map_add_ineq(tab->bmap, ineq);
1491 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1492 goto error;
1493 if (!tab->bmap)
1494 goto error;
1496 r = isl_tab_add_row(tab, ineq);
1497 if (r < 0)
1498 goto error;
1499 tab->con[r].is_nonneg = 1;
1500 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1501 goto error;
1502 if (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;
1508 if (restore_lexmin(tab) < 0)
1509 goto error;
1510 if (!tab->empty && tab->con[r].is_row &&
1511 isl_tab_row_is_redundant(tab, tab->con[r].index))
1512 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1513 goto error;
1514 return tab;
1515 error:
1516 isl_tab_free(tab);
1517 return NULL;
1520 /* Check if the coefficients of the parameters are all integral.
1522 static int integer_parameter(struct isl_tab *tab, int row)
1524 int i;
1525 int col;
1526 unsigned off = 2 + tab->M;
1528 for (i = 0; i < tab->n_param; ++i) {
1529 /* Eliminated parameter */
1530 if (tab->var[i].is_row)
1531 continue;
1532 col = tab->var[i].index;
1533 if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
1534 tab->mat->row[row][0]))
1535 return 0;
1537 for (i = 0; i < tab->n_div; ++i) {
1538 if (tab->var[tab->n_var - tab->n_div + i].is_row)
1539 continue;
1540 col = tab->var[tab->n_var - tab->n_div + i].index;
1541 if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
1542 tab->mat->row[row][0]))
1543 return 0;
1545 return 1;
1548 /* Check if the coefficients of the non-parameter variables are all integral.
1550 static int integer_variable(struct isl_tab *tab, int row)
1552 int i;
1553 unsigned off = 2 + tab->M;
1555 for (i = tab->n_dead; i < tab->n_col; ++i) {
1556 if (tab->col_var[i] >= 0 &&
1557 (tab->col_var[i] < tab->n_param ||
1558 tab->col_var[i] >= tab->n_var - tab->n_div))
1559 continue;
1560 if (!isl_int_is_divisible_by(tab->mat->row[row][off + i],
1561 tab->mat->row[row][0]))
1562 return 0;
1564 return 1;
1567 /* Check if the constant term is integral.
1569 static int integer_constant(struct isl_tab *tab, int row)
1571 return isl_int_is_divisible_by(tab->mat->row[row][1],
1572 tab->mat->row[row][0]);
1575 #define I_CST 1 << 0
1576 #define I_PAR 1 << 1
1577 #define I_VAR 1 << 2
1579 /* Check for next (non-parameter) variable after "var" (first if var == -1)
1580 * that is non-integer and therefore requires a cut and return
1581 * the index of the variable.
1582 * For parametric tableaus, there are three parts in a row,
1583 * the constant, the coefficients of the parameters and the rest.
1584 * For each part, we check whether the coefficients in that part
1585 * are all integral and if so, set the corresponding flag in *f.
1586 * If the constant and the parameter part are integral, then the
1587 * current sample value is integral and no cut is required
1588 * (irrespective of whether the variable part is integral).
1590 static int next_non_integer_var(struct isl_tab *tab, int var, int *f)
1592 var = var < 0 ? tab->n_param : var + 1;
1594 for (; var < tab->n_var - tab->n_div; ++var) {
1595 int flags = 0;
1596 int row;
1597 if (!tab->var[var].is_row)
1598 continue;
1599 row = tab->var[var].index;
1600 if (integer_constant(tab, row))
1601 ISL_FL_SET(flags, I_CST);
1602 if (integer_parameter(tab, row))
1603 ISL_FL_SET(flags, I_PAR);
1604 if (ISL_FL_ISSET(flags, I_CST) && ISL_FL_ISSET(flags, I_PAR))
1605 continue;
1606 if (integer_variable(tab, row))
1607 ISL_FL_SET(flags, I_VAR);
1608 *f = flags;
1609 return var;
1611 return -1;
1614 /* Check for first (non-parameter) variable that is non-integer and
1615 * therefore requires a cut and return the corresponding row.
1616 * For parametric tableaus, there are three parts in a row,
1617 * the constant, the coefficients of the parameters and the rest.
1618 * For each part, we check whether the coefficients in that part
1619 * are all integral and if so, set the corresponding flag in *f.
1620 * If the constant and the parameter part are integral, then the
1621 * current sample value is integral and no cut is required
1622 * (irrespective of whether the variable part is integral).
1624 static int first_non_integer_row(struct isl_tab *tab, int *f)
1626 int var = next_non_integer_var(tab, -1, f);
1628 return var < 0 ? -1 : tab->var[var].index;
1631 /* Add a (non-parametric) cut to cut away the non-integral sample
1632 * value of the given row.
1634 * If the row is given by
1636 * m r = f + \sum_i a_i y_i
1638 * then the cut is
1640 * c = - {-f/m} + \sum_i {a_i/m} y_i >= 0
1642 * The big parameter, if any, is ignored, since it is assumed to be big
1643 * enough to be divisible by any integer.
1644 * If the tableau is actually a parametric tableau, then this function
1645 * is only called when all coefficients of the parameters are integral.
1646 * The cut therefore has zero coefficients for the parameters.
1648 * The current value is known to be negative, so row_sign, if it
1649 * exists, is set accordingly.
1651 * Return the row of the cut or -1.
1653 static int add_cut(struct isl_tab *tab, int row)
1655 int i;
1656 int r;
1657 isl_int *r_row;
1658 unsigned off = 2 + tab->M;
1660 if (isl_tab_extend_cons(tab, 1) < 0)
1661 return -1;
1662 r = isl_tab_allocate_con(tab);
1663 if (r < 0)
1664 return -1;
1666 r_row = tab->mat->row[tab->con[r].index];
1667 isl_int_set(r_row[0], tab->mat->row[row][0]);
1668 isl_int_neg(r_row[1], tab->mat->row[row][1]);
1669 isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
1670 isl_int_neg(r_row[1], r_row[1]);
1671 if (tab->M)
1672 isl_int_set_si(r_row[2], 0);
1673 for (i = 0; i < tab->n_col; ++i)
1674 isl_int_fdiv_r(r_row[off + i],
1675 tab->mat->row[row][off + i], tab->mat->row[row][0]);
1677 tab->con[r].is_nonneg = 1;
1678 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1679 return -1;
1680 if (tab->row_sign)
1681 tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
1683 return tab->con[r].index;
1686 #define CUT_ALL 1
1687 #define CUT_ONE 0
1689 /* Given a non-parametric tableau, add cuts until an integer
1690 * sample point is obtained or until the tableau is determined
1691 * to be integer infeasible.
1692 * As long as there is any non-integer value in the sample point,
1693 * we add appropriate cuts, if possible, for each of these
1694 * non-integer values and then resolve the violated
1695 * cut constraints using restore_lexmin.
1696 * If one of the corresponding rows is equal to an integral
1697 * combination of variables/constraints plus a non-integral constant,
1698 * then there is no way to obtain an integer point and we return
1699 * a tableau that is marked empty.
1700 * The parameter cutting_strategy controls the strategy used when adding cuts
1701 * to remove non-integer points. CUT_ALL adds all possible cuts
1702 * before continuing the search. CUT_ONE adds only one cut at a time.
1704 static struct isl_tab *cut_to_integer_lexmin(struct isl_tab *tab,
1705 int cutting_strategy)
1707 int var;
1708 int row;
1709 int flags;
1711 if (!tab)
1712 return NULL;
1713 if (tab->empty)
1714 return tab;
1716 while ((var = next_non_integer_var(tab, -1, &flags)) != -1) {
1717 do {
1718 if (ISL_FL_ISSET(flags, I_VAR)) {
1719 if (isl_tab_mark_empty(tab) < 0)
1720 goto error;
1721 return tab;
1723 row = tab->var[var].index;
1724 row = add_cut(tab, row);
1725 if (row < 0)
1726 goto error;
1727 if (cutting_strategy == CUT_ONE)
1728 break;
1729 } while ((var = next_non_integer_var(tab, var, &flags)) != -1);
1730 if (restore_lexmin(tab) < 0)
1731 goto error;
1732 if (tab->empty)
1733 break;
1735 return tab;
1736 error:
1737 isl_tab_free(tab);
1738 return NULL;
1741 /* Check whether all the currently active samples also satisfy the inequality
1742 * "ineq" (treated as an equality if eq is set).
1743 * Remove those samples that do not.
1745 static struct isl_tab *check_samples(struct isl_tab *tab, isl_int *ineq, int eq)
1747 int i;
1748 isl_int v;
1750 if (!tab)
1751 return NULL;
1753 isl_assert(tab->mat->ctx, tab->bmap, goto error);
1754 isl_assert(tab->mat->ctx, tab->samples, goto error);
1755 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, goto error);
1757 isl_int_init(v);
1758 for (i = tab->n_outside; i < tab->n_sample; ++i) {
1759 int sgn;
1760 isl_seq_inner_product(ineq, tab->samples->row[i],
1761 1 + tab->n_var, &v);
1762 sgn = isl_int_sgn(v);
1763 if (eq ? (sgn == 0) : (sgn >= 0))
1764 continue;
1765 tab = isl_tab_drop_sample(tab, i);
1766 if (!tab)
1767 break;
1769 isl_int_clear(v);
1771 return tab;
1772 error:
1773 isl_tab_free(tab);
1774 return NULL;
1777 /* Check whether the sample value of the tableau is finite,
1778 * i.e., either the tableau does not use a big parameter, or
1779 * all values of the variables are equal to the big parameter plus
1780 * some constant. This constant is the actual sample value.
1782 static int sample_is_finite(struct isl_tab *tab)
1784 int i;
1786 if (!tab->M)
1787 return 1;
1789 for (i = 0; i < tab->n_var; ++i) {
1790 int row;
1791 if (!tab->var[i].is_row)
1792 return 0;
1793 row = tab->var[i].index;
1794 if (isl_int_ne(tab->mat->row[row][0], tab->mat->row[row][2]))
1795 return 0;
1797 return 1;
1800 /* Check if the context tableau of sol has any integer points.
1801 * Leave tab in empty state if no integer point can be found.
1802 * If an integer point can be found and if moreover it is finite,
1803 * then it is added to the list of sample values.
1805 * This function is only called when none of the currently active sample
1806 * values satisfies the most recently added constraint.
1808 static struct isl_tab *check_integer_feasible(struct isl_tab *tab)
1810 struct isl_tab_undo *snap;
1812 if (!tab)
1813 return NULL;
1815 snap = isl_tab_snap(tab);
1816 if (isl_tab_push_basis(tab) < 0)
1817 goto error;
1819 tab = cut_to_integer_lexmin(tab, CUT_ALL);
1820 if (!tab)
1821 goto error;
1823 if (!tab->empty && sample_is_finite(tab)) {
1824 struct isl_vec *sample;
1826 sample = isl_tab_get_sample_value(tab);
1828 if (isl_tab_add_sample(tab, sample) < 0)
1829 goto error;
1832 if (!tab->empty && isl_tab_rollback(tab, snap) < 0)
1833 goto error;
1835 return tab;
1836 error:
1837 isl_tab_free(tab);
1838 return NULL;
1841 /* Check if any of the currently active sample values satisfies
1842 * the inequality "ineq" (an equality if eq is set).
1844 static int tab_has_valid_sample(struct isl_tab *tab, isl_int *ineq, int eq)
1846 int i;
1847 isl_int v;
1849 if (!tab)
1850 return -1;
1852 isl_assert(tab->mat->ctx, tab->bmap, return -1);
1853 isl_assert(tab->mat->ctx, tab->samples, return -1);
1854 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, return -1);
1856 isl_int_init(v);
1857 for (i = tab->n_outside; i < tab->n_sample; ++i) {
1858 int sgn;
1859 isl_seq_inner_product(ineq, tab->samples->row[i],
1860 1 + tab->n_var, &v);
1861 sgn = isl_int_sgn(v);
1862 if (eq ? (sgn == 0) : (sgn >= 0))
1863 break;
1865 isl_int_clear(v);
1867 return i < tab->n_sample;
1870 /* Insert a div specified by "div" to the tableau "tab" at position "pos" and
1871 * return isl_bool_true if the div is obviously non-negative.
1873 static isl_bool context_tab_insert_div(struct isl_tab *tab, int pos,
1874 __isl_keep isl_vec *div,
1875 int (*add_ineq)(void *user, isl_int *), void *user)
1877 int i;
1878 int r;
1879 struct isl_mat *samples;
1880 int nonneg;
1882 r = isl_tab_insert_div(tab, pos, div, add_ineq, user);
1883 if (r < 0)
1884 return isl_bool_error;
1885 nonneg = tab->var[r].is_nonneg;
1886 tab->var[r].frozen = 1;
1888 samples = isl_mat_extend(tab->samples,
1889 tab->n_sample, 1 + tab->n_var);
1890 tab->samples = samples;
1891 if (!samples)
1892 return isl_bool_error;
1893 for (i = tab->n_outside; i < samples->n_row; ++i) {
1894 isl_seq_inner_product(div->el + 1, samples->row[i],
1895 div->size - 1, &samples->row[i][samples->n_col - 1]);
1896 isl_int_fdiv_q(samples->row[i][samples->n_col - 1],
1897 samples->row[i][samples->n_col - 1], div->el[0]);
1899 tab->samples = isl_mat_move_cols(tab->samples, 1 + pos,
1900 1 + tab->n_var - 1, 1);
1901 if (!tab->samples)
1902 return isl_bool_error;
1904 return nonneg;
1907 /* Add a div specified by "div" to both the main tableau and
1908 * the context tableau. In case of the main tableau, we only
1909 * need to add an extra div. In the context tableau, we also
1910 * need to express the meaning of the div.
1911 * Return the index of the div or -1 if anything went wrong.
1913 * The new integer division is added before any unknown integer
1914 * divisions in the context to ensure that it does not get
1915 * equated to some linear combination involving unknown integer
1916 * divisions.
1918 static int add_div(struct isl_tab *tab, struct isl_context *context,
1919 __isl_keep isl_vec *div)
1921 int r;
1922 int pos;
1923 isl_bool nonneg;
1924 struct isl_tab *context_tab = context->op->peek_tab(context);
1926 if (!tab || !context_tab)
1927 goto error;
1929 pos = context_tab->n_var - context->n_unknown;
1930 if ((nonneg = context->op->insert_div(context, pos, div)) < 0)
1931 goto error;
1933 if (!context->op->is_ok(context))
1934 goto error;
1936 pos = tab->n_var - context->n_unknown;
1937 if (isl_tab_extend_vars(tab, 1) < 0)
1938 goto error;
1939 r = isl_tab_insert_var(tab, pos);
1940 if (r < 0)
1941 goto error;
1942 if (nonneg)
1943 tab->var[r].is_nonneg = 1;
1944 tab->var[r].frozen = 1;
1945 tab->n_div++;
1947 return tab->n_div - 1 - context->n_unknown;
1948 error:
1949 context->op->invalidate(context);
1950 return -1;
1953 static int find_div(struct isl_tab *tab, isl_int *div, isl_int denom)
1955 int i;
1956 unsigned total = isl_basic_map_total_dim(tab->bmap);
1958 for (i = 0; i < tab->bmap->n_div; ++i) {
1959 if (isl_int_ne(tab->bmap->div[i][0], denom))
1960 continue;
1961 if (!isl_seq_eq(tab->bmap->div[i] + 1, div, 1 + total))
1962 continue;
1963 return i;
1965 return -1;
1968 /* Return the index of a div that corresponds to "div".
1969 * We first check if we already have such a div and if not, we create one.
1971 static int get_div(struct isl_tab *tab, struct isl_context *context,
1972 struct isl_vec *div)
1974 int d;
1975 struct isl_tab *context_tab = context->op->peek_tab(context);
1977 if (!context_tab)
1978 return -1;
1980 d = find_div(context_tab, div->el + 1, div->el[0]);
1981 if (d != -1)
1982 return d;
1984 return add_div(tab, context, div);
1987 /* Add a parametric cut to cut away the non-integral sample value
1988 * of the give row.
1989 * Let a_i be the coefficients of the constant term and the parameters
1990 * and let b_i be the coefficients of the variables or constraints
1991 * in basis of the tableau.
1992 * Let q be the div q = floor(\sum_i {-a_i} y_i).
1994 * The cut is expressed as
1996 * c = \sum_i -{-a_i} y_i + \sum_i {b_i} x_i + q >= 0
1998 * If q did not already exist in the context tableau, then it is added first.
1999 * If q is in a column of the main tableau then the "+ q" can be accomplished
2000 * by setting the corresponding entry to the denominator of the constraint.
2001 * If q happens to be in a row of the main tableau, then the corresponding
2002 * row needs to be added instead (taking care of the denominators).
2003 * Note that this is very unlikely, but perhaps not entirely impossible.
2005 * The current value of the cut is known to be negative (or at least
2006 * non-positive), so row_sign is set accordingly.
2008 * Return the row of the cut or -1.
2010 static int add_parametric_cut(struct isl_tab *tab, int row,
2011 struct isl_context *context)
2013 struct isl_vec *div;
2014 int d;
2015 int i;
2016 int r;
2017 isl_int *r_row;
2018 int col;
2019 int n;
2020 unsigned off = 2 + tab->M;
2022 if (!context)
2023 return -1;
2025 div = get_row_parameter_div(tab, row);
2026 if (!div)
2027 return -1;
2029 n = tab->n_div - context->n_unknown;
2030 d = context->op->get_div(context, tab, div);
2031 isl_vec_free(div);
2032 if (d < 0)
2033 return -1;
2035 if (isl_tab_extend_cons(tab, 1) < 0)
2036 return -1;
2037 r = isl_tab_allocate_con(tab);
2038 if (r < 0)
2039 return -1;
2041 r_row = tab->mat->row[tab->con[r].index];
2042 isl_int_set(r_row[0], tab->mat->row[row][0]);
2043 isl_int_neg(r_row[1], tab->mat->row[row][1]);
2044 isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
2045 isl_int_neg(r_row[1], r_row[1]);
2046 if (tab->M)
2047 isl_int_set_si(r_row[2], 0);
2048 for (i = 0; i < tab->n_param; ++i) {
2049 if (tab->var[i].is_row)
2050 continue;
2051 col = tab->var[i].index;
2052 isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
2053 isl_int_fdiv_r(r_row[off + col], r_row[off + col],
2054 tab->mat->row[row][0]);
2055 isl_int_neg(r_row[off + col], r_row[off + col]);
2057 for (i = 0; i < tab->n_div; ++i) {
2058 if (tab->var[tab->n_var - tab->n_div + i].is_row)
2059 continue;
2060 col = tab->var[tab->n_var - tab->n_div + i].index;
2061 isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
2062 isl_int_fdiv_r(r_row[off + col], r_row[off + col],
2063 tab->mat->row[row][0]);
2064 isl_int_neg(r_row[off + col], r_row[off + col]);
2066 for (i = 0; i < tab->n_col; ++i) {
2067 if (tab->col_var[i] >= 0 &&
2068 (tab->col_var[i] < tab->n_param ||
2069 tab->col_var[i] >= tab->n_var - tab->n_div))
2070 continue;
2071 isl_int_fdiv_r(r_row[off + i],
2072 tab->mat->row[row][off + i], tab->mat->row[row][0]);
2074 if (tab->var[tab->n_var - tab->n_div + d].is_row) {
2075 isl_int gcd;
2076 int d_row = tab->var[tab->n_var - tab->n_div + d].index;
2077 isl_int_init(gcd);
2078 isl_int_gcd(gcd, tab->mat->row[d_row][0], r_row[0]);
2079 isl_int_divexact(r_row[0], r_row[0], gcd);
2080 isl_int_divexact(gcd, tab->mat->row[d_row][0], gcd);
2081 isl_seq_combine(r_row + 1, gcd, r_row + 1,
2082 r_row[0], tab->mat->row[d_row] + 1,
2083 off - 1 + tab->n_col);
2084 isl_int_mul(r_row[0], r_row[0], tab->mat->row[d_row][0]);
2085 isl_int_clear(gcd);
2086 } else {
2087 col = tab->var[tab->n_var - tab->n_div + d].index;
2088 isl_int_set(r_row[off + col], tab->mat->row[row][0]);
2091 tab->con[r].is_nonneg = 1;
2092 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
2093 return -1;
2094 if (tab->row_sign)
2095 tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
2097 row = tab->con[r].index;
2099 if (d >= n && context->op->detect_equalities(context, tab) < 0)
2100 return -1;
2102 return row;
2105 /* Construct a tableau for bmap that can be used for computing
2106 * the lexicographic minimum (or maximum) of bmap.
2107 * If not NULL, then dom is the domain where the minimum
2108 * should be computed. In this case, we set up a parametric
2109 * tableau with row signs (initialized to "unknown").
2110 * If M is set, then the tableau will use a big parameter.
2111 * If max is set, then a maximum should be computed instead of a minimum.
2112 * This means that for each variable x, the tableau will contain the variable
2113 * x' = M - x, rather than x' = M + x. This in turn means that the coefficient
2114 * of the variables in all constraints are negated prior to adding them
2115 * to the tableau.
2117 static struct isl_tab *tab_for_lexmin(struct isl_basic_map *bmap,
2118 struct isl_basic_set *dom, unsigned M, int max)
2120 int i;
2121 struct isl_tab *tab;
2122 unsigned n_var;
2123 unsigned o_var;
2125 tab = isl_tab_alloc(bmap->ctx, 2 * bmap->n_eq + bmap->n_ineq + 1,
2126 isl_basic_map_total_dim(bmap), M);
2127 if (!tab)
2128 return NULL;
2130 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
2131 if (dom) {
2132 tab->n_param = isl_basic_set_total_dim(dom) - dom->n_div;
2133 tab->n_div = dom->n_div;
2134 tab->row_sign = isl_calloc_array(bmap->ctx,
2135 enum isl_tab_row_sign, tab->mat->n_row);
2136 if (tab->mat->n_row && !tab->row_sign)
2137 goto error;
2139 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
2140 if (isl_tab_mark_empty(tab) < 0)
2141 goto error;
2142 return tab;
2145 for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
2146 tab->var[i].is_nonneg = 1;
2147 tab->var[i].frozen = 1;
2149 o_var = 1 + tab->n_param;
2150 n_var = tab->n_var - tab->n_param - tab->n_div;
2151 for (i = 0; i < bmap->n_eq; ++i) {
2152 if (max)
2153 isl_seq_neg(bmap->eq[i] + o_var,
2154 bmap->eq[i] + o_var, n_var);
2155 tab = add_lexmin_valid_eq(tab, bmap->eq[i]);
2156 if (max)
2157 isl_seq_neg(bmap->eq[i] + o_var,
2158 bmap->eq[i] + o_var, n_var);
2159 if (!tab || tab->empty)
2160 return tab;
2162 if (bmap->n_eq && restore_lexmin(tab) < 0)
2163 goto error;
2164 for (i = 0; i < bmap->n_ineq; ++i) {
2165 if (max)
2166 isl_seq_neg(bmap->ineq[i] + o_var,
2167 bmap->ineq[i] + o_var, n_var);
2168 tab = add_lexmin_ineq(tab, bmap->ineq[i]);
2169 if (max)
2170 isl_seq_neg(bmap->ineq[i] + o_var,
2171 bmap->ineq[i] + o_var, n_var);
2172 if (!tab || tab->empty)
2173 return tab;
2175 return tab;
2176 error:
2177 isl_tab_free(tab);
2178 return NULL;
2181 /* Given a main tableau where more than one row requires a split,
2182 * determine and return the "best" row to split on.
2184 * Given two rows in the main tableau, if the inequality corresponding
2185 * to the first row is redundant with respect to that of the second row
2186 * in the current tableau, then it is better to split on the second row,
2187 * since in the positive part, both rows will be positive.
2188 * (In the negative part a pivot will have to be performed and just about
2189 * anything can happen to the sign of the other row.)
2191 * As a simple heuristic, we therefore select the row that makes the most
2192 * of the other rows redundant.
2194 * Perhaps it would also be useful to look at the number of constraints
2195 * that conflict with any given constraint.
2197 * best is the best row so far (-1 when we have not found any row yet).
2198 * best_r is the number of other rows made redundant by row best.
2199 * When best is still -1, bset_r is meaningless, but it is initialized
2200 * to some arbitrary value (0) anyway. Without this redundant initialization
2201 * valgrind may warn about uninitialized memory accesses when isl
2202 * is compiled with some versions of gcc.
2204 static int best_split(struct isl_tab *tab, struct isl_tab *context_tab)
2206 struct isl_tab_undo *snap;
2207 int split;
2208 int row;
2209 int best = -1;
2210 int best_r = 0;
2212 if (isl_tab_extend_cons(context_tab, 2) < 0)
2213 return -1;
2215 snap = isl_tab_snap(context_tab);
2217 for (split = tab->n_redundant; split < tab->n_row; ++split) {
2218 struct isl_tab_undo *snap2;
2219 struct isl_vec *ineq = NULL;
2220 int r = 0;
2221 int ok;
2223 if (!isl_tab_var_from_row(tab, split)->is_nonneg)
2224 continue;
2225 if (tab->row_sign[split] != isl_tab_row_any)
2226 continue;
2228 ineq = get_row_parameter_ineq(tab, split);
2229 if (!ineq)
2230 return -1;
2231 ok = isl_tab_add_ineq(context_tab, ineq->el) >= 0;
2232 isl_vec_free(ineq);
2233 if (!ok)
2234 return -1;
2236 snap2 = isl_tab_snap(context_tab);
2238 for (row = tab->n_redundant; row < tab->n_row; ++row) {
2239 struct isl_tab_var *var;
2241 if (row == split)
2242 continue;
2243 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
2244 continue;
2245 if (tab->row_sign[row] != isl_tab_row_any)
2246 continue;
2248 ineq = get_row_parameter_ineq(tab, row);
2249 if (!ineq)
2250 return -1;
2251 ok = isl_tab_add_ineq(context_tab, ineq->el) >= 0;
2252 isl_vec_free(ineq);
2253 if (!ok)
2254 return -1;
2255 var = &context_tab->con[context_tab->n_con - 1];
2256 if (!context_tab->empty &&
2257 !isl_tab_min_at_most_neg_one(context_tab, var))
2258 r++;
2259 if (isl_tab_rollback(context_tab, snap2) < 0)
2260 return -1;
2262 if (best == -1 || r > best_r) {
2263 best = split;
2264 best_r = r;
2266 if (isl_tab_rollback(context_tab, snap) < 0)
2267 return -1;
2270 return best;
2273 static struct isl_basic_set *context_lex_peek_basic_set(
2274 struct isl_context *context)
2276 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2277 if (!clex->tab)
2278 return NULL;
2279 return isl_tab_peek_bset(clex->tab);
2282 static struct isl_tab *context_lex_peek_tab(struct isl_context *context)
2284 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2285 return clex->tab;
2288 static void context_lex_add_eq(struct isl_context *context, isl_int *eq,
2289 int check, int update)
2291 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2292 if (isl_tab_extend_cons(clex->tab, 2) < 0)
2293 goto error;
2294 if (add_lexmin_eq(clex->tab, eq) < 0)
2295 goto error;
2296 if (check) {
2297 int v = tab_has_valid_sample(clex->tab, eq, 1);
2298 if (v < 0)
2299 goto error;
2300 if (!v)
2301 clex->tab = check_integer_feasible(clex->tab);
2303 if (update)
2304 clex->tab = check_samples(clex->tab, eq, 1);
2305 return;
2306 error:
2307 isl_tab_free(clex->tab);
2308 clex->tab = NULL;
2311 static void context_lex_add_ineq(struct isl_context *context, isl_int *ineq,
2312 int check, int update)
2314 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2315 if (isl_tab_extend_cons(clex->tab, 1) < 0)
2316 goto error;
2317 clex->tab = add_lexmin_ineq(clex->tab, ineq);
2318 if (check) {
2319 int v = tab_has_valid_sample(clex->tab, ineq, 0);
2320 if (v < 0)
2321 goto error;
2322 if (!v)
2323 clex->tab = check_integer_feasible(clex->tab);
2325 if (update)
2326 clex->tab = check_samples(clex->tab, ineq, 0);
2327 return;
2328 error:
2329 isl_tab_free(clex->tab);
2330 clex->tab = NULL;
2333 static int context_lex_add_ineq_wrap(void *user, isl_int *ineq)
2335 struct isl_context *context = (struct isl_context *)user;
2336 context_lex_add_ineq(context, ineq, 0, 0);
2337 return context->op->is_ok(context) ? 0 : -1;
2340 /* Check which signs can be obtained by "ineq" on all the currently
2341 * active sample values. See row_sign for more information.
2343 static enum isl_tab_row_sign tab_ineq_sign(struct isl_tab *tab, isl_int *ineq,
2344 int strict)
2346 int i;
2347 int sgn;
2348 isl_int tmp;
2349 enum isl_tab_row_sign res = isl_tab_row_unknown;
2351 isl_assert(tab->mat->ctx, tab->samples, return isl_tab_row_unknown);
2352 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var,
2353 return isl_tab_row_unknown);
2355 isl_int_init(tmp);
2356 for (i = tab->n_outside; i < tab->n_sample; ++i) {
2357 isl_seq_inner_product(tab->samples->row[i], ineq,
2358 1 + tab->n_var, &tmp);
2359 sgn = isl_int_sgn(tmp);
2360 if (sgn > 0 || (sgn == 0 && strict)) {
2361 if (res == isl_tab_row_unknown)
2362 res = isl_tab_row_pos;
2363 if (res == isl_tab_row_neg)
2364 res = isl_tab_row_any;
2366 if (sgn < 0) {
2367 if (res == isl_tab_row_unknown)
2368 res = isl_tab_row_neg;
2369 if (res == isl_tab_row_pos)
2370 res = isl_tab_row_any;
2372 if (res == isl_tab_row_any)
2373 break;
2375 isl_int_clear(tmp);
2377 return res;
2380 static enum isl_tab_row_sign context_lex_ineq_sign(struct isl_context *context,
2381 isl_int *ineq, int strict)
2383 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2384 return tab_ineq_sign(clex->tab, ineq, strict);
2387 /* Check whether "ineq" can be added to the tableau without rendering
2388 * it infeasible.
2390 static int context_lex_test_ineq(struct isl_context *context, isl_int *ineq)
2392 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2393 struct isl_tab_undo *snap;
2394 int feasible;
2396 if (!clex->tab)
2397 return -1;
2399 if (isl_tab_extend_cons(clex->tab, 1) < 0)
2400 return -1;
2402 snap = isl_tab_snap(clex->tab);
2403 if (isl_tab_push_basis(clex->tab) < 0)
2404 return -1;
2405 clex->tab = add_lexmin_ineq(clex->tab, ineq);
2406 clex->tab = check_integer_feasible(clex->tab);
2407 if (!clex->tab)
2408 return -1;
2409 feasible = !clex->tab->empty;
2410 if (isl_tab_rollback(clex->tab, snap) < 0)
2411 return -1;
2413 return feasible;
2416 static int context_lex_get_div(struct isl_context *context, struct isl_tab *tab,
2417 struct isl_vec *div)
2419 return get_div(tab, context, div);
2422 /* Insert a div specified by "div" to the context tableau at position "pos" and
2423 * return isl_bool_true if the div is obviously non-negative.
2424 * context_tab_add_div will always return isl_bool_true, because all variables
2425 * in a isl_context_lex tableau are non-negative.
2426 * However, if we are using a big parameter in the context, then this only
2427 * reflects the non-negativity of the variable used to _encode_ the
2428 * div, i.e., div' = M + div, so we can't draw any conclusions.
2430 static isl_bool context_lex_insert_div(struct isl_context *context, int pos,
2431 __isl_keep isl_vec *div)
2433 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2434 isl_bool nonneg;
2435 nonneg = context_tab_insert_div(clex->tab, pos, div,
2436 context_lex_add_ineq_wrap, context);
2437 if (nonneg < 0)
2438 return isl_bool_error;
2439 if (clex->tab->M)
2440 return isl_bool_false;
2441 return nonneg;
2444 static int context_lex_detect_equalities(struct isl_context *context,
2445 struct isl_tab *tab)
2447 return 0;
2450 static int context_lex_best_split(struct isl_context *context,
2451 struct isl_tab *tab)
2453 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2454 struct isl_tab_undo *snap;
2455 int r;
2457 snap = isl_tab_snap(clex->tab);
2458 if (isl_tab_push_basis(clex->tab) < 0)
2459 return -1;
2460 r = best_split(tab, clex->tab);
2462 if (r >= 0 && isl_tab_rollback(clex->tab, snap) < 0)
2463 return -1;
2465 return r;
2468 static int context_lex_is_empty(struct isl_context *context)
2470 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2471 if (!clex->tab)
2472 return -1;
2473 return clex->tab->empty;
2476 static void *context_lex_save(struct isl_context *context)
2478 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2479 struct isl_tab_undo *snap;
2481 snap = isl_tab_snap(clex->tab);
2482 if (isl_tab_push_basis(clex->tab) < 0)
2483 return NULL;
2484 if (isl_tab_save_samples(clex->tab) < 0)
2485 return NULL;
2487 return snap;
2490 static void context_lex_restore(struct isl_context *context, void *save)
2492 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2493 if (isl_tab_rollback(clex->tab, (struct isl_tab_undo *)save) < 0) {
2494 isl_tab_free(clex->tab);
2495 clex->tab = NULL;
2499 static void context_lex_discard(void *save)
2503 static int context_lex_is_ok(struct isl_context *context)
2505 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2506 return !!clex->tab;
2509 /* For each variable in the context tableau, check if the variable can
2510 * only attain non-negative values. If so, mark the parameter as non-negative
2511 * in the main tableau. This allows for a more direct identification of some
2512 * cases of violated constraints.
2514 static struct isl_tab *tab_detect_nonnegative_parameters(struct isl_tab *tab,
2515 struct isl_tab *context_tab)
2517 int i;
2518 struct isl_tab_undo *snap;
2519 struct isl_vec *ineq = NULL;
2520 struct isl_tab_var *var;
2521 int n;
2523 if (context_tab->n_var == 0)
2524 return tab;
2526 ineq = isl_vec_alloc(tab->mat->ctx, 1 + context_tab->n_var);
2527 if (!ineq)
2528 goto error;
2530 if (isl_tab_extend_cons(context_tab, 1) < 0)
2531 goto error;
2533 snap = isl_tab_snap(context_tab);
2535 n = 0;
2536 isl_seq_clr(ineq->el, ineq->size);
2537 for (i = 0; i < context_tab->n_var; ++i) {
2538 isl_int_set_si(ineq->el[1 + i], 1);
2539 if (isl_tab_add_ineq(context_tab, ineq->el) < 0)
2540 goto error;
2541 var = &context_tab->con[context_tab->n_con - 1];
2542 if (!context_tab->empty &&
2543 !isl_tab_min_at_most_neg_one(context_tab, var)) {
2544 int j = i;
2545 if (i >= tab->n_param)
2546 j = i - tab->n_param + tab->n_var - tab->n_div;
2547 tab->var[j].is_nonneg = 1;
2548 n++;
2550 isl_int_set_si(ineq->el[1 + i], 0);
2551 if (isl_tab_rollback(context_tab, snap) < 0)
2552 goto error;
2555 if (context_tab->M && n == context_tab->n_var) {
2556 context_tab->mat = isl_mat_drop_cols(context_tab->mat, 2, 1);
2557 context_tab->M = 0;
2560 isl_vec_free(ineq);
2561 return tab;
2562 error:
2563 isl_vec_free(ineq);
2564 isl_tab_free(tab);
2565 return NULL;
2568 static struct isl_tab *context_lex_detect_nonnegative_parameters(
2569 struct isl_context *context, struct isl_tab *tab)
2571 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2572 struct isl_tab_undo *snap;
2574 if (!tab)
2575 return NULL;
2577 snap = isl_tab_snap(clex->tab);
2578 if (isl_tab_push_basis(clex->tab) < 0)
2579 goto error;
2581 tab = tab_detect_nonnegative_parameters(tab, clex->tab);
2583 if (isl_tab_rollback(clex->tab, snap) < 0)
2584 goto error;
2586 return tab;
2587 error:
2588 isl_tab_free(tab);
2589 return NULL;
2592 static void context_lex_invalidate(struct isl_context *context)
2594 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2595 isl_tab_free(clex->tab);
2596 clex->tab = NULL;
2599 static __isl_null struct isl_context *context_lex_free(
2600 struct isl_context *context)
2602 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2603 isl_tab_free(clex->tab);
2604 free(clex);
2606 return NULL;
2609 struct isl_context_op isl_context_lex_op = {
2610 context_lex_detect_nonnegative_parameters,
2611 context_lex_peek_basic_set,
2612 context_lex_peek_tab,
2613 context_lex_add_eq,
2614 context_lex_add_ineq,
2615 context_lex_ineq_sign,
2616 context_lex_test_ineq,
2617 context_lex_get_div,
2618 context_lex_insert_div,
2619 context_lex_detect_equalities,
2620 context_lex_best_split,
2621 context_lex_is_empty,
2622 context_lex_is_ok,
2623 context_lex_save,
2624 context_lex_restore,
2625 context_lex_discard,
2626 context_lex_invalidate,
2627 context_lex_free,
2630 static struct isl_tab *context_tab_for_lexmin(struct isl_basic_set *bset)
2632 struct isl_tab *tab;
2634 if (!bset)
2635 return NULL;
2636 tab = tab_for_lexmin((struct isl_basic_map *)bset, NULL, 1, 0);
2637 if (!tab)
2638 goto error;
2639 if (isl_tab_track_bset(tab, bset) < 0)
2640 goto error;
2641 tab = isl_tab_init_samples(tab);
2642 return tab;
2643 error:
2644 isl_basic_set_free(bset);
2645 return NULL;
2648 static struct isl_context *isl_context_lex_alloc(struct isl_basic_set *dom)
2650 struct isl_context_lex *clex;
2652 if (!dom)
2653 return NULL;
2655 clex = isl_alloc_type(dom->ctx, struct isl_context_lex);
2656 if (!clex)
2657 return NULL;
2659 clex->context.op = &isl_context_lex_op;
2661 clex->tab = context_tab_for_lexmin(isl_basic_set_copy(dom));
2662 if (restore_lexmin(clex->tab) < 0)
2663 goto error;
2664 clex->tab = check_integer_feasible(clex->tab);
2665 if (!clex->tab)
2666 goto error;
2668 return &clex->context;
2669 error:
2670 clex->context.op->free(&clex->context);
2671 return NULL;
2674 /* Representation of the context when using generalized basis reduction.
2676 * "shifted" contains the offsets of the unit hypercubes that lie inside the
2677 * context. Any rational point in "shifted" can therefore be rounded
2678 * up to an integer point in the context.
2679 * If the context is constrained by any equality, then "shifted" is not used
2680 * as it would be empty.
2682 struct isl_context_gbr {
2683 struct isl_context context;
2684 struct isl_tab *tab;
2685 struct isl_tab *shifted;
2686 struct isl_tab *cone;
2689 static struct isl_tab *context_gbr_detect_nonnegative_parameters(
2690 struct isl_context *context, struct isl_tab *tab)
2692 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2693 if (!tab)
2694 return NULL;
2695 return tab_detect_nonnegative_parameters(tab, cgbr->tab);
2698 static struct isl_basic_set *context_gbr_peek_basic_set(
2699 struct isl_context *context)
2701 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2702 if (!cgbr->tab)
2703 return NULL;
2704 return isl_tab_peek_bset(cgbr->tab);
2707 static struct isl_tab *context_gbr_peek_tab(struct isl_context *context)
2709 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2710 return cgbr->tab;
2713 /* Initialize the "shifted" tableau of the context, which
2714 * contains the constraints of the original tableau shifted
2715 * by the sum of all negative coefficients. This ensures
2716 * that any rational point in the shifted tableau can
2717 * be rounded up to yield an integer point in the original tableau.
2719 static void gbr_init_shifted(struct isl_context_gbr *cgbr)
2721 int i, j;
2722 struct isl_vec *cst;
2723 struct isl_basic_set *bset = isl_tab_peek_bset(cgbr->tab);
2724 unsigned dim = isl_basic_set_total_dim(bset);
2726 cst = isl_vec_alloc(cgbr->tab->mat->ctx, bset->n_ineq);
2727 if (!cst)
2728 return;
2730 for (i = 0; i < bset->n_ineq; ++i) {
2731 isl_int_set(cst->el[i], bset->ineq[i][0]);
2732 for (j = 0; j < dim; ++j) {
2733 if (!isl_int_is_neg(bset->ineq[i][1 + j]))
2734 continue;
2735 isl_int_add(bset->ineq[i][0], bset->ineq[i][0],
2736 bset->ineq[i][1 + j]);
2740 cgbr->shifted = isl_tab_from_basic_set(bset, 0);
2742 for (i = 0; i < bset->n_ineq; ++i)
2743 isl_int_set(bset->ineq[i][0], cst->el[i]);
2745 isl_vec_free(cst);
2748 /* Check if the shifted tableau is non-empty, and if so
2749 * use the sample point to construct an integer point
2750 * of the context tableau.
2752 static struct isl_vec *gbr_get_shifted_sample(struct isl_context_gbr *cgbr)
2754 struct isl_vec *sample;
2756 if (!cgbr->shifted)
2757 gbr_init_shifted(cgbr);
2758 if (!cgbr->shifted)
2759 return NULL;
2760 if (cgbr->shifted->empty)
2761 return isl_vec_alloc(cgbr->tab->mat->ctx, 0);
2763 sample = isl_tab_get_sample_value(cgbr->shifted);
2764 sample = isl_vec_ceil(sample);
2766 return sample;
2769 static struct isl_basic_set *drop_constant_terms(struct isl_basic_set *bset)
2771 int i;
2773 if (!bset)
2774 return NULL;
2776 for (i = 0; i < bset->n_eq; ++i)
2777 isl_int_set_si(bset->eq[i][0], 0);
2779 for (i = 0; i < bset->n_ineq; ++i)
2780 isl_int_set_si(bset->ineq[i][0], 0);
2782 return bset;
2785 static int use_shifted(struct isl_context_gbr *cgbr)
2787 if (!cgbr->tab)
2788 return 0;
2789 return cgbr->tab->bmap->n_eq == 0 && cgbr->tab->bmap->n_div == 0;
2792 static struct isl_vec *gbr_get_sample(struct isl_context_gbr *cgbr)
2794 struct isl_basic_set *bset;
2795 struct isl_basic_set *cone;
2797 if (isl_tab_sample_is_integer(cgbr->tab))
2798 return isl_tab_get_sample_value(cgbr->tab);
2800 if (use_shifted(cgbr)) {
2801 struct isl_vec *sample;
2803 sample = gbr_get_shifted_sample(cgbr);
2804 if (!sample || sample->size > 0)
2805 return sample;
2807 isl_vec_free(sample);
2810 if (!cgbr->cone) {
2811 bset = isl_tab_peek_bset(cgbr->tab);
2812 cgbr->cone = isl_tab_from_recession_cone(bset, 0);
2813 if (!cgbr->cone)
2814 return NULL;
2815 if (isl_tab_track_bset(cgbr->cone,
2816 isl_basic_set_copy(bset)) < 0)
2817 return NULL;
2819 if (isl_tab_detect_implicit_equalities(cgbr->cone) < 0)
2820 return NULL;
2822 if (cgbr->cone->n_dead == cgbr->cone->n_col) {
2823 struct isl_vec *sample;
2824 struct isl_tab_undo *snap;
2826 if (cgbr->tab->basis) {
2827 if (cgbr->tab->basis->n_col != 1 + cgbr->tab->n_var) {
2828 isl_mat_free(cgbr->tab->basis);
2829 cgbr->tab->basis = NULL;
2831 cgbr->tab->n_zero = 0;
2832 cgbr->tab->n_unbounded = 0;
2835 snap = isl_tab_snap(cgbr->tab);
2837 sample = isl_tab_sample(cgbr->tab);
2839 if (!sample || isl_tab_rollback(cgbr->tab, snap) < 0) {
2840 isl_vec_free(sample);
2841 return NULL;
2844 return sample;
2847 cone = isl_basic_set_dup(isl_tab_peek_bset(cgbr->cone));
2848 cone = drop_constant_terms(cone);
2849 cone = isl_basic_set_update_from_tab(cone, cgbr->cone);
2850 cone = isl_basic_set_underlying_set(cone);
2851 cone = isl_basic_set_gauss(cone, NULL);
2853 bset = isl_basic_set_dup(isl_tab_peek_bset(cgbr->tab));
2854 bset = isl_basic_set_update_from_tab(bset, cgbr->tab);
2855 bset = isl_basic_set_underlying_set(bset);
2856 bset = isl_basic_set_gauss(bset, NULL);
2858 return isl_basic_set_sample_with_cone(bset, cone);
2861 static void check_gbr_integer_feasible(struct isl_context_gbr *cgbr)
2863 struct isl_vec *sample;
2865 if (!cgbr->tab)
2866 return;
2868 if (cgbr->tab->empty)
2869 return;
2871 sample = gbr_get_sample(cgbr);
2872 if (!sample)
2873 goto error;
2875 if (sample->size == 0) {
2876 isl_vec_free(sample);
2877 if (isl_tab_mark_empty(cgbr->tab) < 0)
2878 goto error;
2879 return;
2882 if (isl_tab_add_sample(cgbr->tab, sample) < 0)
2883 goto error;
2885 return;
2886 error:
2887 isl_tab_free(cgbr->tab);
2888 cgbr->tab = NULL;
2891 static struct isl_tab *add_gbr_eq(struct isl_tab *tab, isl_int *eq)
2893 if (!tab)
2894 return NULL;
2896 if (isl_tab_extend_cons(tab, 2) < 0)
2897 goto error;
2899 if (isl_tab_add_eq(tab, eq) < 0)
2900 goto error;
2902 return tab;
2903 error:
2904 isl_tab_free(tab);
2905 return NULL;
2908 /* Add the equality described by "eq" to the context.
2909 * If "check" is set, then we check if the context is empty after
2910 * adding the equality.
2911 * If "update" is set, then we check if the samples are still valid.
2913 * We do not explicitly add shifted copies of the equality to
2914 * cgbr->shifted since they would conflict with each other.
2915 * Instead, we directly mark cgbr->shifted empty.
2917 static void context_gbr_add_eq(struct isl_context *context, isl_int *eq,
2918 int check, int update)
2920 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2922 cgbr->tab = add_gbr_eq(cgbr->tab, eq);
2924 if (cgbr->shifted && !cgbr->shifted->empty && use_shifted(cgbr)) {
2925 if (isl_tab_mark_empty(cgbr->shifted) < 0)
2926 goto error;
2929 if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
2930 if (isl_tab_extend_cons(cgbr->cone, 2) < 0)
2931 goto error;
2932 if (isl_tab_add_eq(cgbr->cone, eq) < 0)
2933 goto error;
2936 if (check) {
2937 int v = tab_has_valid_sample(cgbr->tab, eq, 1);
2938 if (v < 0)
2939 goto error;
2940 if (!v)
2941 check_gbr_integer_feasible(cgbr);
2943 if (update)
2944 cgbr->tab = check_samples(cgbr->tab, eq, 1);
2945 return;
2946 error:
2947 isl_tab_free(cgbr->tab);
2948 cgbr->tab = NULL;
2951 static void add_gbr_ineq(struct isl_context_gbr *cgbr, isl_int *ineq)
2953 if (!cgbr->tab)
2954 return;
2956 if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
2957 goto error;
2959 if (isl_tab_add_ineq(cgbr->tab, ineq) < 0)
2960 goto error;
2962 if (cgbr->shifted && !cgbr->shifted->empty && use_shifted(cgbr)) {
2963 int i;
2964 unsigned dim;
2965 dim = isl_basic_map_total_dim(cgbr->tab->bmap);
2967 if (isl_tab_extend_cons(cgbr->shifted, 1) < 0)
2968 goto error;
2970 for (i = 0; i < dim; ++i) {
2971 if (!isl_int_is_neg(ineq[1 + i]))
2972 continue;
2973 isl_int_add(ineq[0], ineq[0], ineq[1 + i]);
2976 if (isl_tab_add_ineq(cgbr->shifted, ineq) < 0)
2977 goto error;
2979 for (i = 0; i < dim; ++i) {
2980 if (!isl_int_is_neg(ineq[1 + i]))
2981 continue;
2982 isl_int_sub(ineq[0], ineq[0], ineq[1 + i]);
2986 if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
2987 if (isl_tab_extend_cons(cgbr->cone, 1) < 0)
2988 goto error;
2989 if (isl_tab_add_ineq(cgbr->cone, ineq) < 0)
2990 goto error;
2993 return;
2994 error:
2995 isl_tab_free(cgbr->tab);
2996 cgbr->tab = NULL;
2999 static void context_gbr_add_ineq(struct isl_context *context, isl_int *ineq,
3000 int check, int update)
3002 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3004 add_gbr_ineq(cgbr, ineq);
3005 if (!cgbr->tab)
3006 return;
3008 if (check) {
3009 int v = tab_has_valid_sample(cgbr->tab, ineq, 0);
3010 if (v < 0)
3011 goto error;
3012 if (!v)
3013 check_gbr_integer_feasible(cgbr);
3015 if (update)
3016 cgbr->tab = check_samples(cgbr->tab, ineq, 0);
3017 return;
3018 error:
3019 isl_tab_free(cgbr->tab);
3020 cgbr->tab = NULL;
3023 static int context_gbr_add_ineq_wrap(void *user, isl_int *ineq)
3025 struct isl_context *context = (struct isl_context *)user;
3026 context_gbr_add_ineq(context, ineq, 0, 0);
3027 return context->op->is_ok(context) ? 0 : -1;
3030 static enum isl_tab_row_sign context_gbr_ineq_sign(struct isl_context *context,
3031 isl_int *ineq, int strict)
3033 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3034 return tab_ineq_sign(cgbr->tab, ineq, strict);
3037 /* Check whether "ineq" can be added to the tableau without rendering
3038 * it infeasible.
3040 static int context_gbr_test_ineq(struct isl_context *context, isl_int *ineq)
3042 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3043 struct isl_tab_undo *snap;
3044 struct isl_tab_undo *shifted_snap = NULL;
3045 struct isl_tab_undo *cone_snap = NULL;
3046 int feasible;
3048 if (!cgbr->tab)
3049 return -1;
3051 if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
3052 return -1;
3054 snap = isl_tab_snap(cgbr->tab);
3055 if (cgbr->shifted)
3056 shifted_snap = isl_tab_snap(cgbr->shifted);
3057 if (cgbr->cone)
3058 cone_snap = isl_tab_snap(cgbr->cone);
3059 add_gbr_ineq(cgbr, ineq);
3060 check_gbr_integer_feasible(cgbr);
3061 if (!cgbr->tab)
3062 return -1;
3063 feasible = !cgbr->tab->empty;
3064 if (isl_tab_rollback(cgbr->tab, snap) < 0)
3065 return -1;
3066 if (shifted_snap) {
3067 if (isl_tab_rollback(cgbr->shifted, shifted_snap))
3068 return -1;
3069 } else if (cgbr->shifted) {
3070 isl_tab_free(cgbr->shifted);
3071 cgbr->shifted = NULL;
3073 if (cone_snap) {
3074 if (isl_tab_rollback(cgbr->cone, cone_snap))
3075 return -1;
3076 } else if (cgbr->cone) {
3077 isl_tab_free(cgbr->cone);
3078 cgbr->cone = NULL;
3081 return feasible;
3084 /* Return the column of the last of the variables associated to
3085 * a column that has a non-zero coefficient.
3086 * This function is called in a context where only coefficients
3087 * of parameters or divs can be non-zero.
3089 static int last_non_zero_var_col(struct isl_tab *tab, isl_int *p)
3091 int i;
3092 int col;
3094 if (tab->n_var == 0)
3095 return -1;
3097 for (i = tab->n_var - 1; i >= 0; --i) {
3098 if (i >= tab->n_param && i < tab->n_var - tab->n_div)
3099 continue;
3100 if (tab->var[i].is_row)
3101 continue;
3102 col = tab->var[i].index;
3103 if (!isl_int_is_zero(p[col]))
3104 return col;
3107 return -1;
3110 /* Look through all the recently added equalities in the context
3111 * to see if we can propagate any of them to the main tableau.
3113 * The newly added equalities in the context are encoded as pairs
3114 * of inequalities starting at inequality "first".
3116 * We tentatively add each of these equalities to the main tableau
3117 * and if this happens to result in a row with a final coefficient
3118 * that is one or negative one, we use it to kill a column
3119 * in the main tableau. Otherwise, we discard the tentatively
3120 * added row.
3121 * This tentative addition of equality constraints turns
3122 * on the undo facility of the tableau. Turn it off again
3123 * at the end, assuming it was turned off to begin with.
3125 * Return 0 on success and -1 on failure.
3127 static int propagate_equalities(struct isl_context_gbr *cgbr,
3128 struct isl_tab *tab, unsigned first)
3130 int i;
3131 struct isl_vec *eq = NULL;
3132 isl_bool needs_undo;
3134 needs_undo = isl_tab_need_undo(tab);
3135 if (needs_undo < 0)
3136 goto error;
3137 eq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
3138 if (!eq)
3139 goto error;
3141 if (isl_tab_extend_cons(tab, (cgbr->tab->bmap->n_ineq - first)/2) < 0)
3142 goto error;
3144 isl_seq_clr(eq->el + 1 + tab->n_param,
3145 tab->n_var - tab->n_param - tab->n_div);
3146 for (i = first; i < cgbr->tab->bmap->n_ineq; i += 2) {
3147 int j;
3148 int r;
3149 struct isl_tab_undo *snap;
3150 snap = isl_tab_snap(tab);
3152 isl_seq_cpy(eq->el, cgbr->tab->bmap->ineq[i], 1 + tab->n_param);
3153 isl_seq_cpy(eq->el + 1 + tab->n_var - tab->n_div,
3154 cgbr->tab->bmap->ineq[i] + 1 + tab->n_param,
3155 tab->n_div);
3157 r = isl_tab_add_row(tab, eq->el);
3158 if (r < 0)
3159 goto error;
3160 r = tab->con[r].index;
3161 j = last_non_zero_var_col(tab, tab->mat->row[r] + 2 + tab->M);
3162 if (j < 0 || j < tab->n_dead ||
3163 !isl_int_is_one(tab->mat->row[r][0]) ||
3164 (!isl_int_is_one(tab->mat->row[r][2 + tab->M + j]) &&
3165 !isl_int_is_negone(tab->mat->row[r][2 + tab->M + j]))) {
3166 if (isl_tab_rollback(tab, snap) < 0)
3167 goto error;
3168 continue;
3170 if (isl_tab_pivot(tab, r, j) < 0)
3171 goto error;
3172 if (isl_tab_kill_col(tab, j) < 0)
3173 goto error;
3175 if (restore_lexmin(tab) < 0)
3176 goto error;
3179 if (!needs_undo)
3180 isl_tab_clear_undo(tab);
3181 isl_vec_free(eq);
3183 return 0;
3184 error:
3185 isl_vec_free(eq);
3186 isl_tab_free(cgbr->tab);
3187 cgbr->tab = NULL;
3188 return -1;
3191 static int context_gbr_detect_equalities(struct isl_context *context,
3192 struct isl_tab *tab)
3194 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3195 unsigned n_ineq;
3197 if (!cgbr->cone) {
3198 struct isl_basic_set *bset = isl_tab_peek_bset(cgbr->tab);
3199 cgbr->cone = isl_tab_from_recession_cone(bset, 0);
3200 if (!cgbr->cone)
3201 goto error;
3202 if (isl_tab_track_bset(cgbr->cone,
3203 isl_basic_set_copy(bset)) < 0)
3204 goto error;
3206 if (isl_tab_detect_implicit_equalities(cgbr->cone) < 0)
3207 goto error;
3209 n_ineq = cgbr->tab->bmap->n_ineq;
3210 cgbr->tab = isl_tab_detect_equalities(cgbr->tab, cgbr->cone);
3211 if (!cgbr->tab)
3212 return -1;
3213 if (cgbr->tab->bmap->n_ineq > n_ineq &&
3214 propagate_equalities(cgbr, tab, n_ineq) < 0)
3215 return -1;
3217 return 0;
3218 error:
3219 isl_tab_free(cgbr->tab);
3220 cgbr->tab = NULL;
3221 return -1;
3224 static int context_gbr_get_div(struct isl_context *context, struct isl_tab *tab,
3225 struct isl_vec *div)
3227 return get_div(tab, context, div);
3230 static isl_bool context_gbr_insert_div(struct isl_context *context, int pos,
3231 __isl_keep isl_vec *div)
3233 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3234 if (cgbr->cone) {
3235 int r, n_div, o_div;
3237 n_div = isl_basic_map_dim(cgbr->cone->bmap, isl_dim_div);
3238 o_div = cgbr->cone->n_var - n_div;
3240 if (isl_tab_extend_cons(cgbr->cone, 3) < 0)
3241 return isl_bool_error;
3242 if (isl_tab_extend_vars(cgbr->cone, 1) < 0)
3243 return isl_bool_error;
3244 if ((r = isl_tab_insert_var(cgbr->cone, pos)) <0)
3245 return isl_bool_error;
3247 cgbr->cone->bmap = isl_basic_map_insert_div(cgbr->cone->bmap,
3248 r - o_div, div);
3249 if (!cgbr->cone->bmap)
3250 return isl_bool_error;
3251 if (isl_tab_push_var(cgbr->cone, isl_tab_undo_bmap_div,
3252 &cgbr->cone->var[r]) < 0)
3253 return isl_bool_error;
3255 return context_tab_insert_div(cgbr->tab, pos, div,
3256 context_gbr_add_ineq_wrap, context);
3259 static int context_gbr_best_split(struct isl_context *context,
3260 struct isl_tab *tab)
3262 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3263 struct isl_tab_undo *snap;
3264 int r;
3266 snap = isl_tab_snap(cgbr->tab);
3267 r = best_split(tab, cgbr->tab);
3269 if (r >= 0 && isl_tab_rollback(cgbr->tab, snap) < 0)
3270 return -1;
3272 return r;
3275 static int context_gbr_is_empty(struct isl_context *context)
3277 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3278 if (!cgbr->tab)
3279 return -1;
3280 return cgbr->tab->empty;
3283 struct isl_gbr_tab_undo {
3284 struct isl_tab_undo *tab_snap;
3285 struct isl_tab_undo *shifted_snap;
3286 struct isl_tab_undo *cone_snap;
3289 static void *context_gbr_save(struct isl_context *context)
3291 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3292 struct isl_gbr_tab_undo *snap;
3294 if (!cgbr->tab)
3295 return NULL;
3297 snap = isl_alloc_type(cgbr->tab->mat->ctx, struct isl_gbr_tab_undo);
3298 if (!snap)
3299 return NULL;
3301 snap->tab_snap = isl_tab_snap(cgbr->tab);
3302 if (isl_tab_save_samples(cgbr->tab) < 0)
3303 goto error;
3305 if (cgbr->shifted)
3306 snap->shifted_snap = isl_tab_snap(cgbr->shifted);
3307 else
3308 snap->shifted_snap = NULL;
3310 if (cgbr->cone)
3311 snap->cone_snap = isl_tab_snap(cgbr->cone);
3312 else
3313 snap->cone_snap = NULL;
3315 return snap;
3316 error:
3317 free(snap);
3318 return NULL;
3321 static void context_gbr_restore(struct isl_context *context, void *save)
3323 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3324 struct isl_gbr_tab_undo *snap = (struct isl_gbr_tab_undo *)save;
3325 if (!snap)
3326 goto error;
3327 if (isl_tab_rollback(cgbr->tab, snap->tab_snap) < 0)
3328 goto error;
3330 if (snap->shifted_snap) {
3331 if (isl_tab_rollback(cgbr->shifted, snap->shifted_snap) < 0)
3332 goto error;
3333 } else if (cgbr->shifted) {
3334 isl_tab_free(cgbr->shifted);
3335 cgbr->shifted = NULL;
3338 if (snap->cone_snap) {
3339 if (isl_tab_rollback(cgbr->cone, snap->cone_snap) < 0)
3340 goto error;
3341 } else if (cgbr->cone) {
3342 isl_tab_free(cgbr->cone);
3343 cgbr->cone = NULL;
3346 free(snap);
3348 return;
3349 error:
3350 free(snap);
3351 isl_tab_free(cgbr->tab);
3352 cgbr->tab = NULL;
3355 static void context_gbr_discard(void *save)
3357 struct isl_gbr_tab_undo *snap = (struct isl_gbr_tab_undo *)save;
3358 free(snap);
3361 static int context_gbr_is_ok(struct isl_context *context)
3363 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3364 return !!cgbr->tab;
3367 static void context_gbr_invalidate(struct isl_context *context)
3369 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3370 isl_tab_free(cgbr->tab);
3371 cgbr->tab = NULL;
3374 static __isl_null struct isl_context *context_gbr_free(
3375 struct isl_context *context)
3377 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3378 isl_tab_free(cgbr->tab);
3379 isl_tab_free(cgbr->shifted);
3380 isl_tab_free(cgbr->cone);
3381 free(cgbr);
3383 return NULL;
3386 struct isl_context_op isl_context_gbr_op = {
3387 context_gbr_detect_nonnegative_parameters,
3388 context_gbr_peek_basic_set,
3389 context_gbr_peek_tab,
3390 context_gbr_add_eq,
3391 context_gbr_add_ineq,
3392 context_gbr_ineq_sign,
3393 context_gbr_test_ineq,
3394 context_gbr_get_div,
3395 context_gbr_insert_div,
3396 context_gbr_detect_equalities,
3397 context_gbr_best_split,
3398 context_gbr_is_empty,
3399 context_gbr_is_ok,
3400 context_gbr_save,
3401 context_gbr_restore,
3402 context_gbr_discard,
3403 context_gbr_invalidate,
3404 context_gbr_free,
3407 static struct isl_context *isl_context_gbr_alloc(__isl_keep isl_basic_set *dom)
3409 struct isl_context_gbr *cgbr;
3411 if (!dom)
3412 return NULL;
3414 cgbr = isl_calloc_type(dom->ctx, struct isl_context_gbr);
3415 if (!cgbr)
3416 return NULL;
3418 cgbr->context.op = &isl_context_gbr_op;
3420 cgbr->shifted = NULL;
3421 cgbr->cone = NULL;
3422 cgbr->tab = isl_tab_from_basic_set(dom, 1);
3423 cgbr->tab = isl_tab_init_samples(cgbr->tab);
3424 if (!cgbr->tab)
3425 goto error;
3426 check_gbr_integer_feasible(cgbr);
3428 return &cgbr->context;
3429 error:
3430 cgbr->context.op->free(&cgbr->context);
3431 return NULL;
3434 /* Allocate a context corresponding to "dom".
3435 * The representation specific fields are initialized by
3436 * isl_context_lex_alloc or isl_context_gbr_alloc.
3437 * The shared "n_unknown" field is initialized to the number
3438 * of final unknown integer divisions in "dom".
3440 static struct isl_context *isl_context_alloc(__isl_keep isl_basic_set *dom)
3442 struct isl_context *context;
3443 int first;
3445 if (!dom)
3446 return NULL;
3448 if (dom->ctx->opt->context == ISL_CONTEXT_LEXMIN)
3449 context = isl_context_lex_alloc(dom);
3450 else
3451 context = isl_context_gbr_alloc(dom);
3453 if (!context)
3454 return NULL;
3456 first = isl_basic_set_first_unknown_div(dom);
3457 if (first < 0)
3458 return context->op->free(context);
3459 context->n_unknown = isl_basic_set_dim(dom, isl_dim_div) - first;
3461 return context;
3464 /* Construct an isl_sol_map structure for accumulating the solution.
3465 * If track_empty is set, then we also keep track of the parts
3466 * of the context where there is no solution.
3467 * If max is set, then we are solving a maximization, rather than
3468 * a minimization problem, which means that the variables in the
3469 * tableau have value "M - x" rather than "M + x".
3471 static struct isl_sol *sol_map_init(struct isl_basic_map *bmap,
3472 struct isl_basic_set *dom, int track_empty, int max)
3474 struct isl_sol_map *sol_map = NULL;
3476 if (!bmap)
3477 goto error;
3479 sol_map = isl_calloc_type(bmap->ctx, struct isl_sol_map);
3480 if (!sol_map)
3481 goto error;
3483 sol_map->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
3484 sol_map->sol.dec_level.callback.run = &sol_dec_level_wrap;
3485 sol_map->sol.dec_level.sol = &sol_map->sol;
3486 sol_map->sol.max = max;
3487 sol_map->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
3488 sol_map->sol.add = &sol_map_add_wrap;
3489 sol_map->sol.add_empty = track_empty ? &sol_map_add_empty_wrap : NULL;
3490 sol_map->sol.free = &sol_map_free_wrap;
3491 sol_map->map = isl_map_alloc_space(isl_basic_map_get_space(bmap), 1,
3492 ISL_MAP_DISJOINT);
3493 if (!sol_map->map)
3494 goto error;
3496 sol_map->sol.context = isl_context_alloc(dom);
3497 if (!sol_map->sol.context)
3498 goto error;
3500 if (track_empty) {
3501 sol_map->empty = isl_set_alloc_space(isl_basic_set_get_space(dom),
3502 1, ISL_SET_DISJOINT);
3503 if (!sol_map->empty)
3504 goto error;
3507 isl_basic_set_free(dom);
3508 return &sol_map->sol;
3509 error:
3510 isl_basic_set_free(dom);
3511 sol_map_free(sol_map);
3512 return NULL;
3515 /* Check whether all coefficients of (non-parameter) variables
3516 * are non-positive, meaning that no pivots can be performed on the row.
3518 static int is_critical(struct isl_tab *tab, int row)
3520 int j;
3521 unsigned off = 2 + tab->M;
3523 for (j = tab->n_dead; j < tab->n_col; ++j) {
3524 if (tab->col_var[j] >= 0 &&
3525 (tab->col_var[j] < tab->n_param ||
3526 tab->col_var[j] >= tab->n_var - tab->n_div))
3527 continue;
3529 if (isl_int_is_pos(tab->mat->row[row][off + j]))
3530 return 0;
3533 return 1;
3536 /* Check whether the inequality represented by vec is strict over the integers,
3537 * i.e., there are no integer values satisfying the constraint with
3538 * equality. This happens if the gcd of the coefficients is not a divisor
3539 * of the constant term. If so, scale the constraint down by the gcd
3540 * of the coefficients.
3542 static int is_strict(struct isl_vec *vec)
3544 isl_int gcd;
3545 int strict = 0;
3547 isl_int_init(gcd);
3548 isl_seq_gcd(vec->el + 1, vec->size - 1, &gcd);
3549 if (!isl_int_is_one(gcd)) {
3550 strict = !isl_int_is_divisible_by(vec->el[0], gcd);
3551 isl_int_fdiv_q(vec->el[0], vec->el[0], gcd);
3552 isl_seq_scale_down(vec->el + 1, vec->el + 1, gcd, vec->size-1);
3554 isl_int_clear(gcd);
3556 return strict;
3559 /* Determine the sign of the given row of the main tableau.
3560 * The result is one of
3561 * isl_tab_row_pos: always non-negative; no pivot needed
3562 * isl_tab_row_neg: always non-positive; pivot
3563 * isl_tab_row_any: can be both positive and negative; split
3565 * We first handle some simple cases
3566 * - the row sign may be known already
3567 * - the row may be obviously non-negative
3568 * - the parametric constant may be equal to that of another row
3569 * for which we know the sign. This sign will be either "pos" or
3570 * "any". If it had been "neg" then we would have pivoted before.
3572 * If none of these cases hold, we check the value of the row for each
3573 * of the currently active samples. Based on the signs of these values
3574 * we make an initial determination of the sign of the row.
3576 * all zero -> unk(nown)
3577 * all non-negative -> pos
3578 * all non-positive -> neg
3579 * both negative and positive -> all
3581 * If we end up with "all", we are done.
3582 * Otherwise, we perform a check for positive and/or negative
3583 * values as follows.
3585 * samples neg unk pos
3586 * <0 ? Y N Y N
3587 * pos any pos
3588 * >0 ? Y N Y N
3589 * any neg any neg
3591 * There is no special sign for "zero", because we can usually treat zero
3592 * as either non-negative or non-positive, whatever works out best.
3593 * However, if the row is "critical", meaning that pivoting is impossible
3594 * then we don't want to limp zero with the non-positive case, because
3595 * then we we would lose the solution for those values of the parameters
3596 * where the value of the row is zero. Instead, we treat 0 as non-negative
3597 * ensuring a split if the row can attain both zero and negative values.
3598 * The same happens when the original constraint was one that could not
3599 * be satisfied with equality by any integer values of the parameters.
3600 * In this case, we normalize the constraint, but then a value of zero
3601 * for the normalized constraint is actually a positive value for the
3602 * original constraint, so again we need to treat zero as non-negative.
3603 * In both these cases, we have the following decision tree instead:
3605 * all non-negative -> pos
3606 * all negative -> neg
3607 * both negative and non-negative -> all
3609 * samples neg pos
3610 * <0 ? Y N
3611 * any pos
3612 * >=0 ? Y N
3613 * any neg
3615 static enum isl_tab_row_sign row_sign(struct isl_tab *tab,
3616 struct isl_sol *sol, int row)
3618 struct isl_vec *ineq = NULL;
3619 enum isl_tab_row_sign res = isl_tab_row_unknown;
3620 int critical;
3621 int strict;
3622 int row2;
3624 if (tab->row_sign[row] != isl_tab_row_unknown)
3625 return tab->row_sign[row];
3626 if (is_obviously_nonneg(tab, row))
3627 return isl_tab_row_pos;
3628 for (row2 = tab->n_redundant; row2 < tab->n_row; ++row2) {
3629 if (tab->row_sign[row2] == isl_tab_row_unknown)
3630 continue;
3631 if (identical_parameter_line(tab, row, row2))
3632 return tab->row_sign[row2];
3635 critical = is_critical(tab, row);
3637 ineq = get_row_parameter_ineq(tab, row);
3638 if (!ineq)
3639 goto error;
3641 strict = is_strict(ineq);
3643 res = sol->context->op->ineq_sign(sol->context, ineq->el,
3644 critical || strict);
3646 if (res == isl_tab_row_unknown || res == isl_tab_row_pos) {
3647 /* test for negative values */
3648 int feasible;
3649 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3650 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3652 feasible = sol->context->op->test_ineq(sol->context, ineq->el);
3653 if (feasible < 0)
3654 goto error;
3655 if (!feasible)
3656 res = isl_tab_row_pos;
3657 else
3658 res = (res == isl_tab_row_unknown) ? isl_tab_row_neg
3659 : isl_tab_row_any;
3660 if (res == isl_tab_row_neg) {
3661 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3662 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3666 if (res == isl_tab_row_neg) {
3667 /* test for positive values */
3668 int feasible;
3669 if (!critical && !strict)
3670 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3672 feasible = sol->context->op->test_ineq(sol->context, ineq->el);
3673 if (feasible < 0)
3674 goto error;
3675 if (feasible)
3676 res = isl_tab_row_any;
3679 isl_vec_free(ineq);
3680 return res;
3681 error:
3682 isl_vec_free(ineq);
3683 return isl_tab_row_unknown;
3686 static void find_solutions(struct isl_sol *sol, struct isl_tab *tab);
3688 /* Find solutions for values of the parameters that satisfy the given
3689 * inequality.
3691 * We currently take a snapshot of the context tableau that is reset
3692 * when we return from this function, while we make a copy of the main
3693 * tableau, leaving the original main tableau untouched.
3694 * These are fairly arbitrary choices. Making a copy also of the context
3695 * tableau would obviate the need to undo any changes made to it later,
3696 * while taking a snapshot of the main tableau could reduce memory usage.
3697 * If we were to switch to taking a snapshot of the main tableau,
3698 * we would have to keep in mind that we need to save the row signs
3699 * and that we need to do this before saving the current basis
3700 * such that the basis has been restore before we restore the row signs.
3702 static void find_in_pos(struct isl_sol *sol, struct isl_tab *tab, isl_int *ineq)
3704 void *saved;
3706 if (!sol->context)
3707 goto error;
3708 saved = sol->context->op->save(sol->context);
3710 tab = isl_tab_dup(tab);
3711 if (!tab)
3712 goto error;
3714 sol->context->op->add_ineq(sol->context, ineq, 0, 1);
3716 find_solutions(sol, tab);
3718 if (!sol->error)
3719 sol->context->op->restore(sol->context, saved);
3720 else
3721 sol->context->op->discard(saved);
3722 return;
3723 error:
3724 sol->error = 1;
3727 /* Record the absence of solutions for those values of the parameters
3728 * that do not satisfy the given inequality with equality.
3730 static void no_sol_in_strict(struct isl_sol *sol,
3731 struct isl_tab *tab, struct isl_vec *ineq)
3733 int empty;
3734 void *saved;
3736 if (!sol->context || sol->error)
3737 goto error;
3738 saved = sol->context->op->save(sol->context);
3740 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3742 sol->context->op->add_ineq(sol->context, ineq->el, 1, 0);
3743 if (!sol->context)
3744 goto error;
3746 empty = tab->empty;
3747 tab->empty = 1;
3748 sol_add(sol, tab);
3749 tab->empty = empty;
3751 isl_int_add_ui(ineq->el[0], ineq->el[0], 1);
3753 sol->context->op->restore(sol->context, saved);
3754 return;
3755 error:
3756 sol->error = 1;
3759 /* Reset all row variables that are marked to have a sign that may
3760 * be both positive and negative to have an unknown sign.
3762 static void reset_any_to_unknown(struct isl_tab *tab)
3764 int row;
3766 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3767 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
3768 continue;
3769 if (tab->row_sign[row] == isl_tab_row_any)
3770 tab->row_sign[row] = isl_tab_row_unknown;
3774 /* Compute the lexicographic minimum of the set represented by the main
3775 * tableau "tab" within the context "sol->context_tab".
3776 * On entry the sample value of the main tableau is lexicographically
3777 * less than or equal to this lexicographic minimum.
3778 * Pivots are performed until a feasible point is found, which is then
3779 * necessarily equal to the minimum, or until the tableau is found to
3780 * be infeasible. Some pivots may need to be performed for only some
3781 * feasible values of the context tableau. If so, the context tableau
3782 * is split into a part where the pivot is needed and a part where it is not.
3784 * Whenever we enter the main loop, the main tableau is such that no
3785 * "obvious" pivots need to be performed on it, where "obvious" means
3786 * that the given row can be seen to be negative without looking at
3787 * the context tableau. In particular, for non-parametric problems,
3788 * no pivots need to be performed on the main tableau.
3789 * The caller of find_solutions is responsible for making this property
3790 * hold prior to the first iteration of the loop, while restore_lexmin
3791 * is called before every other iteration.
3793 * Inside the main loop, we first examine the signs of the rows of
3794 * the main tableau within the context of the context tableau.
3795 * If we find a row that is always non-positive for all values of
3796 * the parameters satisfying the context tableau and negative for at
3797 * least one value of the parameters, we perform the appropriate pivot
3798 * and start over. An exception is the case where no pivot can be
3799 * performed on the row. In this case, we require that the sign of
3800 * the row is negative for all values of the parameters (rather than just
3801 * non-positive). This special case is handled inside row_sign, which
3802 * will say that the row can have any sign if it determines that it can
3803 * attain both negative and zero values.
3805 * If we can't find a row that always requires a pivot, but we can find
3806 * one or more rows that require a pivot for some values of the parameters
3807 * (i.e., the row can attain both positive and negative signs), then we split
3808 * the context tableau into two parts, one where we force the sign to be
3809 * non-negative and one where we force is to be negative.
3810 * The non-negative part is handled by a recursive call (through find_in_pos).
3811 * Upon returning from this call, we continue with the negative part and
3812 * perform the required pivot.
3814 * If no such rows can be found, all rows are non-negative and we have
3815 * found a (rational) feasible point. If we only wanted a rational point
3816 * then we are done.
3817 * Otherwise, we check if all values of the sample point of the tableau
3818 * are integral for the variables. If so, we have found the minimal
3819 * integral point and we are done.
3820 * If the sample point is not integral, then we need to make a distinction
3821 * based on whether the constant term is non-integral or the coefficients
3822 * of the parameters. Furthermore, in order to decide how to handle
3823 * the non-integrality, we also need to know whether the coefficients
3824 * of the other columns in the tableau are integral. This leads
3825 * to the following table. The first two rows do not correspond
3826 * to a non-integral sample point and are only mentioned for completeness.
3828 * constant parameters other
3830 * int int int |
3831 * int int rat | -> no problem
3833 * rat int int -> fail
3835 * rat int rat -> cut
3837 * int rat rat |
3838 * rat rat rat | -> parametric cut
3840 * int rat int |
3841 * rat rat int | -> split context
3843 * If the parametric constant is completely integral, then there is nothing
3844 * to be done. If the constant term is non-integral, but all the other
3845 * coefficient are integral, then there is nothing that can be done
3846 * and the tableau has no integral solution.
3847 * If, on the other hand, one or more of the other columns have rational
3848 * coefficients, but the parameter coefficients are all integral, then
3849 * we can perform a regular (non-parametric) cut.
3850 * Finally, if there is any parameter coefficient that is non-integral,
3851 * then we need to involve the context tableau. There are two cases here.
3852 * If at least one other column has a rational coefficient, then we
3853 * can perform a parametric cut in the main tableau by adding a new
3854 * integer division in the context tableau.
3855 * If all other columns have integral coefficients, then we need to
3856 * enforce that the rational combination of parameters (c + \sum a_i y_i)/m
3857 * is always integral. We do this by introducing an integer division
3858 * q = floor((c + \sum a_i y_i)/m) and stipulating that its argument should
3859 * always be integral in the context tableau, i.e., m q = c + \sum a_i y_i.
3860 * Since q is expressed in the tableau as
3861 * c + \sum a_i y_i - m q >= 0
3862 * -c - \sum a_i y_i + m q + m - 1 >= 0
3863 * it is sufficient to add the inequality
3864 * -c - \sum a_i y_i + m q >= 0
3865 * In the part of the context where this inequality does not hold, the
3866 * main tableau is marked as being empty.
3868 static void find_solutions(struct isl_sol *sol, struct isl_tab *tab)
3870 struct isl_context *context;
3871 int r;
3873 if (!tab || sol->error)
3874 goto error;
3876 context = sol->context;
3878 if (tab->empty)
3879 goto done;
3880 if (context->op->is_empty(context))
3881 goto done;
3883 for (r = 0; r >= 0 && tab && !tab->empty; r = restore_lexmin(tab)) {
3884 int flags;
3885 int row;
3886 enum isl_tab_row_sign sgn;
3887 int split = -1;
3888 int n_split = 0;
3890 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3891 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
3892 continue;
3893 sgn = row_sign(tab, sol, row);
3894 if (!sgn)
3895 goto error;
3896 tab->row_sign[row] = sgn;
3897 if (sgn == isl_tab_row_any)
3898 n_split++;
3899 if (sgn == isl_tab_row_any && split == -1)
3900 split = row;
3901 if (sgn == isl_tab_row_neg)
3902 break;
3904 if (row < tab->n_row)
3905 continue;
3906 if (split != -1) {
3907 struct isl_vec *ineq;
3908 if (n_split != 1)
3909 split = context->op->best_split(context, tab);
3910 if (split < 0)
3911 goto error;
3912 ineq = get_row_parameter_ineq(tab, split);
3913 if (!ineq)
3914 goto error;
3915 is_strict(ineq);
3916 reset_any_to_unknown(tab);
3917 tab->row_sign[split] = isl_tab_row_pos;
3918 sol_inc_level(sol);
3919 find_in_pos(sol, tab, ineq->el);
3920 tab->row_sign[split] = isl_tab_row_neg;
3921 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3922 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3923 if (!sol->error)
3924 context->op->add_ineq(context, ineq->el, 0, 1);
3925 isl_vec_free(ineq);
3926 if (sol->error)
3927 goto error;
3928 continue;
3930 if (tab->rational)
3931 break;
3932 row = first_non_integer_row(tab, &flags);
3933 if (row < 0)
3934 break;
3935 if (ISL_FL_ISSET(flags, I_PAR)) {
3936 if (ISL_FL_ISSET(flags, I_VAR)) {
3937 if (isl_tab_mark_empty(tab) < 0)
3938 goto error;
3939 break;
3941 row = add_cut(tab, row);
3942 } else if (ISL_FL_ISSET(flags, I_VAR)) {
3943 struct isl_vec *div;
3944 struct isl_vec *ineq;
3945 int d;
3946 div = get_row_split_div(tab, row);
3947 if (!div)
3948 goto error;
3949 d = context->op->get_div(context, tab, div);
3950 isl_vec_free(div);
3951 if (d < 0)
3952 goto error;
3953 ineq = ineq_for_div(context->op->peek_basic_set(context), d);
3954 if (!ineq)
3955 goto error;
3956 sol_inc_level(sol);
3957 no_sol_in_strict(sol, tab, ineq);
3958 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3959 context->op->add_ineq(context, ineq->el, 1, 1);
3960 isl_vec_free(ineq);
3961 if (sol->error || !context->op->is_ok(context))
3962 goto error;
3963 tab = set_row_cst_to_div(tab, row, d);
3964 if (context->op->is_empty(context))
3965 break;
3966 } else
3967 row = add_parametric_cut(tab, row, context);
3968 if (row < 0)
3969 goto error;
3971 if (r < 0)
3972 goto error;
3973 done:
3974 sol_add(sol, tab);
3975 isl_tab_free(tab);
3976 return;
3977 error:
3978 isl_tab_free(tab);
3979 sol->error = 1;
3982 /* Does "sol" contain a pair of partial solutions that could potentially
3983 * be merged?
3985 * We currently only check that "sol" is not in an error state
3986 * and that there are at least two partial solutions of which the final two
3987 * are defined at the same level.
3989 static int sol_has_mergeable_solutions(struct isl_sol *sol)
3991 if (sol->error)
3992 return 0;
3993 if (!sol->partial)
3994 return 0;
3995 if (!sol->partial->next)
3996 return 0;
3997 return sol->partial->level == sol->partial->next->level;
4000 /* Compute the lexicographic minimum of the set represented by the main
4001 * tableau "tab" within the context "sol->context_tab".
4003 * As a preprocessing step, we first transfer all the purely parametric
4004 * equalities from the main tableau to the context tableau, i.e.,
4005 * parameters that have been pivoted to a row.
4006 * These equalities are ignored by the main algorithm, because the
4007 * corresponding rows may not be marked as being non-negative.
4008 * In parts of the context where the added equality does not hold,
4009 * the main tableau is marked as being empty.
4011 * Before we embark on the actual computation, we save a copy
4012 * of the context. When we return, we check if there are any
4013 * partial solutions that can potentially be merged. If so,
4014 * we perform a rollback to the initial state of the context.
4015 * The merging of partial solutions happens inside calls to
4016 * sol_dec_level that are pushed onto the undo stack of the context.
4017 * If there are no partial solutions that can potentially be merged
4018 * then the rollback is skipped as it would just be wasted effort.
4020 static void find_solutions_main(struct isl_sol *sol, struct isl_tab *tab)
4022 int row;
4023 void *saved;
4025 if (!tab)
4026 goto error;
4028 sol->level = 0;
4030 for (row = tab->n_redundant; row < tab->n_row; ++row) {
4031 int p;
4032 struct isl_vec *eq;
4034 if (tab->row_var[row] < 0)
4035 continue;
4036 if (tab->row_var[row] >= tab->n_param &&
4037 tab->row_var[row] < tab->n_var - tab->n_div)
4038 continue;
4039 if (tab->row_var[row] < tab->n_param)
4040 p = tab->row_var[row];
4041 else
4042 p = tab->row_var[row]
4043 + tab->n_param - (tab->n_var - tab->n_div);
4045 eq = isl_vec_alloc(tab->mat->ctx, 1+tab->n_param+tab->n_div);
4046 if (!eq)
4047 goto error;
4048 get_row_parameter_line(tab, row, eq->el);
4049 isl_int_neg(eq->el[1 + p], tab->mat->row[row][0]);
4050 eq = isl_vec_normalize(eq);
4052 sol_inc_level(sol);
4053 no_sol_in_strict(sol, tab, eq);
4055 isl_seq_neg(eq->el, eq->el, eq->size);
4056 sol_inc_level(sol);
4057 no_sol_in_strict(sol, tab, eq);
4058 isl_seq_neg(eq->el, eq->el, eq->size);
4060 sol->context->op->add_eq(sol->context, eq->el, 1, 1);
4062 isl_vec_free(eq);
4064 if (isl_tab_mark_redundant(tab, row) < 0)
4065 goto error;
4067 if (sol->context->op->is_empty(sol->context))
4068 break;
4070 row = tab->n_redundant - 1;
4073 saved = sol->context->op->save(sol->context);
4075 find_solutions(sol, tab);
4077 if (sol_has_mergeable_solutions(sol))
4078 sol->context->op->restore(sol->context, saved);
4079 else
4080 sol->context->op->discard(saved);
4082 sol->level = 0;
4083 sol_pop(sol);
4085 return;
4086 error:
4087 isl_tab_free(tab);
4088 sol->error = 1;
4091 /* Check if integer division "div" of "dom" also occurs in "bmap".
4092 * If so, return its position within the divs.
4093 * If not, return -1.
4095 static int find_context_div(struct isl_basic_map *bmap,
4096 struct isl_basic_set *dom, unsigned div)
4098 int i;
4099 unsigned b_dim = isl_space_dim(bmap->dim, isl_dim_all);
4100 unsigned d_dim = isl_space_dim(dom->dim, isl_dim_all);
4102 if (isl_int_is_zero(dom->div[div][0]))
4103 return -1;
4104 if (isl_seq_first_non_zero(dom->div[div] + 2 + d_dim, dom->n_div) != -1)
4105 return -1;
4107 for (i = 0; i < bmap->n_div; ++i) {
4108 if (isl_int_is_zero(bmap->div[i][0]))
4109 continue;
4110 if (isl_seq_first_non_zero(bmap->div[i] + 2 + d_dim,
4111 (b_dim - d_dim) + bmap->n_div) != -1)
4112 continue;
4113 if (isl_seq_eq(bmap->div[i], dom->div[div], 2 + d_dim))
4114 return i;
4116 return -1;
4119 /* The correspondence between the variables in the main tableau,
4120 * the context tableau, and the input map and domain is as follows.
4121 * The first n_param and the last n_div variables of the main tableau
4122 * form the variables of the context tableau.
4123 * In the basic map, these n_param variables correspond to the
4124 * parameters and the input dimensions. In the domain, they correspond
4125 * to the parameters and the set dimensions.
4126 * The n_div variables correspond to the integer divisions in the domain.
4127 * To ensure that everything lines up, we may need to copy some of the
4128 * integer divisions of the domain to the map. These have to be placed
4129 * in the same order as those in the context and they have to be placed
4130 * after any other integer divisions that the map may have.
4131 * This function performs the required reordering.
4133 static struct isl_basic_map *align_context_divs(struct isl_basic_map *bmap,
4134 struct isl_basic_set *dom)
4136 int i;
4137 int common = 0;
4138 int other;
4140 for (i = 0; i < dom->n_div; ++i)
4141 if (find_context_div(bmap, dom, i) != -1)
4142 common++;
4143 other = bmap->n_div - common;
4144 if (dom->n_div - common > 0) {
4145 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
4146 dom->n_div - common, 0, 0);
4147 if (!bmap)
4148 return NULL;
4150 for (i = 0; i < dom->n_div; ++i) {
4151 int pos = find_context_div(bmap, dom, i);
4152 if (pos < 0) {
4153 pos = isl_basic_map_alloc_div(bmap);
4154 if (pos < 0)
4155 goto error;
4156 isl_int_set_si(bmap->div[pos][0], 0);
4158 if (pos != other + i)
4159 isl_basic_map_swap_div(bmap, pos, other + i);
4161 return bmap;
4162 error:
4163 isl_basic_map_free(bmap);
4164 return NULL;
4167 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4168 * some obvious symmetries.
4170 * We make sure the divs in the domain are properly ordered,
4171 * because they will be added one by one in the given order
4172 * during the construction of the solution map.
4174 static struct isl_sol *basic_map_partial_lexopt_base_sol(
4175 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4176 __isl_give isl_set **empty, int max,
4177 struct isl_sol *(*init)(__isl_keep isl_basic_map *bmap,
4178 __isl_take isl_basic_set *dom, int track_empty, int max))
4180 struct isl_tab *tab;
4181 struct isl_sol *sol = NULL;
4182 struct isl_context *context;
4184 if (dom->n_div) {
4185 dom = isl_basic_set_order_divs(dom);
4186 bmap = align_context_divs(bmap, dom);
4188 sol = init(bmap, dom, !!empty, max);
4189 if (!sol)
4190 goto error;
4192 context = sol->context;
4193 if (isl_basic_set_plain_is_empty(context->op->peek_basic_set(context)))
4194 /* nothing */;
4195 else if (isl_basic_map_plain_is_empty(bmap)) {
4196 if (sol->add_empty)
4197 sol->add_empty(sol,
4198 isl_basic_set_copy(context->op->peek_basic_set(context)));
4199 } else {
4200 tab = tab_for_lexmin(bmap,
4201 context->op->peek_basic_set(context), 1, max);
4202 tab = context->op->detect_nonnegative_parameters(context, tab);
4203 find_solutions_main(sol, tab);
4205 if (sol->error)
4206 goto error;
4208 isl_basic_map_free(bmap);
4209 return sol;
4210 error:
4211 sol_free(sol);
4212 isl_basic_map_free(bmap);
4213 return NULL;
4216 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4217 * some obvious symmetries.
4219 * We call basic_map_partial_lexopt_base_sol and extract the results.
4221 static __isl_give isl_map *basic_map_partial_lexopt_base(
4222 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4223 __isl_give isl_set **empty, int max)
4225 isl_map *result = NULL;
4226 struct isl_sol *sol;
4227 struct isl_sol_map *sol_map;
4229 sol = basic_map_partial_lexopt_base_sol(bmap, dom, empty, max,
4230 &sol_map_init);
4231 if (!sol)
4232 return NULL;
4233 sol_map = (struct isl_sol_map *) sol;
4235 result = isl_map_copy(sol_map->map);
4236 if (empty)
4237 *empty = isl_set_copy(sol_map->empty);
4238 sol_free(&sol_map->sol);
4239 return result;
4242 /* Return a count of the number of occurrences of the "n" first
4243 * variables in the inequality constraints of "bmap".
4245 static __isl_give int *count_occurrences(__isl_keep isl_basic_map *bmap,
4246 int n)
4248 int i, j;
4249 isl_ctx *ctx;
4250 int *occurrences;
4252 if (!bmap)
4253 return NULL;
4254 ctx = isl_basic_map_get_ctx(bmap);
4255 occurrences = isl_calloc_array(ctx, int, n);
4256 if (!occurrences)
4257 return NULL;
4259 for (i = 0; i < bmap->n_ineq; ++i) {
4260 for (j = 0; j < n; ++j) {
4261 if (!isl_int_is_zero(bmap->ineq[i][1 + j]))
4262 occurrences[j]++;
4266 return occurrences;
4269 /* Do all of the "n" variables with non-zero coefficients in "c"
4270 * occur in exactly a single constraint.
4271 * "occurrences" is an array of length "n" containing the number
4272 * of occurrences of each of the variables in the inequality constraints.
4274 static int single_occurrence(int n, isl_int *c, int *occurrences)
4276 int i;
4278 for (i = 0; i < n; ++i) {
4279 if (isl_int_is_zero(c[i]))
4280 continue;
4281 if (occurrences[i] != 1)
4282 return 0;
4285 return 1;
4288 /* Do all of the "n" initial variables that occur in inequality constraint
4289 * "ineq" of "bmap" only occur in that constraint?
4291 static int all_single_occurrence(__isl_keep isl_basic_map *bmap, int ineq,
4292 int n)
4294 int i, j;
4296 for (i = 0; i < n; ++i) {
4297 if (isl_int_is_zero(bmap->ineq[ineq][1 + i]))
4298 continue;
4299 for (j = 0; j < bmap->n_ineq; ++j) {
4300 if (j == ineq)
4301 continue;
4302 if (!isl_int_is_zero(bmap->ineq[j][1 + i]))
4303 return 0;
4307 return 1;
4310 /* Structure used during detection of parallel constraints.
4311 * n_in: number of "input" variables: isl_dim_param + isl_dim_in
4312 * n_out: number of "output" variables: isl_dim_out + isl_dim_div
4313 * val: the coefficients of the output variables
4315 struct isl_constraint_equal_info {
4316 isl_basic_map *bmap;
4317 unsigned n_in;
4318 unsigned n_out;
4319 isl_int *val;
4322 /* Check whether the coefficients of the output variables
4323 * of the constraint in "entry" are equal to info->val.
4325 static int constraint_equal(const void *entry, const void *val)
4327 isl_int **row = (isl_int **)entry;
4328 const struct isl_constraint_equal_info *info = val;
4330 return isl_seq_eq((*row) + 1 + info->n_in, info->val, info->n_out);
4333 /* Check whether "bmap" has a pair of constraints that have
4334 * the same coefficients for the output variables.
4335 * Note that the coefficients of the existentially quantified
4336 * variables need to be zero since the existentially quantified
4337 * of the result are usually not the same as those of the input.
4338 * Furthermore, check that each of the input variables that occur
4339 * in those constraints does not occur in any other constraint.
4340 * If so, return 1 and return the row indices of the two constraints
4341 * in *first and *second.
4343 static int parallel_constraints(__isl_keep isl_basic_map *bmap,
4344 int *first, int *second)
4346 int i;
4347 isl_ctx *ctx;
4348 int *occurrences = NULL;
4349 struct isl_hash_table *table = NULL;
4350 struct isl_hash_table_entry *entry;
4351 struct isl_constraint_equal_info info;
4352 unsigned n_out;
4353 unsigned n_div;
4355 ctx = isl_basic_map_get_ctx(bmap);
4356 table = isl_hash_table_alloc(ctx, bmap->n_ineq);
4357 if (!table)
4358 goto error;
4360 info.n_in = isl_basic_map_dim(bmap, isl_dim_param) +
4361 isl_basic_map_dim(bmap, isl_dim_in);
4362 occurrences = count_occurrences(bmap, info.n_in);
4363 if (info.n_in && !occurrences)
4364 goto error;
4365 info.bmap = bmap;
4366 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4367 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4368 info.n_out = n_out + n_div;
4369 for (i = 0; i < bmap->n_ineq; ++i) {
4370 uint32_t hash;
4372 info.val = bmap->ineq[i] + 1 + info.n_in;
4373 if (isl_seq_first_non_zero(info.val, n_out) < 0)
4374 continue;
4375 if (isl_seq_first_non_zero(info.val + n_out, n_div) >= 0)
4376 continue;
4377 if (!single_occurrence(info.n_in, bmap->ineq[i] + 1,
4378 occurrences))
4379 continue;
4380 hash = isl_seq_get_hash(info.val, info.n_out);
4381 entry = isl_hash_table_find(ctx, table, hash,
4382 constraint_equal, &info, 1);
4383 if (!entry)
4384 goto error;
4385 if (entry->data)
4386 break;
4387 entry->data = &bmap->ineq[i];
4390 if (i < bmap->n_ineq) {
4391 *first = ((isl_int **)entry->data) - bmap->ineq;
4392 *second = i;
4395 isl_hash_table_free(ctx, table);
4396 free(occurrences);
4398 return i < bmap->n_ineq;
4399 error:
4400 isl_hash_table_free(ctx, table);
4401 free(occurrences);
4402 return -1;
4405 /* Given a set of upper bounds in "var", add constraints to "bset"
4406 * that make the i-th bound smallest.
4408 * In particular, if there are n bounds b_i, then add the constraints
4410 * b_i <= b_j for j > i
4411 * b_i < b_j for j < i
4413 static __isl_give isl_basic_set *select_minimum(__isl_take isl_basic_set *bset,
4414 __isl_keep isl_mat *var, int i)
4416 isl_ctx *ctx;
4417 int j, k;
4419 ctx = isl_mat_get_ctx(var);
4421 for (j = 0; j < var->n_row; ++j) {
4422 if (j == i)
4423 continue;
4424 k = isl_basic_set_alloc_inequality(bset);
4425 if (k < 0)
4426 goto error;
4427 isl_seq_combine(bset->ineq[k], ctx->one, var->row[j],
4428 ctx->negone, var->row[i], var->n_col);
4429 isl_int_set_si(bset->ineq[k][var->n_col], 0);
4430 if (j < i)
4431 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
4434 bset = isl_basic_set_finalize(bset);
4436 return bset;
4437 error:
4438 isl_basic_set_free(bset);
4439 return NULL;
4442 /* Given a set of upper bounds on the last "input" variable m,
4443 * construct a set that assigns the minimal upper bound to m, i.e.,
4444 * construct a set that divides the space into cells where one
4445 * of the upper bounds is smaller than all the others and assign
4446 * this upper bound to m.
4448 * In particular, if there are n bounds b_i, then the result
4449 * consists of n basic sets, each one of the form
4451 * m = b_i
4452 * b_i <= b_j for j > i
4453 * b_i < b_j for j < i
4455 static __isl_give isl_set *set_minimum(__isl_take isl_space *dim,
4456 __isl_take isl_mat *var)
4458 int i, k;
4459 isl_basic_set *bset = NULL;
4460 isl_set *set = NULL;
4462 if (!dim || !var)
4463 goto error;
4465 set = isl_set_alloc_space(isl_space_copy(dim),
4466 var->n_row, ISL_SET_DISJOINT);
4468 for (i = 0; i < var->n_row; ++i) {
4469 bset = isl_basic_set_alloc_space(isl_space_copy(dim), 0,
4470 1, var->n_row - 1);
4471 k = isl_basic_set_alloc_equality(bset);
4472 if (k < 0)
4473 goto error;
4474 isl_seq_cpy(bset->eq[k], var->row[i], var->n_col);
4475 isl_int_set_si(bset->eq[k][var->n_col], -1);
4476 bset = select_minimum(bset, var, i);
4477 set = isl_set_add_basic_set(set, bset);
4480 isl_space_free(dim);
4481 isl_mat_free(var);
4482 return set;
4483 error:
4484 isl_basic_set_free(bset);
4485 isl_set_free(set);
4486 isl_space_free(dim);
4487 isl_mat_free(var);
4488 return NULL;
4491 /* Given that the last input variable of "bmap" represents the minimum
4492 * of the bounds in "cst", check whether we need to split the domain
4493 * based on which bound attains the minimum.
4495 * A split is needed when the minimum appears in an integer division
4496 * or in an equality. Otherwise, it is only needed if it appears in
4497 * an upper bound that is different from the upper bounds on which it
4498 * is defined.
4500 static int need_split_basic_map(__isl_keep isl_basic_map *bmap,
4501 __isl_keep isl_mat *cst)
4503 int i, j;
4504 unsigned total;
4505 unsigned pos;
4507 pos = cst->n_col - 1;
4508 total = isl_basic_map_dim(bmap, isl_dim_all);
4510 for (i = 0; i < bmap->n_div; ++i)
4511 if (!isl_int_is_zero(bmap->div[i][2 + pos]))
4512 return 1;
4514 for (i = 0; i < bmap->n_eq; ++i)
4515 if (!isl_int_is_zero(bmap->eq[i][1 + pos]))
4516 return 1;
4518 for (i = 0; i < bmap->n_ineq; ++i) {
4519 if (isl_int_is_nonneg(bmap->ineq[i][1 + pos]))
4520 continue;
4521 if (!isl_int_is_negone(bmap->ineq[i][1 + pos]))
4522 return 1;
4523 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + pos + 1,
4524 total - pos - 1) >= 0)
4525 return 1;
4527 for (j = 0; j < cst->n_row; ++j)
4528 if (isl_seq_eq(bmap->ineq[i], cst->row[j], cst->n_col))
4529 break;
4530 if (j >= cst->n_row)
4531 return 1;
4534 return 0;
4537 /* Given that the last set variable of "bset" represents the minimum
4538 * of the bounds in "cst", check whether we need to split the domain
4539 * based on which bound attains the minimum.
4541 * We simply call need_split_basic_map here. This is safe because
4542 * the position of the minimum is computed from "cst" and not
4543 * from "bmap".
4545 static int need_split_basic_set(__isl_keep isl_basic_set *bset,
4546 __isl_keep isl_mat *cst)
4548 return need_split_basic_map((isl_basic_map *)bset, cst);
4551 /* Given that the last set variable of "set" represents the minimum
4552 * of the bounds in "cst", check whether we need to split the domain
4553 * based on which bound attains the minimum.
4555 static int need_split_set(__isl_keep isl_set *set, __isl_keep isl_mat *cst)
4557 int i;
4559 for (i = 0; i < set->n; ++i)
4560 if (need_split_basic_set(set->p[i], cst))
4561 return 1;
4563 return 0;
4566 /* Given a set of which the last set variable is the minimum
4567 * of the bounds in "cst", split each basic set in the set
4568 * in pieces where one of the bounds is (strictly) smaller than the others.
4569 * This subdivision is given in "min_expr".
4570 * The variable is subsequently projected out.
4572 * We only do the split when it is needed.
4573 * For example if the last input variable m = min(a,b) and the only
4574 * constraints in the given basic set are lower bounds on m,
4575 * i.e., l <= m = min(a,b), then we can simply project out m
4576 * to obtain l <= a and l <= b, without having to split on whether
4577 * m is equal to a or b.
4579 static __isl_give isl_set *split(__isl_take isl_set *empty,
4580 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
4582 int n_in;
4583 int i;
4584 isl_space *dim;
4585 isl_set *res;
4587 if (!empty || !min_expr || !cst)
4588 goto error;
4590 n_in = isl_set_dim(empty, isl_dim_set);
4591 dim = isl_set_get_space(empty);
4592 dim = isl_space_drop_dims(dim, isl_dim_set, n_in - 1, 1);
4593 res = isl_set_empty(dim);
4595 for (i = 0; i < empty->n; ++i) {
4596 isl_set *set;
4598 set = isl_set_from_basic_set(isl_basic_set_copy(empty->p[i]));
4599 if (need_split_basic_set(empty->p[i], cst))
4600 set = isl_set_intersect(set, isl_set_copy(min_expr));
4601 set = isl_set_remove_dims(set, isl_dim_set, n_in - 1, 1);
4603 res = isl_set_union_disjoint(res, set);
4606 isl_set_free(empty);
4607 isl_set_free(min_expr);
4608 isl_mat_free(cst);
4609 return res;
4610 error:
4611 isl_set_free(empty);
4612 isl_set_free(min_expr);
4613 isl_mat_free(cst);
4614 return NULL;
4617 /* Given a map of which the last input variable is the minimum
4618 * of the bounds in "cst", split each basic set in the set
4619 * in pieces where one of the bounds is (strictly) smaller than the others.
4620 * This subdivision is given in "min_expr".
4621 * The variable is subsequently projected out.
4623 * The implementation is essentially the same as that of "split".
4625 static __isl_give isl_map *split_domain(__isl_take isl_map *opt,
4626 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
4628 int n_in;
4629 int i;
4630 isl_space *dim;
4631 isl_map *res;
4633 if (!opt || !min_expr || !cst)
4634 goto error;
4636 n_in = isl_map_dim(opt, isl_dim_in);
4637 dim = isl_map_get_space(opt);
4638 dim = isl_space_drop_dims(dim, isl_dim_in, n_in - 1, 1);
4639 res = isl_map_empty(dim);
4641 for (i = 0; i < opt->n; ++i) {
4642 isl_map *map;
4644 map = isl_map_from_basic_map(isl_basic_map_copy(opt->p[i]));
4645 if (need_split_basic_map(opt->p[i], cst))
4646 map = isl_map_intersect_domain(map,
4647 isl_set_copy(min_expr));
4648 map = isl_map_remove_dims(map, isl_dim_in, n_in - 1, 1);
4650 res = isl_map_union_disjoint(res, map);
4653 isl_map_free(opt);
4654 isl_set_free(min_expr);
4655 isl_mat_free(cst);
4656 return res;
4657 error:
4658 isl_map_free(opt);
4659 isl_set_free(min_expr);
4660 isl_mat_free(cst);
4661 return NULL;
4664 static __isl_give isl_map *basic_map_partial_lexopt(
4665 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4666 __isl_give isl_set **empty, int max);
4668 /* This function is called from basic_map_partial_lexopt_symm.
4669 * The last variable of "bmap" and "dom" corresponds to the minimum
4670 * of the bounds in "cst". "map_space" is the space of the original
4671 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
4672 * is the space of the original domain.
4674 * We recursively call basic_map_partial_lexopt and then plug in
4675 * the definition of the minimum in the result.
4677 static __isl_give isl_map *basic_map_partial_lexopt_symm_core(
4678 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4679 __isl_give isl_set **empty, int max, __isl_take isl_mat *cst,
4680 __isl_take isl_space *map_space, __isl_take isl_space *set_space)
4682 isl_map *opt;
4683 isl_set *min_expr;
4685 min_expr = set_minimum(isl_basic_set_get_space(dom), isl_mat_copy(cst));
4687 opt = basic_map_partial_lexopt(bmap, dom, empty, max);
4689 if (empty) {
4690 *empty = split(*empty,
4691 isl_set_copy(min_expr), isl_mat_copy(cst));
4692 *empty = isl_set_reset_space(*empty, set_space);
4695 opt = split_domain(opt, min_expr, cst);
4696 opt = isl_map_reset_space(opt, map_space);
4698 return opt;
4701 /* Extract a domain from "bmap" for the purpose of computing
4702 * a lexicographic optimum.
4704 * This function is only called when the caller wants to compute a full
4705 * lexicographic optimum, i.e., without specifying a domain. In this case,
4706 * the caller is not interested in the part of the domain space where
4707 * there is no solution and the domain can be initialized to those constraints
4708 * of "bmap" that only involve the parameters and the input dimensions.
4709 * This relieves the parametric programming engine from detecting those
4710 * inequalities and transferring them to the context. More importantly,
4711 * it ensures that those inequalities are transferred first and not
4712 * intermixed with inequalities that actually split the domain.
4714 * If the caller does not require the absence of existentially quantified
4715 * variables in the result (i.e., if ISL_OPT_QE is not set in "flags"),
4716 * then the actual domain of "bmap" can be used. This ensures that
4717 * the domain does not need to be split at all just to separate out
4718 * pieces of the domain that do not have a solution from piece that do.
4719 * This domain cannot be used in general because it may involve
4720 * (unknown) existentially quantified variables which will then also
4721 * appear in the solution.
4723 static __isl_give isl_basic_set *extract_domain(__isl_keep isl_basic_map *bmap,
4724 unsigned flags)
4726 int n_div;
4727 int n_out;
4729 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4730 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4731 bmap = isl_basic_map_copy(bmap);
4732 if (ISL_FL_ISSET(flags, ISL_OPT_QE)) {
4733 bmap = isl_basic_map_drop_constraints_involving_dims(bmap,
4734 isl_dim_div, 0, n_div);
4735 bmap = isl_basic_map_drop_constraints_involving_dims(bmap,
4736 isl_dim_out, 0, n_out);
4738 return isl_basic_map_domain(bmap);
4741 #undef TYPE
4742 #define TYPE isl_map
4743 #undef SUFFIX
4744 #define SUFFIX
4745 #include "isl_tab_lexopt_templ.c"
4747 struct isl_sol_for {
4748 struct isl_sol sol;
4749 int (*fn)(__isl_take isl_basic_set *dom,
4750 __isl_take isl_aff_list *list, void *user);
4751 void *user;
4754 static void sol_for_free(struct isl_sol_for *sol_for)
4756 if (!sol_for)
4757 return;
4758 if (sol_for->sol.context)
4759 sol_for->sol.context->op->free(sol_for->sol.context);
4760 free(sol_for);
4763 static void sol_for_free_wrap(struct isl_sol *sol)
4765 sol_for_free((struct isl_sol_for *)sol);
4768 /* Add the solution identified by the tableau and the context tableau.
4770 * See documentation of sol_add for more details.
4772 * Instead of constructing a basic map, this function calls a user
4773 * defined function with the current context as a basic set and
4774 * a list of affine expressions representing the relation between
4775 * the input and output. The space over which the affine expressions
4776 * are defined is the same as that of the domain. The number of
4777 * affine expressions in the list is equal to the number of output variables.
4779 static void sol_for_add(struct isl_sol_for *sol,
4780 struct isl_basic_set *dom, struct isl_mat *M)
4782 int i;
4783 isl_ctx *ctx;
4784 isl_local_space *ls;
4785 isl_aff *aff;
4786 isl_aff_list *list;
4788 if (sol->sol.error || !dom || !M)
4789 goto error;
4791 ctx = isl_basic_set_get_ctx(dom);
4792 ls = isl_basic_set_get_local_space(dom);
4793 list = isl_aff_list_alloc(ctx, M->n_row - 1);
4794 for (i = 1; i < M->n_row; ++i) {
4795 aff = isl_aff_alloc(isl_local_space_copy(ls));
4796 if (aff) {
4797 isl_int_set(aff->v->el[0], M->row[0][0]);
4798 isl_seq_cpy(aff->v->el + 1, M->row[i], M->n_col);
4800 aff = isl_aff_normalize(aff);
4801 list = isl_aff_list_add(list, aff);
4803 isl_local_space_free(ls);
4805 dom = isl_basic_set_finalize(dom);
4807 if (sol->fn(isl_basic_set_copy(dom), list, sol->user) < 0)
4808 goto error;
4810 isl_basic_set_free(dom);
4811 isl_mat_free(M);
4812 return;
4813 error:
4814 isl_basic_set_free(dom);
4815 isl_mat_free(M);
4816 sol->sol.error = 1;
4819 static void sol_for_add_wrap(struct isl_sol *sol,
4820 struct isl_basic_set *dom, struct isl_mat *M)
4822 sol_for_add((struct isl_sol_for *)sol, dom, M);
4825 static struct isl_sol_for *sol_for_init(struct isl_basic_map *bmap, int max,
4826 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4827 void *user),
4828 void *user)
4830 struct isl_sol_for *sol_for = NULL;
4831 isl_space *dom_dim;
4832 struct isl_basic_set *dom = NULL;
4834 sol_for = isl_calloc_type(bmap->ctx, struct isl_sol_for);
4835 if (!sol_for)
4836 goto error;
4838 dom_dim = isl_space_domain(isl_space_copy(bmap->dim));
4839 dom = isl_basic_set_universe(dom_dim);
4841 sol_for->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
4842 sol_for->sol.dec_level.callback.run = &sol_dec_level_wrap;
4843 sol_for->sol.dec_level.sol = &sol_for->sol;
4844 sol_for->fn = fn;
4845 sol_for->user = user;
4846 sol_for->sol.max = max;
4847 sol_for->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
4848 sol_for->sol.add = &sol_for_add_wrap;
4849 sol_for->sol.add_empty = NULL;
4850 sol_for->sol.free = &sol_for_free_wrap;
4852 sol_for->sol.context = isl_context_alloc(dom);
4853 if (!sol_for->sol.context)
4854 goto error;
4856 isl_basic_set_free(dom);
4857 return sol_for;
4858 error:
4859 isl_basic_set_free(dom);
4860 sol_for_free(sol_for);
4861 return NULL;
4864 static void sol_for_find_solutions(struct isl_sol_for *sol_for,
4865 struct isl_tab *tab)
4867 find_solutions_main(&sol_for->sol, tab);
4870 int isl_basic_map_foreach_lexopt(__isl_keep isl_basic_map *bmap, int max,
4871 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4872 void *user),
4873 void *user)
4875 struct isl_sol_for *sol_for = NULL;
4877 bmap = isl_basic_map_copy(bmap);
4878 bmap = isl_basic_map_detect_equalities(bmap);
4879 if (!bmap)
4880 return -1;
4882 sol_for = sol_for_init(bmap, max, fn, user);
4883 if (!sol_for)
4884 goto error;
4886 if (isl_basic_map_plain_is_empty(bmap))
4887 /* nothing */;
4888 else {
4889 struct isl_tab *tab;
4890 struct isl_context *context = sol_for->sol.context;
4891 tab = tab_for_lexmin(bmap,
4892 context->op->peek_basic_set(context), 1, max);
4893 tab = context->op->detect_nonnegative_parameters(context, tab);
4894 sol_for_find_solutions(sol_for, tab);
4895 if (sol_for->sol.error)
4896 goto error;
4899 sol_free(&sol_for->sol);
4900 isl_basic_map_free(bmap);
4901 return 0;
4902 error:
4903 sol_free(&sol_for->sol);
4904 isl_basic_map_free(bmap);
4905 return -1;
4908 int isl_basic_set_foreach_lexopt(__isl_keep isl_basic_set *bset, int max,
4909 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4910 void *user),
4911 void *user)
4913 return isl_basic_map_foreach_lexopt(bset, max, fn, user);
4916 /* Check if the given sequence of len variables starting at pos
4917 * represents a trivial (i.e., zero) solution.
4918 * The variables are assumed to be non-negative and to come in pairs,
4919 * with each pair representing a variable of unrestricted sign.
4920 * The solution is trivial if each such pair in the sequence consists
4921 * of two identical values, meaning that the variable being represented
4922 * has value zero.
4924 static int region_is_trivial(struct isl_tab *tab, int pos, int len)
4926 int i;
4928 if (len == 0)
4929 return 0;
4931 for (i = 0; i < len; i += 2) {
4932 int neg_row;
4933 int pos_row;
4935 neg_row = tab->var[pos + i].is_row ?
4936 tab->var[pos + i].index : -1;
4937 pos_row = tab->var[pos + i + 1].is_row ?
4938 tab->var[pos + i + 1].index : -1;
4940 if ((neg_row < 0 ||
4941 isl_int_is_zero(tab->mat->row[neg_row][1])) &&
4942 (pos_row < 0 ||
4943 isl_int_is_zero(tab->mat->row[pos_row][1])))
4944 continue;
4946 if (neg_row < 0 || pos_row < 0)
4947 return 0;
4948 if (isl_int_ne(tab->mat->row[neg_row][1],
4949 tab->mat->row[pos_row][1]))
4950 return 0;
4953 return 1;
4956 /* Return the index of the first trivial region or -1 if all regions
4957 * are non-trivial.
4959 static int first_trivial_region(struct isl_tab *tab,
4960 int n_region, struct isl_region *region)
4962 int i;
4964 for (i = 0; i < n_region; ++i) {
4965 if (region_is_trivial(tab, region[i].pos, region[i].len))
4966 return i;
4969 return -1;
4972 /* Check if the solution is optimal, i.e., whether the first
4973 * n_op entries are zero.
4975 static int is_optimal(__isl_keep isl_vec *sol, int n_op)
4977 int i;
4979 for (i = 0; i < n_op; ++i)
4980 if (!isl_int_is_zero(sol->el[1 + i]))
4981 return 0;
4982 return 1;
4985 /* Add constraints to "tab" that ensure that any solution is significantly
4986 * better than that represented by "sol". That is, find the first
4987 * relevant (within first n_op) non-zero coefficient and force it (along
4988 * with all previous coefficients) to be zero.
4989 * If the solution is already optimal (all relevant coefficients are zero),
4990 * then just mark the table as empty.
4992 * This function assumes that at least 2 * n_op more rows and at least
4993 * 2 * n_op more elements in the constraint array are available in the tableau.
4995 static int force_better_solution(struct isl_tab *tab,
4996 __isl_keep isl_vec *sol, int n_op)
4998 int i;
4999 isl_ctx *ctx;
5000 isl_vec *v = NULL;
5002 if (!sol)
5003 return -1;
5005 for (i = 0; i < n_op; ++i)
5006 if (!isl_int_is_zero(sol->el[1 + i]))
5007 break;
5009 if (i == n_op) {
5010 if (isl_tab_mark_empty(tab) < 0)
5011 return -1;
5012 return 0;
5015 ctx = isl_vec_get_ctx(sol);
5016 v = isl_vec_alloc(ctx, 1 + tab->n_var);
5017 if (!v)
5018 return -1;
5020 for (; i >= 0; --i) {
5021 v = isl_vec_clr(v);
5022 isl_int_set_si(v->el[1 + i], -1);
5023 if (add_lexmin_eq(tab, v->el) < 0)
5024 goto error;
5027 isl_vec_free(v);
5028 return 0;
5029 error:
5030 isl_vec_free(v);
5031 return -1;
5034 struct isl_trivial {
5035 int update;
5036 int region;
5037 int side;
5038 struct isl_tab_undo *snap;
5041 /* Return the lexicographically smallest non-trivial solution of the
5042 * given ILP problem.
5044 * All variables are assumed to be non-negative.
5046 * n_op is the number of initial coordinates to optimize.
5047 * That is, once a solution has been found, we will only continue looking
5048 * for solution that result in significantly better values for those
5049 * initial coordinates. That is, we only continue looking for solutions
5050 * that increase the number of initial zeros in this sequence.
5052 * A solution is non-trivial, if it is non-trivial on each of the
5053 * specified regions. Each region represents a sequence of pairs
5054 * of variables. A solution is non-trivial on such a region if
5055 * at least one of these pairs consists of different values, i.e.,
5056 * such that the non-negative variable represented by the pair is non-zero.
5058 * Whenever a conflict is encountered, all constraints involved are
5059 * reported to the caller through a call to "conflict".
5061 * We perform a simple branch-and-bound backtracking search.
5062 * Each level in the search represents initially trivial region that is forced
5063 * to be non-trivial.
5064 * At each level we consider n cases, where n is the length of the region.
5065 * In terms of the n/2 variables of unrestricted signs being encoded by
5066 * the region, we consider the cases
5067 * x_0 >= 1
5068 * x_0 <= -1
5069 * x_0 = 0 and x_1 >= 1
5070 * x_0 = 0 and x_1 <= -1
5071 * x_0 = 0 and x_1 = 0 and x_2 >= 1
5072 * x_0 = 0 and x_1 = 0 and x_2 <= -1
5073 * ...
5074 * The cases are considered in this order, assuming that each pair
5075 * x_i_a x_i_b represents the value x_i_b - x_i_a.
5076 * That is, x_0 >= 1 is enforced by adding the constraint
5077 * x_0_b - x_0_a >= 1
5079 __isl_give isl_vec *isl_tab_basic_set_non_trivial_lexmin(
5080 __isl_take isl_basic_set *bset, int n_op, int n_region,
5081 struct isl_region *region,
5082 int (*conflict)(int con, void *user), void *user)
5084 int i, j;
5085 int r;
5086 isl_ctx *ctx;
5087 isl_vec *v = NULL;
5088 isl_vec *sol = NULL;
5089 struct isl_tab *tab;
5090 struct isl_trivial *triv = NULL;
5091 int level, init;
5093 if (!bset)
5094 return NULL;
5096 ctx = isl_basic_set_get_ctx(bset);
5097 sol = isl_vec_alloc(ctx, 0);
5099 tab = tab_for_lexmin(bset, NULL, 0, 0);
5100 if (!tab)
5101 goto error;
5102 tab->conflict = conflict;
5103 tab->conflict_user = user;
5105 v = isl_vec_alloc(ctx, 1 + tab->n_var);
5106 triv = isl_calloc_array(ctx, struct isl_trivial, n_region);
5107 if (!v || (n_region && !triv))
5108 goto error;
5110 level = 0;
5111 init = 1;
5113 while (level >= 0) {
5114 int side, base;
5116 if (init) {
5117 tab = cut_to_integer_lexmin(tab, CUT_ONE);
5118 if (!tab)
5119 goto error;
5120 if (tab->empty)
5121 goto backtrack;
5122 r = first_trivial_region(tab, n_region, region);
5123 if (r < 0) {
5124 for (i = 0; i < level; ++i)
5125 triv[i].update = 1;
5126 isl_vec_free(sol);
5127 sol = isl_tab_get_sample_value(tab);
5128 if (!sol)
5129 goto error;
5130 if (is_optimal(sol, n_op))
5131 break;
5132 goto backtrack;
5134 if (level >= n_region)
5135 isl_die(ctx, isl_error_internal,
5136 "nesting level too deep", goto error);
5137 if (isl_tab_extend_cons(tab,
5138 2 * region[r].len + 2 * n_op) < 0)
5139 goto error;
5140 triv[level].region = r;
5141 triv[level].side = 0;
5144 r = triv[level].region;
5145 side = triv[level].side;
5146 base = 2 * (side/2);
5148 if (side >= region[r].len) {
5149 backtrack:
5150 level--;
5151 init = 0;
5152 if (level >= 0)
5153 if (isl_tab_rollback(tab, triv[level].snap) < 0)
5154 goto error;
5155 continue;
5158 if (triv[level].update) {
5159 if (force_better_solution(tab, sol, n_op) < 0)
5160 goto error;
5161 triv[level].update = 0;
5164 if (side == base && base >= 2) {
5165 for (j = base - 2; j < base; ++j) {
5166 v = isl_vec_clr(v);
5167 isl_int_set_si(v->el[1 + region[r].pos + j], 1);
5168 if (add_lexmin_eq(tab, v->el) < 0)
5169 goto error;
5173 triv[level].snap = isl_tab_snap(tab);
5174 if (isl_tab_push_basis(tab) < 0)
5175 goto error;
5177 v = isl_vec_clr(v);
5178 isl_int_set_si(v->el[0], -1);
5179 isl_int_set_si(v->el[1 + region[r].pos + side], -1);
5180 isl_int_set_si(v->el[1 + region[r].pos + (side ^ 1)], 1);
5181 tab = add_lexmin_ineq(tab, v->el);
5183 triv[level].side++;
5184 level++;
5185 init = 1;
5188 free(triv);
5189 isl_vec_free(v);
5190 isl_tab_free(tab);
5191 isl_basic_set_free(bset);
5193 return sol;
5194 error:
5195 free(triv);
5196 isl_vec_free(v);
5197 isl_tab_free(tab);
5198 isl_basic_set_free(bset);
5199 isl_vec_free(sol);
5200 return NULL;
5203 /* Wrapper for a tableau that is used for computing
5204 * the lexicographically smallest rational point of a non-negative set.
5205 * This point is represented by the sample value of "tab",
5206 * unless "tab" is empty.
5208 struct isl_tab_lexmin {
5209 isl_ctx *ctx;
5210 struct isl_tab *tab;
5213 /* Free "tl" and return NULL.
5215 __isl_null isl_tab_lexmin *isl_tab_lexmin_free(__isl_take isl_tab_lexmin *tl)
5217 if (!tl)
5218 return NULL;
5219 isl_ctx_deref(tl->ctx);
5220 isl_tab_free(tl->tab);
5221 free(tl);
5223 return NULL;
5226 /* Construct an isl_tab_lexmin for computing
5227 * the lexicographically smallest rational point in "bset",
5228 * assuming that all variables are non-negative.
5230 __isl_give isl_tab_lexmin *isl_tab_lexmin_from_basic_set(
5231 __isl_take isl_basic_set *bset)
5233 isl_ctx *ctx;
5234 isl_tab_lexmin *tl;
5236 if (!bset)
5237 return NULL;
5239 ctx = isl_basic_set_get_ctx(bset);
5240 tl = isl_calloc_type(ctx, struct isl_tab_lexmin);
5241 if (!tl)
5242 goto error;
5243 tl->ctx = ctx;
5244 isl_ctx_ref(ctx);
5245 tl->tab = tab_for_lexmin(bset, NULL, 0, 0);
5246 isl_basic_set_free(bset);
5247 if (!tl->tab)
5248 return isl_tab_lexmin_free(tl);
5249 return tl;
5250 error:
5251 isl_basic_set_free(bset);
5252 isl_tab_lexmin_free(tl);
5253 return NULL;
5256 /* Return the dimension of the set represented by "tl".
5258 int isl_tab_lexmin_dim(__isl_keep isl_tab_lexmin *tl)
5260 return tl ? tl->tab->n_var : -1;
5263 /* Add the equality with coefficients "eq" to "tl", updating the optimal
5264 * solution if needed.
5265 * The equality is added as two opposite inequality constraints.
5267 __isl_give isl_tab_lexmin *isl_tab_lexmin_add_eq(__isl_take isl_tab_lexmin *tl,
5268 isl_int *eq)
5270 unsigned n_var;
5272 if (!tl || !eq)
5273 return isl_tab_lexmin_free(tl);
5275 if (isl_tab_extend_cons(tl->tab, 2) < 0)
5276 return isl_tab_lexmin_free(tl);
5277 n_var = tl->tab->n_var;
5278 isl_seq_neg(eq, eq, 1 + n_var);
5279 tl->tab = add_lexmin_ineq(tl->tab, eq);
5280 isl_seq_neg(eq, eq, 1 + n_var);
5281 tl->tab = add_lexmin_ineq(tl->tab, eq);
5283 if (!tl->tab)
5284 return isl_tab_lexmin_free(tl);
5286 return tl;
5289 /* Return the lexicographically smallest rational point in the basic set
5290 * from which "tl" was constructed.
5291 * If the original input was empty, then return a zero-length vector.
5293 __isl_give isl_vec *isl_tab_lexmin_get_solution(__isl_keep isl_tab_lexmin *tl)
5295 if (!tl)
5296 return NULL;
5297 if (tl->tab->empty)
5298 return isl_vec_alloc(tl->ctx, 0);
5299 else
5300 return isl_tab_get_sample_value(tl->tab);
5303 /* Return the lexicographically smallest rational point in "bset",
5304 * assuming that all variables are non-negative.
5305 * If "bset" is empty, then return a zero-length vector.
5307 __isl_give isl_vec *isl_tab_basic_set_non_neg_lexmin(
5308 __isl_take isl_basic_set *bset)
5310 isl_tab_lexmin *tl;
5311 isl_vec *sol;
5313 tl = isl_tab_lexmin_from_basic_set(bset);
5314 sol = isl_tab_lexmin_get_solution(tl);
5315 isl_tab_lexmin_free(tl);
5316 return sol;
5319 struct isl_sol_pma {
5320 struct isl_sol sol;
5321 isl_pw_multi_aff *pma;
5322 isl_set *empty;
5325 static void sol_pma_free(struct isl_sol_pma *sol_pma)
5327 if (!sol_pma)
5328 return;
5329 if (sol_pma->sol.context)
5330 sol_pma->sol.context->op->free(sol_pma->sol.context);
5331 isl_pw_multi_aff_free(sol_pma->pma);
5332 isl_set_free(sol_pma->empty);
5333 free(sol_pma);
5336 /* This function is called for parts of the context where there is
5337 * no solution, with "bset" corresponding to the context tableau.
5338 * Simply add the basic set to the set "empty".
5340 static void sol_pma_add_empty(struct isl_sol_pma *sol,
5341 __isl_take isl_basic_set *bset)
5343 if (!bset || !sol->empty)
5344 goto error;
5346 sol->empty = isl_set_grow(sol->empty, 1);
5347 bset = isl_basic_set_simplify(bset);
5348 bset = isl_basic_set_finalize(bset);
5349 sol->empty = isl_set_add_basic_set(sol->empty, bset);
5350 if (!sol->empty)
5351 sol->sol.error = 1;
5352 return;
5353 error:
5354 isl_basic_set_free(bset);
5355 sol->sol.error = 1;
5358 /* Return the equality constraint in "bset" that defines existentially
5359 * quantified variable "pos" in terms of earlier dimensions.
5360 * The equality constraint is guaranteed to exist by the caller.
5361 * If "c" is not NULL, then it is the result of a previous call
5362 * to this function for the same variable, so simply return the input "c"
5363 * in that case.
5365 static __isl_give isl_constraint *get_equality(__isl_keep isl_basic_set *bset,
5366 int pos, __isl_take isl_constraint *c)
5368 int r;
5370 if (c)
5371 return c;
5372 r = isl_basic_set_has_defining_equality(bset, isl_dim_div, pos, &c);
5373 if (r < 0)
5374 return NULL;
5375 if (!r)
5376 isl_die(isl_basic_set_get_ctx(bset), isl_error_internal,
5377 "unexpected missing equality", return NULL);
5378 return c;
5381 /* Given a set "dom", of which only the first "n_known" existentially
5382 * quantified variables have a known explicit representation, and
5383 * a matrix "M", the rows of which are defined in terms of the dimensions
5384 * of "dom", eliminate all references to the existentially quantified
5385 * variables without a known explicit representation from "M"
5386 * by exploiting the equality constraints of "dom".
5388 * In particular, for each of those existentially quantified variables,
5389 * if there are non-zero entries in the corresponding column of "M",
5390 * then look for an equality constraint of "dom" that defines that variable
5391 * in terms of earlier variables and use it to clear the entries.
5393 * In particular, if the equality is of the form
5395 * f() + a alpha = 0
5397 * while the matrix entry is b/d (with d the global denominator of "M"),
5398 * then first scale the matrix such that the entry becomes b'/d' with
5399 * b' a multiple of a. Do this by multiplying the entire matrix
5400 * by abs(a/gcd(a,b)). Then subtract the equality multiplied by b'/a
5401 * from the row of "M" to clear the entry.
5403 static __isl_give isl_mat *eliminate_unknown_divs(__isl_take isl_mat *M,
5404 __isl_keep isl_basic_set *dom, int n_known)
5406 int i, j, n_div, off;
5407 isl_int t;
5408 isl_constraint *c = NULL;
5410 if (!M)
5411 return NULL;
5413 n_div = isl_basic_set_dim(dom, isl_dim_div);
5414 off = M->n_col - n_div;
5416 isl_int_init(t);
5417 for (i = n_div - 1; i >= n_known; --i) {
5418 for (j = 1; j < M->n_row; ++j) {
5419 if (isl_int_is_zero(M->row[j][off + i]))
5420 continue;
5421 c = get_equality(dom, i, c);
5422 if (!c)
5423 goto error;
5424 isl_int_gcd(t, M->row[j][off + i], c->v->el[off + i]);
5425 isl_int_divexact(t, c->v->el[off + i], t);
5426 isl_int_abs(t, t);
5427 M = isl_mat_scale(M, t);
5428 M = isl_mat_cow(M);
5429 if (!M)
5430 goto error;
5431 isl_int_divexact(t,
5432 M->row[j][off + i], c->v->el[off + i]);
5433 isl_seq_submul(M->row[j], t, c->v->el, M->n_col);
5435 c = isl_constraint_free(c);
5437 isl_int_clear(t);
5439 return M;
5440 error:
5441 isl_int_clear(t);
5442 isl_constraint_free(c);
5443 isl_mat_free(M);
5444 return NULL;
5447 /* Return the index of the last known div of "bset" after "start" and
5448 * up to (but not including) "end".
5449 * Return "start" if there is no such known div.
5451 static int last_known_div_after(__isl_keep isl_basic_set *bset,
5452 int start, int end)
5454 for (end = end - 1; end > start; --end) {
5455 if (isl_basic_set_div_is_known(bset, end))
5456 return end;
5459 return start;
5462 /* Set the affine expressions in "ma" according to the rows in "M", which
5463 * are defined over the local space "ls".
5464 * The matrix "M" may have extra (zero) columns beyond the number
5465 * of variables in "ls".
5467 static __isl_give isl_multi_aff *set_from_affine_matrix(
5468 __isl_take isl_multi_aff *ma, __isl_take isl_local_space *ls,
5469 __isl_take isl_mat *M)
5471 int i, dim;
5472 isl_aff *aff;
5474 if (!ma || !ls || !M)
5475 goto error;
5477 dim = isl_local_space_dim(ls, isl_dim_all);
5478 for (i = 1; i < M->n_row; ++i) {
5479 aff = isl_aff_alloc(isl_local_space_copy(ls));
5480 if (aff) {
5481 isl_int_set(aff->v->el[0], M->row[0][0]);
5482 isl_seq_cpy(aff->v->el + 1, M->row[i], 1 + dim);
5484 aff = isl_aff_normalize(aff);
5485 ma = isl_multi_aff_set_aff(ma, i - 1, aff);
5487 isl_local_space_free(ls);
5488 isl_mat_free(M);
5490 return ma;
5491 error:
5492 isl_local_space_free(ls);
5493 isl_mat_free(M);
5494 isl_multi_aff_free(ma);
5495 return NULL;
5498 /* Given a basic map "dom" that represents the context and an affine
5499 * matrix "M" that maps the dimensions of the context to the
5500 * output variables, construct an isl_pw_multi_aff with a single
5501 * cell corresponding to "dom" and affine expressions copied from "M".
5503 * Note that the description of the initial context may have involved
5504 * existentially quantified variables, in which case they also appear
5505 * in "dom". These need to be removed before creating the affine
5506 * expression because an affine expression cannot be defined in terms
5507 * of existentially quantified variables without a known representation.
5508 * In particular, they are first moved to the end in both "dom" and "M" and
5509 * then ignored in "M". In principle, the final columns of "M"
5510 * (i.e., those that will be ignored) should be zero at this stage
5511 * because align_context_divs adds the existentially quantified
5512 * variables of the context to the main tableau without any constraints.
5513 * The computed minimal value can therefore not depend on these variables.
5514 * However, additional integer divisions that get added for parametric cuts
5515 * get added to the end and they may happen to be equal to some affine
5516 * expression involving the original existentially quantified variables.
5517 * These equality constraints are then propagated to the main tableau
5518 * such that the computed minimum can in fact depend on those existentially
5519 * quantified variables. This dependence can however be removed again
5520 * by exploiting the equality constraints in "dom".
5521 * eliminate_unknown_divs takes care of this.
5523 static void sol_pma_add(struct isl_sol_pma *sol,
5524 __isl_take isl_basic_set *dom, __isl_take isl_mat *M)
5526 isl_local_space *ls;
5527 isl_multi_aff *maff;
5528 isl_pw_multi_aff *pma;
5529 int n_div, n_known, end, off;
5531 n_div = isl_basic_set_dim(dom, isl_dim_div);
5532 off = M->n_col - n_div;
5533 end = n_div;
5534 for (n_known = 0; n_known < end; ++n_known) {
5535 if (isl_basic_set_div_is_known(dom, n_known))
5536 continue;
5537 end = last_known_div_after(dom, n_known, end);
5538 if (end == n_known)
5539 break;
5540 isl_basic_set_swap_div(dom, n_known, end);
5541 M = isl_mat_swap_cols(M, off + n_known, off + end);
5543 dom = isl_basic_set_gauss(dom, NULL);
5544 if (n_known < n_div)
5545 M = eliminate_unknown_divs(M, dom, n_known);
5547 maff = isl_multi_aff_alloc(isl_pw_multi_aff_get_space(sol->pma));
5548 ls = isl_basic_set_get_local_space(dom);
5549 ls = isl_local_space_drop_dims(ls, isl_dim_div,
5550 n_known, n_div - n_known);
5551 maff = set_from_affine_matrix(maff, ls, M);
5552 dom = isl_basic_set_simplify(dom);
5553 dom = isl_basic_set_finalize(dom);
5554 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(dom), maff);
5555 sol->pma = isl_pw_multi_aff_add_disjoint(sol->pma, pma);
5556 if (!sol->pma)
5557 sol->sol.error = 1;
5560 static void sol_pma_free_wrap(struct isl_sol *sol)
5562 sol_pma_free((struct isl_sol_pma *)sol);
5565 static void sol_pma_add_empty_wrap(struct isl_sol *sol,
5566 __isl_take isl_basic_set *bset)
5568 sol_pma_add_empty((struct isl_sol_pma *)sol, bset);
5571 static void sol_pma_add_wrap(struct isl_sol *sol,
5572 __isl_take isl_basic_set *dom, __isl_take isl_mat *M)
5574 sol_pma_add((struct isl_sol_pma *)sol, dom, M);
5577 /* Construct an isl_sol_pma structure for accumulating the solution.
5578 * If track_empty is set, then we also keep track of the parts
5579 * of the context where there is no solution.
5580 * If max is set, then we are solving a maximization, rather than
5581 * a minimization problem, which means that the variables in the
5582 * tableau have value "M - x" rather than "M + x".
5584 static struct isl_sol *sol_pma_init(__isl_keep isl_basic_map *bmap,
5585 __isl_take isl_basic_set *dom, int track_empty, int max)
5587 struct isl_sol_pma *sol_pma = NULL;
5589 if (!bmap)
5590 goto error;
5592 sol_pma = isl_calloc_type(bmap->ctx, struct isl_sol_pma);
5593 if (!sol_pma)
5594 goto error;
5596 sol_pma->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
5597 sol_pma->sol.dec_level.callback.run = &sol_dec_level_wrap;
5598 sol_pma->sol.dec_level.sol = &sol_pma->sol;
5599 sol_pma->sol.max = max;
5600 sol_pma->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
5601 sol_pma->sol.add = &sol_pma_add_wrap;
5602 sol_pma->sol.add_empty = track_empty ? &sol_pma_add_empty_wrap : NULL;
5603 sol_pma->sol.free = &sol_pma_free_wrap;
5604 sol_pma->pma = isl_pw_multi_aff_empty(isl_basic_map_get_space(bmap));
5605 if (!sol_pma->pma)
5606 goto error;
5608 sol_pma->sol.context = isl_context_alloc(dom);
5609 if (!sol_pma->sol.context)
5610 goto error;
5612 if (track_empty) {
5613 sol_pma->empty = isl_set_alloc_space(isl_basic_set_get_space(dom),
5614 1, ISL_SET_DISJOINT);
5615 if (!sol_pma->empty)
5616 goto error;
5619 isl_basic_set_free(dom);
5620 return &sol_pma->sol;
5621 error:
5622 isl_basic_set_free(dom);
5623 sol_pma_free(sol_pma);
5624 return NULL;
5627 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
5628 * some obvious symmetries.
5630 * We call basic_map_partial_lexopt_base_sol and extract the results.
5632 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_base_pw_multi_aff(
5633 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5634 __isl_give isl_set **empty, int max)
5636 isl_pw_multi_aff *result = NULL;
5637 struct isl_sol *sol;
5638 struct isl_sol_pma *sol_pma;
5640 sol = basic_map_partial_lexopt_base_sol(bmap, dom, empty, max,
5641 &sol_pma_init);
5642 if (!sol)
5643 return NULL;
5644 sol_pma = (struct isl_sol_pma *) sol;
5646 result = isl_pw_multi_aff_copy(sol_pma->pma);
5647 if (empty)
5648 *empty = isl_set_copy(sol_pma->empty);
5649 sol_free(&sol_pma->sol);
5650 return result;
5653 /* Given that the last input variable of "maff" represents the minimum
5654 * of some bounds, check whether we need to plug in the expression
5655 * of the minimum.
5657 * In particular, check if the last input variable appears in any
5658 * of the expressions in "maff".
5660 static int need_substitution(__isl_keep isl_multi_aff *maff)
5662 int i;
5663 unsigned pos;
5665 pos = isl_multi_aff_dim(maff, isl_dim_in) - 1;
5667 for (i = 0; i < maff->n; ++i)
5668 if (isl_aff_involves_dims(maff->p[i], isl_dim_in, pos, 1))
5669 return 1;
5671 return 0;
5674 /* Given a set of upper bounds on the last "input" variable m,
5675 * construct a piecewise affine expression that selects
5676 * the minimal upper bound to m, i.e.,
5677 * divide the space into cells where one
5678 * of the upper bounds is smaller than all the others and select
5679 * this upper bound on that cell.
5681 * In particular, if there are n bounds b_i, then the result
5682 * consists of n cell, each one of the form
5684 * b_i <= b_j for j > i
5685 * b_i < b_j for j < i
5687 * The affine expression on this cell is
5689 * b_i
5691 static __isl_give isl_pw_aff *set_minimum_pa(__isl_take isl_space *space,
5692 __isl_take isl_mat *var)
5694 int i;
5695 isl_aff *aff = NULL;
5696 isl_basic_set *bset = NULL;
5697 isl_pw_aff *paff = NULL;
5698 isl_space *pw_space;
5699 isl_local_space *ls = NULL;
5701 if (!space || !var)
5702 goto error;
5704 ls = isl_local_space_from_space(isl_space_copy(space));
5705 pw_space = isl_space_copy(space);
5706 pw_space = isl_space_from_domain(pw_space);
5707 pw_space = isl_space_add_dims(pw_space, isl_dim_out, 1);
5708 paff = isl_pw_aff_alloc_size(pw_space, var->n_row);
5710 for (i = 0; i < var->n_row; ++i) {
5711 isl_pw_aff *paff_i;
5713 aff = isl_aff_alloc(isl_local_space_copy(ls));
5714 bset = isl_basic_set_alloc_space(isl_space_copy(space), 0,
5715 0, var->n_row - 1);
5716 if (!aff || !bset)
5717 goto error;
5718 isl_int_set_si(aff->v->el[0], 1);
5719 isl_seq_cpy(aff->v->el + 1, var->row[i], var->n_col);
5720 isl_int_set_si(aff->v->el[1 + var->n_col], 0);
5721 bset = select_minimum(bset, var, i);
5722 paff_i = isl_pw_aff_alloc(isl_set_from_basic_set(bset), aff);
5723 paff = isl_pw_aff_add_disjoint(paff, paff_i);
5726 isl_local_space_free(ls);
5727 isl_space_free(space);
5728 isl_mat_free(var);
5729 return paff;
5730 error:
5731 isl_aff_free(aff);
5732 isl_basic_set_free(bset);
5733 isl_pw_aff_free(paff);
5734 isl_local_space_free(ls);
5735 isl_space_free(space);
5736 isl_mat_free(var);
5737 return NULL;
5740 /* Given a piecewise multi-affine expression of which the last input variable
5741 * is the minimum of the bounds in "cst", plug in the value of the minimum.
5742 * This minimum expression is given in "min_expr_pa".
5743 * The set "min_expr" contains the same information, but in the form of a set.
5744 * The variable is subsequently projected out.
5746 * The implementation is similar to those of "split" and "split_domain".
5747 * If the variable appears in a given expression, then minimum expression
5748 * is plugged in. Otherwise, if the variable appears in the constraints
5749 * and a split is required, then the domain is split. Otherwise, no split
5750 * is performed.
5752 static __isl_give isl_pw_multi_aff *split_domain_pma(
5753 __isl_take isl_pw_multi_aff *opt, __isl_take isl_pw_aff *min_expr_pa,
5754 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
5756 int n_in;
5757 int i;
5758 isl_space *space;
5759 isl_pw_multi_aff *res;
5761 if (!opt || !min_expr || !cst)
5762 goto error;
5764 n_in = isl_pw_multi_aff_dim(opt, isl_dim_in);
5765 space = isl_pw_multi_aff_get_space(opt);
5766 space = isl_space_drop_dims(space, isl_dim_in, n_in - 1, 1);
5767 res = isl_pw_multi_aff_empty(space);
5769 for (i = 0; i < opt->n; ++i) {
5770 isl_pw_multi_aff *pma;
5772 pma = isl_pw_multi_aff_alloc(isl_set_copy(opt->p[i].set),
5773 isl_multi_aff_copy(opt->p[i].maff));
5774 if (need_substitution(opt->p[i].maff))
5775 pma = isl_pw_multi_aff_substitute(pma,
5776 isl_dim_in, n_in - 1, min_expr_pa);
5777 else if (need_split_set(opt->p[i].set, cst))
5778 pma = isl_pw_multi_aff_intersect_domain(pma,
5779 isl_set_copy(min_expr));
5780 pma = isl_pw_multi_aff_project_out(pma,
5781 isl_dim_in, n_in - 1, 1);
5783 res = isl_pw_multi_aff_add_disjoint(res, pma);
5786 isl_pw_multi_aff_free(opt);
5787 isl_pw_aff_free(min_expr_pa);
5788 isl_set_free(min_expr);
5789 isl_mat_free(cst);
5790 return res;
5791 error:
5792 isl_pw_multi_aff_free(opt);
5793 isl_pw_aff_free(min_expr_pa);
5794 isl_set_free(min_expr);
5795 isl_mat_free(cst);
5796 return NULL;
5799 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_pw_multi_aff(
5800 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5801 __isl_give isl_set **empty, int max);
5803 /* This function is called from basic_map_partial_lexopt_symm.
5804 * The last variable of "bmap" and "dom" corresponds to the minimum
5805 * of the bounds in "cst". "map_space" is the space of the original
5806 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
5807 * is the space of the original domain.
5809 * We recursively call basic_map_partial_lexopt and then plug in
5810 * the definition of the minimum in the result.
5812 static __isl_give isl_pw_multi_aff *
5813 basic_map_partial_lexopt_symm_core_pw_multi_aff(
5814 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5815 __isl_give isl_set **empty, int max, __isl_take isl_mat *cst,
5816 __isl_take isl_space *map_space, __isl_take isl_space *set_space)
5818 isl_pw_multi_aff *opt;
5819 isl_pw_aff *min_expr_pa;
5820 isl_set *min_expr;
5822 min_expr = set_minimum(isl_basic_set_get_space(dom), isl_mat_copy(cst));
5823 min_expr_pa = set_minimum_pa(isl_basic_set_get_space(dom),
5824 isl_mat_copy(cst));
5826 opt = basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, max);
5828 if (empty) {
5829 *empty = split(*empty,
5830 isl_set_copy(min_expr), isl_mat_copy(cst));
5831 *empty = isl_set_reset_space(*empty, set_space);
5834 opt = split_domain_pma(opt, min_expr_pa, min_expr, cst);
5835 opt = isl_pw_multi_aff_reset_space(opt, map_space);
5837 return opt;
5840 #undef TYPE
5841 #define TYPE isl_pw_multi_aff
5842 #undef SUFFIX
5843 #define SUFFIX _pw_multi_aff
5844 #include "isl_tab_lexopt_templ.c"