interface/python.cc: document order of superclasses
[isl.git] / isl_tab_pip.c
blob63dce300f3ca98260e0fd0e101be8e197808c4ad
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>
26 #include <bset_to_bmap.c>
29 * The implementation of parametric integer linear programming in this file
30 * was inspired by the paper "Parametric Integer Programming" and the
31 * report "Solving systems of affine (in)equalities" by Paul Feautrier
32 * (and others).
34 * The strategy used for obtaining a feasible solution is different
35 * from the one used in isl_tab.c. In particular, in isl_tab.c,
36 * upon finding a constraint that is not yet satisfied, we pivot
37 * in a row that increases the constant term of the row holding the
38 * constraint, making sure the sample solution remains feasible
39 * for all the constraints it already satisfied.
40 * Here, we always pivot in the row holding the constraint,
41 * choosing a column that induces the lexicographically smallest
42 * increment to the sample solution.
44 * By starting out from a sample value that is lexicographically
45 * smaller than any integer point in the problem space, the first
46 * feasible integer sample point we find will also be the lexicographically
47 * smallest. If all variables can be assumed to be non-negative,
48 * then the initial sample value may be chosen equal to zero.
49 * However, we will not make this assumption. Instead, we apply
50 * the "big parameter" trick. Any variable x is then not directly
51 * used in the tableau, but instead it is represented by another
52 * variable x' = M + x, where M is an arbitrarily large (positive)
53 * value. x' is therefore always non-negative, whatever the value of x.
54 * Taking as initial sample value x' = 0 corresponds to x = -M,
55 * which is always smaller than any possible value of x.
57 * The big parameter trick is used in the main tableau and
58 * also in the context tableau if isl_context_lex is used.
59 * In this case, each tableaus has its own big parameter.
60 * Before doing any real work, we check if all the parameters
61 * happen to be non-negative. If so, we drop the column corresponding
62 * to M from the initial context tableau.
63 * If isl_context_gbr is used, then the big parameter trick is only
64 * used in the main tableau.
67 struct isl_context;
68 struct isl_context_op {
69 /* detect nonnegative parameters in context and mark them in tab */
70 struct isl_tab *(*detect_nonnegative_parameters)(
71 struct isl_context *context, struct isl_tab *tab);
72 /* return temporary reference to basic set representation of context */
73 struct isl_basic_set *(*peek_basic_set)(struct isl_context *context);
74 /* return temporary reference to tableau representation of context */
75 struct isl_tab *(*peek_tab)(struct isl_context *context);
76 /* add equality; check is 1 if eq may not be valid;
77 * update is 1 if we may want to call ineq_sign on context later.
79 void (*add_eq)(struct isl_context *context, isl_int *eq,
80 int check, int update);
81 /* add inequality; check is 1 if ineq may not be valid;
82 * update is 1 if we may want to call ineq_sign on context later.
84 void (*add_ineq)(struct isl_context *context, isl_int *ineq,
85 int check, int update);
86 /* check sign of ineq based on previous information.
87 * strict is 1 if saturation should be treated as a positive sign.
89 enum isl_tab_row_sign (*ineq_sign)(struct isl_context *context,
90 isl_int *ineq, int strict);
91 /* check if inequality maintains feasibility */
92 int (*test_ineq)(struct isl_context *context, isl_int *ineq);
93 /* return index of a div that corresponds to "div" */
94 int (*get_div)(struct isl_context *context, struct isl_tab *tab,
95 struct isl_vec *div);
96 /* insert div "div" to context at "pos" and return non-negativity */
97 isl_bool (*insert_div)(struct isl_context *context, int pos,
98 __isl_keep isl_vec *div);
99 int (*detect_equalities)(struct isl_context *context,
100 struct isl_tab *tab);
101 /* return row index of "best" split */
102 int (*best_split)(struct isl_context *context, struct isl_tab *tab);
103 /* check if context has already been determined to be empty */
104 int (*is_empty)(struct isl_context *context);
105 /* check if context is still usable */
106 int (*is_ok)(struct isl_context *context);
107 /* save a copy/snapshot of context */
108 void *(*save)(struct isl_context *context);
109 /* restore saved context */
110 void (*restore)(struct isl_context *context, void *);
111 /* discard saved context */
112 void (*discard)(void *);
113 /* invalidate context */
114 void (*invalidate)(struct isl_context *context);
115 /* free context */
116 __isl_null struct isl_context *(*free)(struct isl_context *context);
119 /* Shared parts of context representation.
121 * "n_unknown" is the number of final unknown integer divisions
122 * in the input domain.
124 struct isl_context {
125 struct isl_context_op *op;
126 int n_unknown;
129 struct isl_context_lex {
130 struct isl_context context;
131 struct isl_tab *tab;
134 /* A stack (linked list) of solutions of subtrees of the search space.
136 * "M" describes the solution in terms of the dimensions of "dom".
137 * The number of columns of "M" is one more than the total number
138 * of dimensions of "dom".
140 * If "M" is NULL, then there is no solution on "dom".
142 struct isl_partial_sol {
143 int level;
144 struct isl_basic_set *dom;
145 struct isl_mat *M;
147 struct isl_partial_sol *next;
150 struct isl_sol;
151 struct isl_sol_callback {
152 struct isl_tab_callback callback;
153 struct isl_sol *sol;
156 /* isl_sol is an interface for constructing a solution to
157 * a parametric integer linear programming problem.
158 * Every time the algorithm reaches a state where a solution
159 * can be read off from the tableau (including cases where the tableau
160 * is empty), the function "add" is called on the isl_sol passed
161 * to find_solutions_main.
163 * The context tableau is owned by isl_sol and is updated incrementally.
165 * There are currently two implementations of this interface,
166 * isl_sol_map, which simply collects the solutions in an isl_map
167 * and (optionally) the parts of the context where there is no solution
168 * in an isl_set, and
169 * isl_sol_for, which calls a user-defined function for each part of
170 * the solution.
172 struct isl_sol {
173 int error;
174 int rational;
175 int level;
176 int max;
177 int n_out;
178 struct isl_context *context;
179 struct isl_partial_sol *partial;
180 void (*add)(struct isl_sol *sol,
181 struct isl_basic_set *dom, struct isl_mat *M);
182 void (*add_empty)(struct isl_sol *sol, struct isl_basic_set *bset);
183 void (*free)(struct isl_sol *sol);
184 struct isl_sol_callback dec_level;
187 static void sol_free(struct isl_sol *sol)
189 struct isl_partial_sol *partial, *next;
190 if (!sol)
191 return;
192 for (partial = sol->partial; partial; partial = next) {
193 next = partial->next;
194 isl_basic_set_free(partial->dom);
195 isl_mat_free(partial->M);
196 free(partial);
198 sol->free(sol);
201 /* Push a partial solution represented by a domain and mapping M
202 * onto the stack of partial solutions.
204 static void sol_push_sol(struct isl_sol *sol,
205 struct isl_basic_set *dom, struct isl_mat *M)
207 struct isl_partial_sol *partial;
209 if (sol->error || !dom)
210 goto error;
212 partial = isl_alloc_type(dom->ctx, struct isl_partial_sol);
213 if (!partial)
214 goto error;
216 partial->level = sol->level;
217 partial->dom = dom;
218 partial->M = M;
219 partial->next = sol->partial;
221 sol->partial = partial;
223 return;
224 error:
225 isl_basic_set_free(dom);
226 isl_mat_free(M);
227 sol->error = 1;
230 /* Pop one partial solution from the partial solution stack and
231 * pass it on to sol->add or sol->add_empty.
233 static void sol_pop_one(struct isl_sol *sol)
235 struct isl_partial_sol *partial;
237 partial = sol->partial;
238 sol->partial = partial->next;
240 if (partial->M)
241 sol->add(sol, partial->dom, partial->M);
242 else
243 sol->add_empty(sol, partial->dom);
244 free(partial);
247 /* Return a fresh copy of the domain represented by the context tableau.
249 static struct isl_basic_set *sol_domain(struct isl_sol *sol)
251 struct isl_basic_set *bset;
253 if (sol->error)
254 return NULL;
256 bset = isl_basic_set_dup(sol->context->op->peek_basic_set(sol->context));
257 bset = isl_basic_set_update_from_tab(bset,
258 sol->context->op->peek_tab(sol->context));
260 return bset;
263 /* Check whether two partial solutions have the same mapping, where n_div
264 * is the number of divs that the two partial solutions have in common.
266 static int same_solution(struct isl_partial_sol *s1, struct isl_partial_sol *s2,
267 unsigned n_div)
269 int i;
270 unsigned dim;
272 if (!s1->M != !s2->M)
273 return 0;
274 if (!s1->M)
275 return 1;
277 dim = isl_basic_set_total_dim(s1->dom) - s1->dom->n_div;
279 for (i = 0; i < s1->M->n_row; ++i) {
280 if (isl_seq_first_non_zero(s1->M->row[i]+1+dim+n_div,
281 s1->M->n_col-1-dim-n_div) != -1)
282 return 0;
283 if (isl_seq_first_non_zero(s2->M->row[i]+1+dim+n_div,
284 s2->M->n_col-1-dim-n_div) != -1)
285 return 0;
286 if (!isl_seq_eq(s1->M->row[i], s2->M->row[i], 1+dim+n_div))
287 return 0;
289 return 1;
292 /* Pop all solutions from the partial solution stack that were pushed onto
293 * the stack at levels that are deeper than the current level.
294 * If the two topmost elements on the stack have the same level
295 * and represent the same solution, then their domains are combined.
296 * This combined domain is the same as the current context domain
297 * as sol_pop is called each time we move back to a higher level.
298 * If the outer level (0) has been reached, then all partial solutions
299 * at the current level are also popped off.
301 static void sol_pop(struct isl_sol *sol)
303 struct isl_partial_sol *partial;
304 unsigned n_div;
306 if (sol->error)
307 return;
309 partial = sol->partial;
310 if (!partial)
311 return;
313 if (partial->level == 0 && sol->level == 0) {
314 for (partial = sol->partial; partial; partial = sol->partial)
315 sol_pop_one(sol);
316 return;
319 if (partial->level <= sol->level)
320 return;
322 if (partial->next && partial->next->level == partial->level) {
323 n_div = isl_basic_set_dim(
324 sol->context->op->peek_basic_set(sol->context),
325 isl_dim_div);
327 if (!same_solution(partial, partial->next, n_div)) {
328 sol_pop_one(sol);
329 sol_pop_one(sol);
330 } else {
331 struct isl_basic_set *bset;
332 isl_mat *M;
333 unsigned n;
335 n = isl_basic_set_dim(partial->next->dom, isl_dim_div);
336 n -= n_div;
337 bset = sol_domain(sol);
338 isl_basic_set_free(partial->next->dom);
339 partial->next->dom = bset;
340 M = partial->next->M;
341 if (M) {
342 M = isl_mat_drop_cols(M, M->n_col - n, n);
343 partial->next->M = M;
344 if (!M)
345 goto error;
347 partial->next->level = sol->level;
349 if (!bset)
350 goto error;
352 sol->partial = partial->next;
353 isl_basic_set_free(partial->dom);
354 isl_mat_free(partial->M);
355 free(partial);
357 } else
358 sol_pop_one(sol);
360 if (sol->level == 0) {
361 for (partial = sol->partial; partial; partial = sol->partial)
362 sol_pop_one(sol);
363 return;
366 if (0)
367 error: sol->error = 1;
370 static void sol_dec_level(struct isl_sol *sol)
372 if (sol->error)
373 return;
375 sol->level--;
377 sol_pop(sol);
380 static int sol_dec_level_wrap(struct isl_tab_callback *cb)
382 struct isl_sol_callback *callback = (struct isl_sol_callback *)cb;
384 sol_dec_level(callback->sol);
386 return callback->sol->error ? -1 : 0;
389 /* Move down to next level and push callback onto context tableau
390 * to decrease the level again when it gets rolled back across
391 * the current state. That is, dec_level will be called with
392 * the context tableau in the same state as it is when inc_level
393 * is called.
395 static void sol_inc_level(struct isl_sol *sol)
397 struct isl_tab *tab;
399 if (sol->error)
400 return;
402 sol->level++;
403 tab = sol->context->op->peek_tab(sol->context);
404 if (isl_tab_push_callback(tab, &sol->dec_level.callback) < 0)
405 sol->error = 1;
408 static void scale_rows(struct isl_mat *mat, isl_int m, int n_row)
410 int i;
412 if (isl_int_is_one(m))
413 return;
415 for (i = 0; i < n_row; ++i)
416 isl_seq_scale(mat->row[i], mat->row[i], m, mat->n_col);
419 /* Add the solution identified by the tableau and the context tableau.
421 * The layout of the variables is as follows.
422 * tab->n_var is equal to the total number of variables in the input
423 * map (including divs that were copied from the context)
424 * + the number of extra divs constructed
425 * Of these, the first tab->n_param and the last tab->n_div variables
426 * correspond to the variables in the context, i.e.,
427 * tab->n_param + tab->n_div = context_tab->n_var
428 * tab->n_param is equal to the number of parameters and input
429 * dimensions in the input map
430 * tab->n_div is equal to the number of divs in the context
432 * If there is no solution, then call add_empty with a basic set
433 * that corresponds to the context tableau. (If add_empty is NULL,
434 * then do nothing).
436 * If there is a solution, then first construct a matrix that maps
437 * all dimensions of the context to the output variables, i.e.,
438 * the output dimensions in the input map.
439 * The divs in the input map (if any) that do not correspond to any
440 * div in the context do not appear in the solution.
441 * The algorithm will make sure that they have an integer value,
442 * but these values themselves are of no interest.
443 * We have to be careful not to drop or rearrange any divs in the
444 * context because that would change the meaning of the matrix.
446 * To extract the value of the output variables, it should be noted
447 * that we always use a big parameter M in the main tableau and so
448 * the variable stored in this tableau is not an output variable x itself, but
449 * x' = M + x (in case of minimization)
450 * or
451 * x' = M - x (in case of maximization)
452 * If x' appears in a column, then its optimal value is zero,
453 * which means that the optimal value of x is an unbounded number
454 * (-M for minimization and M for maximization).
455 * We currently assume that the output dimensions in the original map
456 * are bounded, so this cannot occur.
457 * Similarly, when x' appears in a row, then the coefficient of M in that
458 * row is necessarily 1.
459 * If the row in the tableau represents
460 * d x' = c + d M + e(y)
461 * then, in case of minimization, the corresponding row in the matrix
462 * will be
463 * a c + a e(y)
464 * with a d = m, the (updated) common denominator of the matrix.
465 * In case of maximization, the row will be
466 * -a c - a e(y)
468 static void sol_add(struct isl_sol *sol, struct isl_tab *tab)
470 struct isl_basic_set *bset = NULL;
471 struct isl_mat *mat = NULL;
472 unsigned off;
473 int row;
474 isl_int m;
476 if (sol->error || !tab)
477 goto error;
479 if (tab->empty && !sol->add_empty)
480 return;
481 if (sol->context->op->is_empty(sol->context))
482 return;
484 bset = sol_domain(sol);
486 if (tab->empty) {
487 sol_push_sol(sol, bset, NULL);
488 return;
491 off = 2 + tab->M;
493 mat = isl_mat_alloc(tab->mat->ctx, 1 + sol->n_out,
494 1 + tab->n_param + tab->n_div);
495 if (!mat)
496 goto error;
498 isl_int_init(m);
500 isl_seq_clr(mat->row[0] + 1, mat->n_col - 1);
501 isl_int_set_si(mat->row[0][0], 1);
502 for (row = 0; row < sol->n_out; ++row) {
503 int i = tab->n_param + row;
504 int r, j;
506 isl_seq_clr(mat->row[1 + row], mat->n_col);
507 if (!tab->var[i].is_row) {
508 if (tab->M)
509 isl_die(mat->ctx, isl_error_invalid,
510 "unbounded optimum", goto error2);
511 continue;
514 r = tab->var[i].index;
515 if (tab->M &&
516 isl_int_ne(tab->mat->row[r][2], tab->mat->row[r][0]))
517 isl_die(mat->ctx, isl_error_invalid,
518 "unbounded optimum", goto error2);
519 isl_int_gcd(m, mat->row[0][0], tab->mat->row[r][0]);
520 isl_int_divexact(m, tab->mat->row[r][0], m);
521 scale_rows(mat, m, 1 + row);
522 isl_int_divexact(m, mat->row[0][0], tab->mat->row[r][0]);
523 isl_int_mul(mat->row[1 + row][0], m, tab->mat->row[r][1]);
524 for (j = 0; j < tab->n_param; ++j) {
525 int col;
526 if (tab->var[j].is_row)
527 continue;
528 col = tab->var[j].index;
529 isl_int_mul(mat->row[1 + row][1 + j], m,
530 tab->mat->row[r][off + col]);
532 for (j = 0; j < tab->n_div; ++j) {
533 int col;
534 if (tab->var[tab->n_var - tab->n_div+j].is_row)
535 continue;
536 col = tab->var[tab->n_var - tab->n_div+j].index;
537 isl_int_mul(mat->row[1 + row][1 + tab->n_param + j], m,
538 tab->mat->row[r][off + col]);
540 if (sol->max)
541 isl_seq_neg(mat->row[1 + row], mat->row[1 + row],
542 mat->n_col);
545 isl_int_clear(m);
547 sol_push_sol(sol, bset, mat);
548 return;
549 error2:
550 isl_int_clear(m);
551 error:
552 isl_basic_set_free(bset);
553 isl_mat_free(mat);
554 sol->error = 1;
557 struct isl_sol_map {
558 struct isl_sol sol;
559 struct isl_map *map;
560 struct isl_set *empty;
563 static void sol_map_free(struct isl_sol_map *sol_map)
565 if (!sol_map)
566 return;
567 if (sol_map->sol.context)
568 sol_map->sol.context->op->free(sol_map->sol.context);
569 isl_map_free(sol_map->map);
570 isl_set_free(sol_map->empty);
571 free(sol_map);
574 static void sol_map_free_wrap(struct isl_sol *sol)
576 sol_map_free((struct isl_sol_map *)sol);
579 /* This function is called for parts of the context where there is
580 * no solution, with "bset" corresponding to the context tableau.
581 * Simply add the basic set to the set "empty".
583 static void sol_map_add_empty(struct isl_sol_map *sol,
584 struct isl_basic_set *bset)
586 if (!bset || !sol->empty)
587 goto error;
589 sol->empty = isl_set_grow(sol->empty, 1);
590 bset = isl_basic_set_simplify(bset);
591 bset = isl_basic_set_finalize(bset);
592 sol->empty = isl_set_add_basic_set(sol->empty, isl_basic_set_copy(bset));
593 if (!sol->empty)
594 goto error;
595 isl_basic_set_free(bset);
596 return;
597 error:
598 isl_basic_set_free(bset);
599 sol->sol.error = 1;
602 static void sol_map_add_empty_wrap(struct isl_sol *sol,
603 struct isl_basic_set *bset)
605 sol_map_add_empty((struct isl_sol_map *)sol, bset);
608 /* Given a basic set "dom" that represents the context and an affine
609 * matrix "M" that maps the dimensions of the context to the
610 * output variables, construct a basic map with the same parameters
611 * and divs as the context, the dimensions of the context as input
612 * dimensions and a number of output dimensions that is equal to
613 * the number of output dimensions in the input map.
615 * The constraints and divs of the context are simply copied
616 * from "dom". For each row
617 * x = c + e(y)
618 * an equality
619 * c + e(y) - d x = 0
620 * is added, with d the common denominator of M.
622 static void sol_map_add(struct isl_sol_map *sol,
623 struct isl_basic_set *dom, struct isl_mat *M)
625 int i;
626 struct isl_basic_map *bmap = NULL;
627 unsigned n_eq;
628 unsigned n_ineq;
629 unsigned nparam;
630 unsigned total;
631 unsigned n_div;
632 unsigned n_out;
634 if (sol->sol.error || !dom || !M)
635 goto error;
637 n_out = sol->sol.n_out;
638 n_eq = dom->n_eq + n_out;
639 n_ineq = dom->n_ineq;
640 n_div = dom->n_div;
641 nparam = isl_basic_set_total_dim(dom) - n_div;
642 total = isl_map_dim(sol->map, isl_dim_all);
643 bmap = isl_basic_map_alloc_space(isl_map_get_space(sol->map),
644 n_div, n_eq, 2 * n_div + n_ineq);
645 if (!bmap)
646 goto error;
647 if (sol->sol.rational)
648 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
649 for (i = 0; i < dom->n_div; ++i) {
650 int k = isl_basic_map_alloc_div(bmap);
651 if (k < 0)
652 goto error;
653 isl_seq_cpy(bmap->div[k], dom->div[i], 1 + 1 + nparam);
654 isl_seq_clr(bmap->div[k] + 1 + 1 + nparam, total - nparam);
655 isl_seq_cpy(bmap->div[k] + 1 + 1 + total,
656 dom->div[i] + 1 + 1 + nparam, i);
658 for (i = 0; i < dom->n_eq; ++i) {
659 int k = isl_basic_map_alloc_equality(bmap);
660 if (k < 0)
661 goto error;
662 isl_seq_cpy(bmap->eq[k], dom->eq[i], 1 + nparam);
663 isl_seq_clr(bmap->eq[k] + 1 + nparam, total - nparam);
664 isl_seq_cpy(bmap->eq[k] + 1 + total,
665 dom->eq[i] + 1 + nparam, n_div);
667 for (i = 0; i < dom->n_ineq; ++i) {
668 int k = isl_basic_map_alloc_inequality(bmap);
669 if (k < 0)
670 goto error;
671 isl_seq_cpy(bmap->ineq[k], dom->ineq[i], 1 + nparam);
672 isl_seq_clr(bmap->ineq[k] + 1 + nparam, total - nparam);
673 isl_seq_cpy(bmap->ineq[k] + 1 + total,
674 dom->ineq[i] + 1 + nparam, n_div);
676 for (i = 0; i < M->n_row - 1; ++i) {
677 int k = isl_basic_map_alloc_equality(bmap);
678 if (k < 0)
679 goto error;
680 isl_seq_cpy(bmap->eq[k], M->row[1 + i], 1 + nparam);
681 isl_seq_clr(bmap->eq[k] + 1 + nparam, n_out);
682 isl_int_neg(bmap->eq[k][1 + nparam + i], M->row[0][0]);
683 isl_seq_cpy(bmap->eq[k] + 1 + nparam + n_out,
684 M->row[1 + i] + 1 + nparam, n_div);
686 bmap = isl_basic_map_simplify(bmap);
687 bmap = isl_basic_map_finalize(bmap);
688 sol->map = isl_map_grow(sol->map, 1);
689 sol->map = isl_map_add_basic_map(sol->map, bmap);
690 isl_basic_set_free(dom);
691 isl_mat_free(M);
692 if (!sol->map)
693 sol->sol.error = 1;
694 return;
695 error:
696 isl_basic_set_free(dom);
697 isl_mat_free(M);
698 isl_basic_map_free(bmap);
699 sol->sol.error = 1;
702 static void sol_map_add_wrap(struct isl_sol *sol,
703 struct isl_basic_set *dom, struct isl_mat *M)
705 sol_map_add((struct isl_sol_map *)sol, dom, M);
709 /* Store the "parametric constant" of row "row" of tableau "tab" in "line",
710 * i.e., the constant term and the coefficients of all variables that
711 * appear in the context tableau.
712 * Note that the coefficient of the big parameter M is NOT copied.
713 * The context tableau may not have a big parameter and even when it
714 * does, it is a different big parameter.
716 static void get_row_parameter_line(struct isl_tab *tab, int row, isl_int *line)
718 int i;
719 unsigned off = 2 + tab->M;
721 isl_int_set(line[0], tab->mat->row[row][1]);
722 for (i = 0; i < tab->n_param; ++i) {
723 if (tab->var[i].is_row)
724 isl_int_set_si(line[1 + i], 0);
725 else {
726 int col = tab->var[i].index;
727 isl_int_set(line[1 + i], tab->mat->row[row][off + col]);
730 for (i = 0; i < tab->n_div; ++i) {
731 if (tab->var[tab->n_var - tab->n_div + i].is_row)
732 isl_int_set_si(line[1 + tab->n_param + i], 0);
733 else {
734 int col = tab->var[tab->n_var - tab->n_div + i].index;
735 isl_int_set(line[1 + tab->n_param + i],
736 tab->mat->row[row][off + col]);
741 /* Check if rows "row1" and "row2" have identical "parametric constants",
742 * as explained above.
743 * In this case, we also insist that the coefficients of the big parameter
744 * be the same as the values of the constants will only be the same
745 * if these coefficients are also the same.
747 static int identical_parameter_line(struct isl_tab *tab, int row1, int row2)
749 int i;
750 unsigned off = 2 + tab->M;
752 if (isl_int_ne(tab->mat->row[row1][1], tab->mat->row[row2][1]))
753 return 0;
755 if (tab->M && isl_int_ne(tab->mat->row[row1][2],
756 tab->mat->row[row2][2]))
757 return 0;
759 for (i = 0; i < tab->n_param + tab->n_div; ++i) {
760 int pos = i < tab->n_param ? i :
761 tab->n_var - tab->n_div + i - tab->n_param;
762 int col;
764 if (tab->var[pos].is_row)
765 continue;
766 col = tab->var[pos].index;
767 if (isl_int_ne(tab->mat->row[row1][off + col],
768 tab->mat->row[row2][off + col]))
769 return 0;
771 return 1;
774 /* Return an inequality that expresses that the "parametric constant"
775 * should be non-negative.
776 * This function is only called when the coefficient of the big parameter
777 * is equal to zero.
779 static struct isl_vec *get_row_parameter_ineq(struct isl_tab *tab, int row)
781 struct isl_vec *ineq;
783 ineq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_param + tab->n_div);
784 if (!ineq)
785 return NULL;
787 get_row_parameter_line(tab, row, ineq->el);
788 if (ineq)
789 ineq = isl_vec_normalize(ineq);
791 return ineq;
794 /* Normalize a div expression of the form
796 * [(g*f(x) + c)/(g * m)]
798 * with c the constant term and f(x) the remaining coefficients, to
800 * [(f(x) + [c/g])/m]
802 static void normalize_div(__isl_keep isl_vec *div)
804 isl_ctx *ctx = isl_vec_get_ctx(div);
805 int len = div->size - 2;
807 isl_seq_gcd(div->el + 2, len, &ctx->normalize_gcd);
808 isl_int_gcd(ctx->normalize_gcd, ctx->normalize_gcd, div->el[0]);
810 if (isl_int_is_one(ctx->normalize_gcd))
811 return;
813 isl_int_divexact(div->el[0], div->el[0], ctx->normalize_gcd);
814 isl_int_fdiv_q(div->el[1], div->el[1], ctx->normalize_gcd);
815 isl_seq_scale_down(div->el + 2, div->el + 2, ctx->normalize_gcd, len);
818 /* Return an integer division for use in a parametric cut based
819 * on the given row.
820 * In particular, let the parametric constant of the row be
822 * \sum_i a_i y_i
824 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
825 * The div returned is equal to
827 * floor(\sum_i {-a_i} y_i) = floor((\sum_i (-a_i mod d) y_i)/d)
829 static struct isl_vec *get_row_parameter_div(struct isl_tab *tab, int row)
831 struct isl_vec *div;
833 div = isl_vec_alloc(tab->mat->ctx, 1 + 1 + tab->n_param + tab->n_div);
834 if (!div)
835 return NULL;
837 isl_int_set(div->el[0], tab->mat->row[row][0]);
838 get_row_parameter_line(tab, row, div->el + 1);
839 isl_seq_neg(div->el + 1, div->el + 1, div->size - 1);
840 normalize_div(div);
841 isl_seq_fdiv_r(div->el + 1, div->el + 1, div->el[0], div->size - 1);
843 return div;
846 /* Return an integer division for use in transferring an integrality constraint
847 * to the context.
848 * In particular, let the parametric constant of the row be
850 * \sum_i a_i y_i
852 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
853 * The the returned div is equal to
855 * floor(\sum_i {a_i} y_i) = floor((\sum_i (a_i mod d) y_i)/d)
857 static struct isl_vec *get_row_split_div(struct isl_tab *tab, int row)
859 struct isl_vec *div;
861 div = isl_vec_alloc(tab->mat->ctx, 1 + 1 + tab->n_param + tab->n_div);
862 if (!div)
863 return NULL;
865 isl_int_set(div->el[0], tab->mat->row[row][0]);
866 get_row_parameter_line(tab, row, div->el + 1);
867 normalize_div(div);
868 isl_seq_fdiv_r(div->el + 1, div->el + 1, div->el[0], div->size - 1);
870 return div;
873 /* Construct and return an inequality that expresses an upper bound
874 * on the given div.
875 * In particular, if the div is given by
877 * d = floor(e/m)
879 * then the inequality expresses
881 * m d <= e
883 static struct isl_vec *ineq_for_div(struct isl_basic_set *bset, unsigned div)
885 unsigned total;
886 unsigned div_pos;
887 struct isl_vec *ineq;
889 if (!bset)
890 return NULL;
892 total = isl_basic_set_total_dim(bset);
893 div_pos = 1 + total - bset->n_div + div;
895 ineq = isl_vec_alloc(bset->ctx, 1 + total);
896 if (!ineq)
897 return NULL;
899 isl_seq_cpy(ineq->el, bset->div[div] + 1, 1 + total);
900 isl_int_neg(ineq->el[div_pos], bset->div[div][0]);
901 return ineq;
904 /* Given a row in the tableau and a div that was created
905 * using get_row_split_div and that has been constrained to equality, i.e.,
907 * d = floor(\sum_i {a_i} y_i) = \sum_i {a_i} y_i
909 * replace the expression "\sum_i {a_i} y_i" in the row by d,
910 * i.e., we subtract "\sum_i {a_i} y_i" and add 1 d.
911 * The coefficients of the non-parameters in the tableau have been
912 * verified to be integral. We can therefore simply replace coefficient b
913 * by floor(b). For the coefficients of the parameters we have
914 * floor(a_i) = a_i - {a_i}, while for the other coefficients, we have
915 * floor(b) = b.
917 static struct isl_tab *set_row_cst_to_div(struct isl_tab *tab, int row, int div)
919 isl_seq_fdiv_q(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
920 tab->mat->row[row][0], 1 + tab->M + tab->n_col);
922 isl_int_set_si(tab->mat->row[row][0], 1);
924 if (tab->var[tab->n_var - tab->n_div + div].is_row) {
925 int drow = tab->var[tab->n_var - tab->n_div + div].index;
927 isl_assert(tab->mat->ctx,
928 isl_int_is_one(tab->mat->row[drow][0]), goto error);
929 isl_seq_combine(tab->mat->row[row] + 1,
930 tab->mat->ctx->one, tab->mat->row[row] + 1,
931 tab->mat->ctx->one, tab->mat->row[drow] + 1,
932 1 + tab->M + tab->n_col);
933 } else {
934 int dcol = tab->var[tab->n_var - tab->n_div + div].index;
936 isl_int_add_ui(tab->mat->row[row][2 + tab->M + dcol],
937 tab->mat->row[row][2 + tab->M + dcol], 1);
940 return tab;
941 error:
942 isl_tab_free(tab);
943 return NULL;
946 /* Check if the (parametric) constant of the given row is obviously
947 * negative, meaning that we don't need to consult the context tableau.
948 * If there is a big parameter and its coefficient is non-zero,
949 * then this coefficient determines the outcome.
950 * Otherwise, we check whether the constant is negative and
951 * all non-zero coefficients of parameters are negative and
952 * belong to non-negative parameters.
954 static int is_obviously_neg(struct isl_tab *tab, int row)
956 int i;
957 int col;
958 unsigned off = 2 + tab->M;
960 if (tab->M) {
961 if (isl_int_is_pos(tab->mat->row[row][2]))
962 return 0;
963 if (isl_int_is_neg(tab->mat->row[row][2]))
964 return 1;
967 if (isl_int_is_nonneg(tab->mat->row[row][1]))
968 return 0;
969 for (i = 0; i < tab->n_param; ++i) {
970 /* Eliminated parameter */
971 if (tab->var[i].is_row)
972 continue;
973 col = tab->var[i].index;
974 if (isl_int_is_zero(tab->mat->row[row][off + col]))
975 continue;
976 if (!tab->var[i].is_nonneg)
977 return 0;
978 if (isl_int_is_pos(tab->mat->row[row][off + col]))
979 return 0;
981 for (i = 0; i < tab->n_div; ++i) {
982 if (tab->var[tab->n_var - tab->n_div + i].is_row)
983 continue;
984 col = tab->var[tab->n_var - tab->n_div + i].index;
985 if (isl_int_is_zero(tab->mat->row[row][off + col]))
986 continue;
987 if (!tab->var[tab->n_var - tab->n_div + i].is_nonneg)
988 return 0;
989 if (isl_int_is_pos(tab->mat->row[row][off + col]))
990 return 0;
992 return 1;
995 /* Check if the (parametric) constant of the given row is obviously
996 * non-negative, meaning that we don't need to consult the context tableau.
997 * If there is a big parameter and its coefficient is non-zero,
998 * then this coefficient determines the outcome.
999 * Otherwise, we check whether the constant is non-negative and
1000 * all non-zero coefficients of parameters are positive and
1001 * belong to non-negative parameters.
1003 static int is_obviously_nonneg(struct isl_tab *tab, int row)
1005 int i;
1006 int col;
1007 unsigned off = 2 + tab->M;
1009 if (tab->M) {
1010 if (isl_int_is_pos(tab->mat->row[row][2]))
1011 return 1;
1012 if (isl_int_is_neg(tab->mat->row[row][2]))
1013 return 0;
1016 if (isl_int_is_neg(tab->mat->row[row][1]))
1017 return 0;
1018 for (i = 0; i < tab->n_param; ++i) {
1019 /* Eliminated parameter */
1020 if (tab->var[i].is_row)
1021 continue;
1022 col = tab->var[i].index;
1023 if (isl_int_is_zero(tab->mat->row[row][off + col]))
1024 continue;
1025 if (!tab->var[i].is_nonneg)
1026 return 0;
1027 if (isl_int_is_neg(tab->mat->row[row][off + col]))
1028 return 0;
1030 for (i = 0; i < tab->n_div; ++i) {
1031 if (tab->var[tab->n_var - tab->n_div + i].is_row)
1032 continue;
1033 col = tab->var[tab->n_var - tab->n_div + i].index;
1034 if (isl_int_is_zero(tab->mat->row[row][off + col]))
1035 continue;
1036 if (!tab->var[tab->n_var - tab->n_div + i].is_nonneg)
1037 return 0;
1038 if (isl_int_is_neg(tab->mat->row[row][off + col]))
1039 return 0;
1041 return 1;
1044 /* Given a row r and two columns, return the column that would
1045 * lead to the lexicographically smallest increment in the sample
1046 * solution when leaving the basis in favor of the row.
1047 * Pivoting with column c will increment the sample value by a non-negative
1048 * constant times a_{V,c}/a_{r,c}, with a_{V,c} the elements of column c
1049 * corresponding to the non-parametric variables.
1050 * If variable v appears in a column c_v, the a_{v,c} = 1 iff c = c_v,
1051 * with all other entries in this virtual row equal to zero.
1052 * If variable v appears in a row, then a_{v,c} is the element in column c
1053 * of that row.
1055 * Let v be the first variable with a_{v,c1}/a_{r,c1} != a_{v,c2}/a_{r,c2}.
1056 * Then if a_{v,c1}/a_{r,c1} < a_{v,c2}/a_{r,c2}, i.e.,
1057 * a_{v,c2} a_{r,c1} - a_{v,c1} a_{r,c2} > 0, c1 results in the minimal
1058 * increment. Otherwise, it's c2.
1060 static int lexmin_col_pair(struct isl_tab *tab,
1061 int row, int col1, int col2, isl_int tmp)
1063 int i;
1064 isl_int *tr;
1066 tr = tab->mat->row[row] + 2 + tab->M;
1068 for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
1069 int s1, s2;
1070 isl_int *r;
1072 if (!tab->var[i].is_row) {
1073 if (tab->var[i].index == col1)
1074 return col2;
1075 if (tab->var[i].index == col2)
1076 return col1;
1077 continue;
1080 if (tab->var[i].index == row)
1081 continue;
1083 r = tab->mat->row[tab->var[i].index] + 2 + tab->M;
1084 s1 = isl_int_sgn(r[col1]);
1085 s2 = isl_int_sgn(r[col2]);
1086 if (s1 == 0 && s2 == 0)
1087 continue;
1088 if (s1 < s2)
1089 return col1;
1090 if (s2 < s1)
1091 return col2;
1093 isl_int_mul(tmp, r[col2], tr[col1]);
1094 isl_int_submul(tmp, r[col1], tr[col2]);
1095 if (isl_int_is_pos(tmp))
1096 return col1;
1097 if (isl_int_is_neg(tmp))
1098 return col2;
1100 return -1;
1103 /* Given a row in the tableau, find and return the column that would
1104 * result in the lexicographically smallest, but positive, increment
1105 * in the sample point.
1106 * If there is no such column, then return tab->n_col.
1107 * If anything goes wrong, return -1.
1109 static int lexmin_pivot_col(struct isl_tab *tab, int row)
1111 int j;
1112 int col = tab->n_col;
1113 isl_int *tr;
1114 isl_int tmp;
1116 tr = tab->mat->row[row] + 2 + tab->M;
1118 isl_int_init(tmp);
1120 for (j = tab->n_dead; j < tab->n_col; ++j) {
1121 if (tab->col_var[j] >= 0 &&
1122 (tab->col_var[j] < tab->n_param ||
1123 tab->col_var[j] >= tab->n_var - tab->n_div))
1124 continue;
1126 if (!isl_int_is_pos(tr[j]))
1127 continue;
1129 if (col == tab->n_col)
1130 col = j;
1131 else
1132 col = lexmin_col_pair(tab, row, col, j, tmp);
1133 isl_assert(tab->mat->ctx, col >= 0, goto error);
1136 isl_int_clear(tmp);
1137 return col;
1138 error:
1139 isl_int_clear(tmp);
1140 return -1;
1143 /* Return the first known violated constraint, i.e., a non-negative
1144 * constraint that currently has an either obviously negative value
1145 * or a previously determined to be negative value.
1147 * If any constraint has a negative coefficient for the big parameter,
1148 * if any, then we return one of these first.
1150 static int first_neg(struct isl_tab *tab)
1152 int row;
1154 if (tab->M)
1155 for (row = tab->n_redundant; row < tab->n_row; ++row) {
1156 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
1157 continue;
1158 if (!isl_int_is_neg(tab->mat->row[row][2]))
1159 continue;
1160 if (tab->row_sign)
1161 tab->row_sign[row] = isl_tab_row_neg;
1162 return row;
1164 for (row = tab->n_redundant; row < tab->n_row; ++row) {
1165 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
1166 continue;
1167 if (tab->row_sign) {
1168 if (tab->row_sign[row] == 0 &&
1169 is_obviously_neg(tab, row))
1170 tab->row_sign[row] = isl_tab_row_neg;
1171 if (tab->row_sign[row] != isl_tab_row_neg)
1172 continue;
1173 } else if (!is_obviously_neg(tab, row))
1174 continue;
1175 return row;
1177 return -1;
1180 /* Check whether the invariant that all columns are lexico-positive
1181 * is satisfied. This function is not called from the current code
1182 * but is useful during debugging.
1184 static void check_lexpos(struct isl_tab *tab) __attribute__ ((unused));
1185 static void check_lexpos(struct isl_tab *tab)
1187 unsigned off = 2 + tab->M;
1188 int col;
1189 int var;
1190 int row;
1192 for (col = tab->n_dead; col < tab->n_col; ++col) {
1193 if (tab->col_var[col] >= 0 &&
1194 (tab->col_var[col] < tab->n_param ||
1195 tab->col_var[col] >= tab->n_var - tab->n_div))
1196 continue;
1197 for (var = tab->n_param; var < tab->n_var - tab->n_div; ++var) {
1198 if (!tab->var[var].is_row) {
1199 if (tab->var[var].index == col)
1200 break;
1201 else
1202 continue;
1204 row = tab->var[var].index;
1205 if (isl_int_is_zero(tab->mat->row[row][off + col]))
1206 continue;
1207 if (isl_int_is_pos(tab->mat->row[row][off + col]))
1208 break;
1209 fprintf(stderr, "lexneg column %d (row %d)\n",
1210 col, row);
1212 if (var >= tab->n_var - tab->n_div)
1213 fprintf(stderr, "zero column %d\n", col);
1217 /* Report to the caller that the given constraint is part of an encountered
1218 * conflict.
1220 static int report_conflicting_constraint(struct isl_tab *tab, int con)
1222 return tab->conflict(con, tab->conflict_user);
1225 /* Given a conflicting row in the tableau, report all constraints
1226 * involved in the row to the caller. That is, the row itself
1227 * (if it represents a constraint) and all constraint columns with
1228 * non-zero (and therefore negative) coefficients.
1230 static int report_conflict(struct isl_tab *tab, int row)
1232 int j;
1233 isl_int *tr;
1235 if (!tab->conflict)
1236 return 0;
1238 if (tab->row_var[row] < 0 &&
1239 report_conflicting_constraint(tab, ~tab->row_var[row]) < 0)
1240 return -1;
1242 tr = tab->mat->row[row] + 2 + tab->M;
1244 for (j = tab->n_dead; j < tab->n_col; ++j) {
1245 if (tab->col_var[j] >= 0 &&
1246 (tab->col_var[j] < tab->n_param ||
1247 tab->col_var[j] >= tab->n_var - tab->n_div))
1248 continue;
1250 if (!isl_int_is_neg(tr[j]))
1251 continue;
1253 if (tab->col_var[j] < 0 &&
1254 report_conflicting_constraint(tab, ~tab->col_var[j]) < 0)
1255 return -1;
1258 return 0;
1261 /* Resolve all known or obviously violated constraints through pivoting.
1262 * In particular, as long as we can find any violated constraint, we
1263 * look for a pivoting column that would result in the lexicographically
1264 * smallest increment in the sample point. If there is no such column
1265 * then the tableau is infeasible.
1267 static int restore_lexmin(struct isl_tab *tab) WARN_UNUSED;
1268 static int restore_lexmin(struct isl_tab *tab)
1270 int row, col;
1272 if (!tab)
1273 return -1;
1274 if (tab->empty)
1275 return 0;
1276 while ((row = first_neg(tab)) != -1) {
1277 col = lexmin_pivot_col(tab, row);
1278 if (col >= tab->n_col) {
1279 if (report_conflict(tab, row) < 0)
1280 return -1;
1281 if (isl_tab_mark_empty(tab) < 0)
1282 return -1;
1283 return 0;
1285 if (col < 0)
1286 return -1;
1287 if (isl_tab_pivot(tab, row, col) < 0)
1288 return -1;
1290 return 0;
1293 /* Given a row that represents an equality, look for an appropriate
1294 * pivoting column.
1295 * In particular, if there are any non-zero coefficients among
1296 * the non-parameter variables, then we take the last of these
1297 * variables. Eliminating this variable in terms of the other
1298 * variables and/or parameters does not influence the property
1299 * that all column in the initial tableau are lexicographically
1300 * positive. The row corresponding to the eliminated variable
1301 * will only have non-zero entries below the diagonal of the
1302 * initial tableau. That is, we transform
1304 * I I
1305 * 1 into a
1306 * I I
1308 * If there is no such non-parameter variable, then we are dealing with
1309 * pure parameter equality and we pick any parameter with coefficient 1 or -1
1310 * for elimination. This will ensure that the eliminated parameter
1311 * always has an integer value whenever all the other parameters are integral.
1312 * If there is no such parameter then we return -1.
1314 static int last_var_col_or_int_par_col(struct isl_tab *tab, int row)
1316 unsigned off = 2 + tab->M;
1317 int i;
1319 for (i = tab->n_var - tab->n_div - 1; i >= 0 && i >= tab->n_param; --i) {
1320 int col;
1321 if (tab->var[i].is_row)
1322 continue;
1323 col = tab->var[i].index;
1324 if (col <= tab->n_dead)
1325 continue;
1326 if (!isl_int_is_zero(tab->mat->row[row][off + col]))
1327 return col;
1329 for (i = tab->n_dead; i < tab->n_col; ++i) {
1330 if (isl_int_is_one(tab->mat->row[row][off + i]))
1331 return i;
1332 if (isl_int_is_negone(tab->mat->row[row][off + i]))
1333 return i;
1335 return -1;
1338 /* Add an equality that is known to be valid to the tableau.
1339 * We first check if we can eliminate a variable or a parameter.
1340 * If not, we add the equality as two inequalities.
1341 * In this case, the equality was a pure parameter equality and there
1342 * is no need to resolve any constraint violations.
1344 * This function assumes that at least two more rows and at least
1345 * two more elements in the constraint array are available in the tableau.
1347 static struct isl_tab *add_lexmin_valid_eq(struct isl_tab *tab, isl_int *eq)
1349 int i;
1350 int r;
1352 if (!tab)
1353 return NULL;
1354 r = isl_tab_add_row(tab, eq);
1355 if (r < 0)
1356 goto error;
1358 r = tab->con[r].index;
1359 i = last_var_col_or_int_par_col(tab, r);
1360 if (i < 0) {
1361 tab->con[r].is_nonneg = 1;
1362 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1363 goto error;
1364 isl_seq_neg(eq, eq, 1 + tab->n_var);
1365 r = isl_tab_add_row(tab, eq);
1366 if (r < 0)
1367 goto error;
1368 tab->con[r].is_nonneg = 1;
1369 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1370 goto error;
1371 } else {
1372 if (isl_tab_pivot(tab, r, i) < 0)
1373 goto error;
1374 if (isl_tab_kill_col(tab, i) < 0)
1375 goto error;
1376 tab->n_eq++;
1379 return tab;
1380 error:
1381 isl_tab_free(tab);
1382 return NULL;
1385 /* Check if the given row is a pure constant.
1387 static int is_constant(struct isl_tab *tab, int row)
1389 unsigned off = 2 + tab->M;
1391 return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1392 tab->n_col - tab->n_dead) == -1;
1395 /* Add an equality that may or may not be valid to the tableau.
1396 * If the resulting row is a pure constant, then it must be zero.
1397 * Otherwise, the resulting tableau is empty.
1399 * If the row is not a pure constant, then we add two inequalities,
1400 * each time checking that they can be satisfied.
1401 * In the end we try to use one of the two constraints to eliminate
1402 * a column.
1404 * This function assumes that at least two more rows and at least
1405 * two more elements in the constraint array are available in the tableau.
1407 static int add_lexmin_eq(struct isl_tab *tab, isl_int *eq) WARN_UNUSED;
1408 static int add_lexmin_eq(struct isl_tab *tab, isl_int *eq)
1410 int r1, r2;
1411 int row;
1412 struct isl_tab_undo *snap;
1414 if (!tab)
1415 return -1;
1416 snap = isl_tab_snap(tab);
1417 r1 = isl_tab_add_row(tab, eq);
1418 if (r1 < 0)
1419 return -1;
1420 tab->con[r1].is_nonneg = 1;
1421 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r1]) < 0)
1422 return -1;
1424 row = tab->con[r1].index;
1425 if (is_constant(tab, row)) {
1426 if (!isl_int_is_zero(tab->mat->row[row][1]) ||
1427 (tab->M && !isl_int_is_zero(tab->mat->row[row][2]))) {
1428 if (isl_tab_mark_empty(tab) < 0)
1429 return -1;
1430 return 0;
1432 if (isl_tab_rollback(tab, snap) < 0)
1433 return -1;
1434 return 0;
1437 if (restore_lexmin(tab) < 0)
1438 return -1;
1439 if (tab->empty)
1440 return 0;
1442 isl_seq_neg(eq, eq, 1 + tab->n_var);
1444 r2 = isl_tab_add_row(tab, eq);
1445 if (r2 < 0)
1446 return -1;
1447 tab->con[r2].is_nonneg = 1;
1448 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r2]) < 0)
1449 return -1;
1451 if (restore_lexmin(tab) < 0)
1452 return -1;
1453 if (tab->empty)
1454 return 0;
1456 if (!tab->con[r1].is_row) {
1457 if (isl_tab_kill_col(tab, tab->con[r1].index) < 0)
1458 return -1;
1459 } else if (!tab->con[r2].is_row) {
1460 if (isl_tab_kill_col(tab, tab->con[r2].index) < 0)
1461 return -1;
1464 if (tab->bmap) {
1465 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1466 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1467 return -1;
1468 isl_seq_neg(eq, eq, 1 + tab->n_var);
1469 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1470 isl_seq_neg(eq, eq, 1 + tab->n_var);
1471 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1472 return -1;
1473 if (!tab->bmap)
1474 return -1;
1477 return 0;
1480 /* Add an inequality to the tableau, resolving violations using
1481 * restore_lexmin.
1483 * This function assumes that at least one more row and at least
1484 * one more element in the constraint array are available in the tableau.
1486 static struct isl_tab *add_lexmin_ineq(struct isl_tab *tab, isl_int *ineq)
1488 int r;
1490 if (!tab)
1491 return NULL;
1492 if (tab->bmap) {
1493 tab->bmap = isl_basic_map_add_ineq(tab->bmap, ineq);
1494 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1495 goto error;
1496 if (!tab->bmap)
1497 goto error;
1499 r = isl_tab_add_row(tab, ineq);
1500 if (r < 0)
1501 goto error;
1502 tab->con[r].is_nonneg = 1;
1503 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1504 goto error;
1505 if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1506 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1507 goto error;
1508 return tab;
1511 if (restore_lexmin(tab) < 0)
1512 goto error;
1513 if (!tab->empty && tab->con[r].is_row &&
1514 isl_tab_row_is_redundant(tab, tab->con[r].index))
1515 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1516 goto error;
1517 return tab;
1518 error:
1519 isl_tab_free(tab);
1520 return NULL;
1523 /* Check if the coefficients of the parameters are all integral.
1525 static int integer_parameter(struct isl_tab *tab, int row)
1527 int i;
1528 int col;
1529 unsigned off = 2 + tab->M;
1531 for (i = 0; i < tab->n_param; ++i) {
1532 /* Eliminated parameter */
1533 if (tab->var[i].is_row)
1534 continue;
1535 col = tab->var[i].index;
1536 if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
1537 tab->mat->row[row][0]))
1538 return 0;
1540 for (i = 0; i < tab->n_div; ++i) {
1541 if (tab->var[tab->n_var - tab->n_div + i].is_row)
1542 continue;
1543 col = tab->var[tab->n_var - tab->n_div + i].index;
1544 if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
1545 tab->mat->row[row][0]))
1546 return 0;
1548 return 1;
1551 /* Check if the coefficients of the non-parameter variables are all integral.
1553 static int integer_variable(struct isl_tab *tab, int row)
1555 int i;
1556 unsigned off = 2 + tab->M;
1558 for (i = tab->n_dead; i < tab->n_col; ++i) {
1559 if (tab->col_var[i] >= 0 &&
1560 (tab->col_var[i] < tab->n_param ||
1561 tab->col_var[i] >= tab->n_var - tab->n_div))
1562 continue;
1563 if (!isl_int_is_divisible_by(tab->mat->row[row][off + i],
1564 tab->mat->row[row][0]))
1565 return 0;
1567 return 1;
1570 /* Check if the constant term is integral.
1572 static int integer_constant(struct isl_tab *tab, int row)
1574 return isl_int_is_divisible_by(tab->mat->row[row][1],
1575 tab->mat->row[row][0]);
1578 #define I_CST 1 << 0
1579 #define I_PAR 1 << 1
1580 #define I_VAR 1 << 2
1582 /* Check for next (non-parameter) variable after "var" (first if var == -1)
1583 * that is non-integer and therefore requires a cut and return
1584 * the index of the variable.
1585 * For parametric tableaus, there are three parts in a row,
1586 * the constant, the coefficients of the parameters and the rest.
1587 * For each part, we check whether the coefficients in that part
1588 * are all integral and if so, set the corresponding flag in *f.
1589 * If the constant and the parameter part are integral, then the
1590 * current sample value is integral and no cut is required
1591 * (irrespective of whether the variable part is integral).
1593 static int next_non_integer_var(struct isl_tab *tab, int var, int *f)
1595 var = var < 0 ? tab->n_param : var + 1;
1597 for (; var < tab->n_var - tab->n_div; ++var) {
1598 int flags = 0;
1599 int row;
1600 if (!tab->var[var].is_row)
1601 continue;
1602 row = tab->var[var].index;
1603 if (integer_constant(tab, row))
1604 ISL_FL_SET(flags, I_CST);
1605 if (integer_parameter(tab, row))
1606 ISL_FL_SET(flags, I_PAR);
1607 if (ISL_FL_ISSET(flags, I_CST) && ISL_FL_ISSET(flags, I_PAR))
1608 continue;
1609 if (integer_variable(tab, row))
1610 ISL_FL_SET(flags, I_VAR);
1611 *f = flags;
1612 return var;
1614 return -1;
1617 /* Check for first (non-parameter) variable that is non-integer and
1618 * therefore requires a cut and return the corresponding row.
1619 * For parametric tableaus, there are three parts in a row,
1620 * the constant, the coefficients of the parameters and the rest.
1621 * For each part, we check whether the coefficients in that part
1622 * are all integral and if so, set the corresponding flag in *f.
1623 * If the constant and the parameter part are integral, then the
1624 * current sample value is integral and no cut is required
1625 * (irrespective of whether the variable part is integral).
1627 static int first_non_integer_row(struct isl_tab *tab, int *f)
1629 int var = next_non_integer_var(tab, -1, f);
1631 return var < 0 ? -1 : tab->var[var].index;
1634 /* Add a (non-parametric) cut to cut away the non-integral sample
1635 * value of the given row.
1637 * If the row is given by
1639 * m r = f + \sum_i a_i y_i
1641 * then the cut is
1643 * c = - {-f/m} + \sum_i {a_i/m} y_i >= 0
1645 * The big parameter, if any, is ignored, since it is assumed to be big
1646 * enough to be divisible by any integer.
1647 * If the tableau is actually a parametric tableau, then this function
1648 * is only called when all coefficients of the parameters are integral.
1649 * The cut therefore has zero coefficients for the parameters.
1651 * The current value is known to be negative, so row_sign, if it
1652 * exists, is set accordingly.
1654 * Return the row of the cut or -1.
1656 static int add_cut(struct isl_tab *tab, int row)
1658 int i;
1659 int r;
1660 isl_int *r_row;
1661 unsigned off = 2 + tab->M;
1663 if (isl_tab_extend_cons(tab, 1) < 0)
1664 return -1;
1665 r = isl_tab_allocate_con(tab);
1666 if (r < 0)
1667 return -1;
1669 r_row = tab->mat->row[tab->con[r].index];
1670 isl_int_set(r_row[0], tab->mat->row[row][0]);
1671 isl_int_neg(r_row[1], tab->mat->row[row][1]);
1672 isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
1673 isl_int_neg(r_row[1], r_row[1]);
1674 if (tab->M)
1675 isl_int_set_si(r_row[2], 0);
1676 for (i = 0; i < tab->n_col; ++i)
1677 isl_int_fdiv_r(r_row[off + i],
1678 tab->mat->row[row][off + i], tab->mat->row[row][0]);
1680 tab->con[r].is_nonneg = 1;
1681 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1682 return -1;
1683 if (tab->row_sign)
1684 tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
1686 return tab->con[r].index;
1689 #define CUT_ALL 1
1690 #define CUT_ONE 0
1692 /* Given a non-parametric tableau, add cuts until an integer
1693 * sample point is obtained or until the tableau is determined
1694 * to be integer infeasible.
1695 * As long as there is any non-integer value in the sample point,
1696 * we add appropriate cuts, if possible, for each of these
1697 * non-integer values and then resolve the violated
1698 * cut constraints using restore_lexmin.
1699 * If one of the corresponding rows is equal to an integral
1700 * combination of variables/constraints plus a non-integral constant,
1701 * then there is no way to obtain an integer point and we return
1702 * a tableau that is marked empty.
1703 * The parameter cutting_strategy controls the strategy used when adding cuts
1704 * to remove non-integer points. CUT_ALL adds all possible cuts
1705 * before continuing the search. CUT_ONE adds only one cut at a time.
1707 static struct isl_tab *cut_to_integer_lexmin(struct isl_tab *tab,
1708 int cutting_strategy)
1710 int var;
1711 int row;
1712 int flags;
1714 if (!tab)
1715 return NULL;
1716 if (tab->empty)
1717 return tab;
1719 while ((var = next_non_integer_var(tab, -1, &flags)) != -1) {
1720 do {
1721 if (ISL_FL_ISSET(flags, I_VAR)) {
1722 if (isl_tab_mark_empty(tab) < 0)
1723 goto error;
1724 return tab;
1726 row = tab->var[var].index;
1727 row = add_cut(tab, row);
1728 if (row < 0)
1729 goto error;
1730 if (cutting_strategy == CUT_ONE)
1731 break;
1732 } while ((var = next_non_integer_var(tab, var, &flags)) != -1);
1733 if (restore_lexmin(tab) < 0)
1734 goto error;
1735 if (tab->empty)
1736 break;
1738 return tab;
1739 error:
1740 isl_tab_free(tab);
1741 return NULL;
1744 /* Check whether all the currently active samples also satisfy the inequality
1745 * "ineq" (treated as an equality if eq is set).
1746 * Remove those samples that do not.
1748 static struct isl_tab *check_samples(struct isl_tab *tab, isl_int *ineq, int eq)
1750 int i;
1751 isl_int v;
1753 if (!tab)
1754 return NULL;
1756 isl_assert(tab->mat->ctx, tab->bmap, goto error);
1757 isl_assert(tab->mat->ctx, tab->samples, goto error);
1758 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, goto error);
1760 isl_int_init(v);
1761 for (i = tab->n_outside; i < tab->n_sample; ++i) {
1762 int sgn;
1763 isl_seq_inner_product(ineq, tab->samples->row[i],
1764 1 + tab->n_var, &v);
1765 sgn = isl_int_sgn(v);
1766 if (eq ? (sgn == 0) : (sgn >= 0))
1767 continue;
1768 tab = isl_tab_drop_sample(tab, i);
1769 if (!tab)
1770 break;
1772 isl_int_clear(v);
1774 return tab;
1775 error:
1776 isl_tab_free(tab);
1777 return NULL;
1780 /* Check whether the sample value of the tableau is finite,
1781 * i.e., either the tableau does not use a big parameter, or
1782 * all values of the variables are equal to the big parameter plus
1783 * some constant. This constant is the actual sample value.
1785 static int sample_is_finite(struct isl_tab *tab)
1787 int i;
1789 if (!tab->M)
1790 return 1;
1792 for (i = 0; i < tab->n_var; ++i) {
1793 int row;
1794 if (!tab->var[i].is_row)
1795 return 0;
1796 row = tab->var[i].index;
1797 if (isl_int_ne(tab->mat->row[row][0], tab->mat->row[row][2]))
1798 return 0;
1800 return 1;
1803 /* Check if the context tableau of sol has any integer points.
1804 * Leave tab in empty state if no integer point can be found.
1805 * If an integer point can be found and if moreover it is finite,
1806 * then it is added to the list of sample values.
1808 * This function is only called when none of the currently active sample
1809 * values satisfies the most recently added constraint.
1811 static struct isl_tab *check_integer_feasible(struct isl_tab *tab)
1813 struct isl_tab_undo *snap;
1815 if (!tab)
1816 return NULL;
1818 snap = isl_tab_snap(tab);
1819 if (isl_tab_push_basis(tab) < 0)
1820 goto error;
1822 tab = cut_to_integer_lexmin(tab, CUT_ALL);
1823 if (!tab)
1824 goto error;
1826 if (!tab->empty && sample_is_finite(tab)) {
1827 struct isl_vec *sample;
1829 sample = isl_tab_get_sample_value(tab);
1831 if (isl_tab_add_sample(tab, sample) < 0)
1832 goto error;
1835 if (!tab->empty && isl_tab_rollback(tab, snap) < 0)
1836 goto error;
1838 return tab;
1839 error:
1840 isl_tab_free(tab);
1841 return NULL;
1844 /* Check if any of the currently active sample values satisfies
1845 * the inequality "ineq" (an equality if eq is set).
1847 static int tab_has_valid_sample(struct isl_tab *tab, isl_int *ineq, int eq)
1849 int i;
1850 isl_int v;
1852 if (!tab)
1853 return -1;
1855 isl_assert(tab->mat->ctx, tab->bmap, return -1);
1856 isl_assert(tab->mat->ctx, tab->samples, return -1);
1857 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, return -1);
1859 isl_int_init(v);
1860 for (i = tab->n_outside; i < tab->n_sample; ++i) {
1861 int sgn;
1862 isl_seq_inner_product(ineq, tab->samples->row[i],
1863 1 + tab->n_var, &v);
1864 sgn = isl_int_sgn(v);
1865 if (eq ? (sgn == 0) : (sgn >= 0))
1866 break;
1868 isl_int_clear(v);
1870 return i < tab->n_sample;
1873 /* Insert a div specified by "div" to the tableau "tab" at position "pos" and
1874 * return isl_bool_true if the div is obviously non-negative.
1876 static isl_bool context_tab_insert_div(struct isl_tab *tab, int pos,
1877 __isl_keep isl_vec *div,
1878 int (*add_ineq)(void *user, isl_int *), void *user)
1880 int i;
1881 int r;
1882 struct isl_mat *samples;
1883 int nonneg;
1885 r = isl_tab_insert_div(tab, pos, div, add_ineq, user);
1886 if (r < 0)
1887 return isl_bool_error;
1888 nonneg = tab->var[r].is_nonneg;
1889 tab->var[r].frozen = 1;
1891 samples = isl_mat_extend(tab->samples,
1892 tab->n_sample, 1 + tab->n_var);
1893 tab->samples = samples;
1894 if (!samples)
1895 return isl_bool_error;
1896 for (i = tab->n_outside; i < samples->n_row; ++i) {
1897 isl_seq_inner_product(div->el + 1, samples->row[i],
1898 div->size - 1, &samples->row[i][samples->n_col - 1]);
1899 isl_int_fdiv_q(samples->row[i][samples->n_col - 1],
1900 samples->row[i][samples->n_col - 1], div->el[0]);
1902 tab->samples = isl_mat_move_cols(tab->samples, 1 + pos,
1903 1 + tab->n_var - 1, 1);
1904 if (!tab->samples)
1905 return isl_bool_error;
1907 return nonneg;
1910 /* Add a div specified by "div" to both the main tableau and
1911 * the context tableau. In case of the main tableau, we only
1912 * need to add an extra div. In the context tableau, we also
1913 * need to express the meaning of the div.
1914 * Return the index of the div or -1 if anything went wrong.
1916 * The new integer division is added before any unknown integer
1917 * divisions in the context to ensure that it does not get
1918 * equated to some linear combination involving unknown integer
1919 * divisions.
1921 static int add_div(struct isl_tab *tab, struct isl_context *context,
1922 __isl_keep isl_vec *div)
1924 int r;
1925 int pos;
1926 isl_bool nonneg;
1927 struct isl_tab *context_tab = context->op->peek_tab(context);
1929 if (!tab || !context_tab)
1930 goto error;
1932 pos = context_tab->n_var - context->n_unknown;
1933 if ((nonneg = context->op->insert_div(context, pos, div)) < 0)
1934 goto error;
1936 if (!context->op->is_ok(context))
1937 goto error;
1939 pos = tab->n_var - context->n_unknown;
1940 if (isl_tab_extend_vars(tab, 1) < 0)
1941 goto error;
1942 r = isl_tab_insert_var(tab, pos);
1943 if (r < 0)
1944 goto error;
1945 if (nonneg)
1946 tab->var[r].is_nonneg = 1;
1947 tab->var[r].frozen = 1;
1948 tab->n_div++;
1950 return tab->n_div - 1 - context->n_unknown;
1951 error:
1952 context->op->invalidate(context);
1953 return -1;
1956 static int find_div(struct isl_tab *tab, isl_int *div, isl_int denom)
1958 int i;
1959 unsigned total = isl_basic_map_total_dim(tab->bmap);
1961 for (i = 0; i < tab->bmap->n_div; ++i) {
1962 if (isl_int_ne(tab->bmap->div[i][0], denom))
1963 continue;
1964 if (!isl_seq_eq(tab->bmap->div[i] + 1, div, 1 + total))
1965 continue;
1966 return i;
1968 return -1;
1971 /* Return the index of a div that corresponds to "div".
1972 * We first check if we already have such a div and if not, we create one.
1974 static int get_div(struct isl_tab *tab, struct isl_context *context,
1975 struct isl_vec *div)
1977 int d;
1978 struct isl_tab *context_tab = context->op->peek_tab(context);
1980 if (!context_tab)
1981 return -1;
1983 d = find_div(context_tab, div->el + 1, div->el[0]);
1984 if (d != -1)
1985 return d;
1987 return add_div(tab, context, div);
1990 /* Add a parametric cut to cut away the non-integral sample value
1991 * of the give row.
1992 * Let a_i be the coefficients of the constant term and the parameters
1993 * and let b_i be the coefficients of the variables or constraints
1994 * in basis of the tableau.
1995 * Let q be the div q = floor(\sum_i {-a_i} y_i).
1997 * The cut is expressed as
1999 * c = \sum_i -{-a_i} y_i + \sum_i {b_i} x_i + q >= 0
2001 * If q did not already exist in the context tableau, then it is added first.
2002 * If q is in a column of the main tableau then the "+ q" can be accomplished
2003 * by setting the corresponding entry to the denominator of the constraint.
2004 * If q happens to be in a row of the main tableau, then the corresponding
2005 * row needs to be added instead (taking care of the denominators).
2006 * Note that this is very unlikely, but perhaps not entirely impossible.
2008 * The current value of the cut is known to be negative (or at least
2009 * non-positive), so row_sign is set accordingly.
2011 * Return the row of the cut or -1.
2013 static int add_parametric_cut(struct isl_tab *tab, int row,
2014 struct isl_context *context)
2016 struct isl_vec *div;
2017 int d;
2018 int i;
2019 int r;
2020 isl_int *r_row;
2021 int col;
2022 int n;
2023 unsigned off = 2 + tab->M;
2025 if (!context)
2026 return -1;
2028 div = get_row_parameter_div(tab, row);
2029 if (!div)
2030 return -1;
2032 n = tab->n_div - context->n_unknown;
2033 d = context->op->get_div(context, tab, div);
2034 isl_vec_free(div);
2035 if (d < 0)
2036 return -1;
2038 if (isl_tab_extend_cons(tab, 1) < 0)
2039 return -1;
2040 r = isl_tab_allocate_con(tab);
2041 if (r < 0)
2042 return -1;
2044 r_row = tab->mat->row[tab->con[r].index];
2045 isl_int_set(r_row[0], tab->mat->row[row][0]);
2046 isl_int_neg(r_row[1], tab->mat->row[row][1]);
2047 isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
2048 isl_int_neg(r_row[1], r_row[1]);
2049 if (tab->M)
2050 isl_int_set_si(r_row[2], 0);
2051 for (i = 0; i < tab->n_param; ++i) {
2052 if (tab->var[i].is_row)
2053 continue;
2054 col = tab->var[i].index;
2055 isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
2056 isl_int_fdiv_r(r_row[off + col], r_row[off + col],
2057 tab->mat->row[row][0]);
2058 isl_int_neg(r_row[off + col], r_row[off + col]);
2060 for (i = 0; i < tab->n_div; ++i) {
2061 if (tab->var[tab->n_var - tab->n_div + i].is_row)
2062 continue;
2063 col = tab->var[tab->n_var - tab->n_div + i].index;
2064 isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
2065 isl_int_fdiv_r(r_row[off + col], r_row[off + col],
2066 tab->mat->row[row][0]);
2067 isl_int_neg(r_row[off + col], r_row[off + col]);
2069 for (i = 0; i < tab->n_col; ++i) {
2070 if (tab->col_var[i] >= 0 &&
2071 (tab->col_var[i] < tab->n_param ||
2072 tab->col_var[i] >= tab->n_var - tab->n_div))
2073 continue;
2074 isl_int_fdiv_r(r_row[off + i],
2075 tab->mat->row[row][off + i], tab->mat->row[row][0]);
2077 if (tab->var[tab->n_var - tab->n_div + d].is_row) {
2078 isl_int gcd;
2079 int d_row = tab->var[tab->n_var - tab->n_div + d].index;
2080 isl_int_init(gcd);
2081 isl_int_gcd(gcd, tab->mat->row[d_row][0], r_row[0]);
2082 isl_int_divexact(r_row[0], r_row[0], gcd);
2083 isl_int_divexact(gcd, tab->mat->row[d_row][0], gcd);
2084 isl_seq_combine(r_row + 1, gcd, r_row + 1,
2085 r_row[0], tab->mat->row[d_row] + 1,
2086 off - 1 + tab->n_col);
2087 isl_int_mul(r_row[0], r_row[0], tab->mat->row[d_row][0]);
2088 isl_int_clear(gcd);
2089 } else {
2090 col = tab->var[tab->n_var - tab->n_div + d].index;
2091 isl_int_set(r_row[off + col], tab->mat->row[row][0]);
2094 tab->con[r].is_nonneg = 1;
2095 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
2096 return -1;
2097 if (tab->row_sign)
2098 tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
2100 row = tab->con[r].index;
2102 if (d >= n && context->op->detect_equalities(context, tab) < 0)
2103 return -1;
2105 return row;
2108 /* Construct a tableau for bmap that can be used for computing
2109 * the lexicographic minimum (or maximum) of bmap.
2110 * If not NULL, then dom is the domain where the minimum
2111 * should be computed. In this case, we set up a parametric
2112 * tableau with row signs (initialized to "unknown").
2113 * If M is set, then the tableau will use a big parameter.
2114 * If max is set, then a maximum should be computed instead of a minimum.
2115 * This means that for each variable x, the tableau will contain the variable
2116 * x' = M - x, rather than x' = M + x. This in turn means that the coefficient
2117 * of the variables in all constraints are negated prior to adding them
2118 * to the tableau.
2120 static struct isl_tab *tab_for_lexmin(struct isl_basic_map *bmap,
2121 struct isl_basic_set *dom, unsigned M, int max)
2123 int i;
2124 struct isl_tab *tab;
2125 unsigned n_var;
2126 unsigned o_var;
2128 tab = isl_tab_alloc(bmap->ctx, 2 * bmap->n_eq + bmap->n_ineq + 1,
2129 isl_basic_map_total_dim(bmap), M);
2130 if (!tab)
2131 return NULL;
2133 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
2134 if (dom) {
2135 tab->n_param = isl_basic_set_total_dim(dom) - dom->n_div;
2136 tab->n_div = dom->n_div;
2137 tab->row_sign = isl_calloc_array(bmap->ctx,
2138 enum isl_tab_row_sign, tab->mat->n_row);
2139 if (tab->mat->n_row && !tab->row_sign)
2140 goto error;
2142 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
2143 if (isl_tab_mark_empty(tab) < 0)
2144 goto error;
2145 return tab;
2148 for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
2149 tab->var[i].is_nonneg = 1;
2150 tab->var[i].frozen = 1;
2152 o_var = 1 + tab->n_param;
2153 n_var = tab->n_var - tab->n_param - tab->n_div;
2154 for (i = 0; i < bmap->n_eq; ++i) {
2155 if (max)
2156 isl_seq_neg(bmap->eq[i] + o_var,
2157 bmap->eq[i] + o_var, n_var);
2158 tab = add_lexmin_valid_eq(tab, bmap->eq[i]);
2159 if (max)
2160 isl_seq_neg(bmap->eq[i] + o_var,
2161 bmap->eq[i] + o_var, n_var);
2162 if (!tab || tab->empty)
2163 return tab;
2165 if (bmap->n_eq && restore_lexmin(tab) < 0)
2166 goto error;
2167 for (i = 0; i < bmap->n_ineq; ++i) {
2168 if (max)
2169 isl_seq_neg(bmap->ineq[i] + o_var,
2170 bmap->ineq[i] + o_var, n_var);
2171 tab = add_lexmin_ineq(tab, bmap->ineq[i]);
2172 if (max)
2173 isl_seq_neg(bmap->ineq[i] + o_var,
2174 bmap->ineq[i] + o_var, n_var);
2175 if (!tab || tab->empty)
2176 return tab;
2178 return tab;
2179 error:
2180 isl_tab_free(tab);
2181 return NULL;
2184 /* Given a main tableau where more than one row requires a split,
2185 * determine and return the "best" row to split on.
2187 * Given two rows in the main tableau, if the inequality corresponding
2188 * to the first row is redundant with respect to that of the second row
2189 * in the current tableau, then it is better to split on the second row,
2190 * since in the positive part, both rows will be positive.
2191 * (In the negative part a pivot will have to be performed and just about
2192 * anything can happen to the sign of the other row.)
2194 * As a simple heuristic, we therefore select the row that makes the most
2195 * of the other rows redundant.
2197 * Perhaps it would also be useful to look at the number of constraints
2198 * that conflict with any given constraint.
2200 * best is the best row so far (-1 when we have not found any row yet).
2201 * best_r is the number of other rows made redundant by row best.
2202 * When best is still -1, bset_r is meaningless, but it is initialized
2203 * to some arbitrary value (0) anyway. Without this redundant initialization
2204 * valgrind may warn about uninitialized memory accesses when isl
2205 * is compiled with some versions of gcc.
2207 static int best_split(struct isl_tab *tab, struct isl_tab *context_tab)
2209 struct isl_tab_undo *snap;
2210 int split;
2211 int row;
2212 int best = -1;
2213 int best_r = 0;
2215 if (isl_tab_extend_cons(context_tab, 2) < 0)
2216 return -1;
2218 snap = isl_tab_snap(context_tab);
2220 for (split = tab->n_redundant; split < tab->n_row; ++split) {
2221 struct isl_tab_undo *snap2;
2222 struct isl_vec *ineq = NULL;
2223 int r = 0;
2224 int ok;
2226 if (!isl_tab_var_from_row(tab, split)->is_nonneg)
2227 continue;
2228 if (tab->row_sign[split] != isl_tab_row_any)
2229 continue;
2231 ineq = get_row_parameter_ineq(tab, split);
2232 if (!ineq)
2233 return -1;
2234 ok = isl_tab_add_ineq(context_tab, ineq->el) >= 0;
2235 isl_vec_free(ineq);
2236 if (!ok)
2237 return -1;
2239 snap2 = isl_tab_snap(context_tab);
2241 for (row = tab->n_redundant; row < tab->n_row; ++row) {
2242 struct isl_tab_var *var;
2244 if (row == split)
2245 continue;
2246 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
2247 continue;
2248 if (tab->row_sign[row] != isl_tab_row_any)
2249 continue;
2251 ineq = get_row_parameter_ineq(tab, row);
2252 if (!ineq)
2253 return -1;
2254 ok = isl_tab_add_ineq(context_tab, ineq->el) >= 0;
2255 isl_vec_free(ineq);
2256 if (!ok)
2257 return -1;
2258 var = &context_tab->con[context_tab->n_con - 1];
2259 if (!context_tab->empty &&
2260 !isl_tab_min_at_most_neg_one(context_tab, var))
2261 r++;
2262 if (isl_tab_rollback(context_tab, snap2) < 0)
2263 return -1;
2265 if (best == -1 || r > best_r) {
2266 best = split;
2267 best_r = r;
2269 if (isl_tab_rollback(context_tab, snap) < 0)
2270 return -1;
2273 return best;
2276 static struct isl_basic_set *context_lex_peek_basic_set(
2277 struct isl_context *context)
2279 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2280 if (!clex->tab)
2281 return NULL;
2282 return isl_tab_peek_bset(clex->tab);
2285 static struct isl_tab *context_lex_peek_tab(struct isl_context *context)
2287 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2288 return clex->tab;
2291 static void context_lex_add_eq(struct isl_context *context, isl_int *eq,
2292 int check, int update)
2294 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2295 if (isl_tab_extend_cons(clex->tab, 2) < 0)
2296 goto error;
2297 if (add_lexmin_eq(clex->tab, eq) < 0)
2298 goto error;
2299 if (check) {
2300 int v = tab_has_valid_sample(clex->tab, eq, 1);
2301 if (v < 0)
2302 goto error;
2303 if (!v)
2304 clex->tab = check_integer_feasible(clex->tab);
2306 if (update)
2307 clex->tab = check_samples(clex->tab, eq, 1);
2308 return;
2309 error:
2310 isl_tab_free(clex->tab);
2311 clex->tab = NULL;
2314 static void context_lex_add_ineq(struct isl_context *context, isl_int *ineq,
2315 int check, int update)
2317 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2318 if (isl_tab_extend_cons(clex->tab, 1) < 0)
2319 goto error;
2320 clex->tab = add_lexmin_ineq(clex->tab, ineq);
2321 if (check) {
2322 int v = tab_has_valid_sample(clex->tab, ineq, 0);
2323 if (v < 0)
2324 goto error;
2325 if (!v)
2326 clex->tab = check_integer_feasible(clex->tab);
2328 if (update)
2329 clex->tab = check_samples(clex->tab, ineq, 0);
2330 return;
2331 error:
2332 isl_tab_free(clex->tab);
2333 clex->tab = NULL;
2336 static int context_lex_add_ineq_wrap(void *user, isl_int *ineq)
2338 struct isl_context *context = (struct isl_context *)user;
2339 context_lex_add_ineq(context, ineq, 0, 0);
2340 return context->op->is_ok(context) ? 0 : -1;
2343 /* Check which signs can be obtained by "ineq" on all the currently
2344 * active sample values. See row_sign for more information.
2346 static enum isl_tab_row_sign tab_ineq_sign(struct isl_tab *tab, isl_int *ineq,
2347 int strict)
2349 int i;
2350 int sgn;
2351 isl_int tmp;
2352 enum isl_tab_row_sign res = isl_tab_row_unknown;
2354 isl_assert(tab->mat->ctx, tab->samples, return isl_tab_row_unknown);
2355 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var,
2356 return isl_tab_row_unknown);
2358 isl_int_init(tmp);
2359 for (i = tab->n_outside; i < tab->n_sample; ++i) {
2360 isl_seq_inner_product(tab->samples->row[i], ineq,
2361 1 + tab->n_var, &tmp);
2362 sgn = isl_int_sgn(tmp);
2363 if (sgn > 0 || (sgn == 0 && strict)) {
2364 if (res == isl_tab_row_unknown)
2365 res = isl_tab_row_pos;
2366 if (res == isl_tab_row_neg)
2367 res = isl_tab_row_any;
2369 if (sgn < 0) {
2370 if (res == isl_tab_row_unknown)
2371 res = isl_tab_row_neg;
2372 if (res == isl_tab_row_pos)
2373 res = isl_tab_row_any;
2375 if (res == isl_tab_row_any)
2376 break;
2378 isl_int_clear(tmp);
2380 return res;
2383 static enum isl_tab_row_sign context_lex_ineq_sign(struct isl_context *context,
2384 isl_int *ineq, int strict)
2386 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2387 return tab_ineq_sign(clex->tab, ineq, strict);
2390 /* Check whether "ineq" can be added to the tableau without rendering
2391 * it infeasible.
2393 static int context_lex_test_ineq(struct isl_context *context, isl_int *ineq)
2395 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2396 struct isl_tab_undo *snap;
2397 int feasible;
2399 if (!clex->tab)
2400 return -1;
2402 if (isl_tab_extend_cons(clex->tab, 1) < 0)
2403 return -1;
2405 snap = isl_tab_snap(clex->tab);
2406 if (isl_tab_push_basis(clex->tab) < 0)
2407 return -1;
2408 clex->tab = add_lexmin_ineq(clex->tab, ineq);
2409 clex->tab = check_integer_feasible(clex->tab);
2410 if (!clex->tab)
2411 return -1;
2412 feasible = !clex->tab->empty;
2413 if (isl_tab_rollback(clex->tab, snap) < 0)
2414 return -1;
2416 return feasible;
2419 static int context_lex_get_div(struct isl_context *context, struct isl_tab *tab,
2420 struct isl_vec *div)
2422 return get_div(tab, context, div);
2425 /* Insert a div specified by "div" to the context tableau at position "pos" and
2426 * return isl_bool_true if the div is obviously non-negative.
2427 * context_tab_add_div will always return isl_bool_true, because all variables
2428 * in a isl_context_lex tableau are non-negative.
2429 * However, if we are using a big parameter in the context, then this only
2430 * reflects the non-negativity of the variable used to _encode_ the
2431 * div, i.e., div' = M + div, so we can't draw any conclusions.
2433 static isl_bool context_lex_insert_div(struct isl_context *context, int pos,
2434 __isl_keep isl_vec *div)
2436 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2437 isl_bool nonneg;
2438 nonneg = context_tab_insert_div(clex->tab, pos, div,
2439 context_lex_add_ineq_wrap, context);
2440 if (nonneg < 0)
2441 return isl_bool_error;
2442 if (clex->tab->M)
2443 return isl_bool_false;
2444 return nonneg;
2447 static int context_lex_detect_equalities(struct isl_context *context,
2448 struct isl_tab *tab)
2450 return 0;
2453 static int context_lex_best_split(struct isl_context *context,
2454 struct isl_tab *tab)
2456 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2457 struct isl_tab_undo *snap;
2458 int r;
2460 snap = isl_tab_snap(clex->tab);
2461 if (isl_tab_push_basis(clex->tab) < 0)
2462 return -1;
2463 r = best_split(tab, clex->tab);
2465 if (r >= 0 && isl_tab_rollback(clex->tab, snap) < 0)
2466 return -1;
2468 return r;
2471 static int context_lex_is_empty(struct isl_context *context)
2473 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2474 if (!clex->tab)
2475 return -1;
2476 return clex->tab->empty;
2479 static void *context_lex_save(struct isl_context *context)
2481 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2482 struct isl_tab_undo *snap;
2484 snap = isl_tab_snap(clex->tab);
2485 if (isl_tab_push_basis(clex->tab) < 0)
2486 return NULL;
2487 if (isl_tab_save_samples(clex->tab) < 0)
2488 return NULL;
2490 return snap;
2493 static void context_lex_restore(struct isl_context *context, void *save)
2495 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2496 if (isl_tab_rollback(clex->tab, (struct isl_tab_undo *)save) < 0) {
2497 isl_tab_free(clex->tab);
2498 clex->tab = NULL;
2502 static void context_lex_discard(void *save)
2506 static int context_lex_is_ok(struct isl_context *context)
2508 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2509 return !!clex->tab;
2512 /* For each variable in the context tableau, check if the variable can
2513 * only attain non-negative values. If so, mark the parameter as non-negative
2514 * in the main tableau. This allows for a more direct identification of some
2515 * cases of violated constraints.
2517 static struct isl_tab *tab_detect_nonnegative_parameters(struct isl_tab *tab,
2518 struct isl_tab *context_tab)
2520 int i;
2521 struct isl_tab_undo *snap;
2522 struct isl_vec *ineq = NULL;
2523 struct isl_tab_var *var;
2524 int n;
2526 if (context_tab->n_var == 0)
2527 return tab;
2529 ineq = isl_vec_alloc(tab->mat->ctx, 1 + context_tab->n_var);
2530 if (!ineq)
2531 goto error;
2533 if (isl_tab_extend_cons(context_tab, 1) < 0)
2534 goto error;
2536 snap = isl_tab_snap(context_tab);
2538 n = 0;
2539 isl_seq_clr(ineq->el, ineq->size);
2540 for (i = 0; i < context_tab->n_var; ++i) {
2541 isl_int_set_si(ineq->el[1 + i], 1);
2542 if (isl_tab_add_ineq(context_tab, ineq->el) < 0)
2543 goto error;
2544 var = &context_tab->con[context_tab->n_con - 1];
2545 if (!context_tab->empty &&
2546 !isl_tab_min_at_most_neg_one(context_tab, var)) {
2547 int j = i;
2548 if (i >= tab->n_param)
2549 j = i - tab->n_param + tab->n_var - tab->n_div;
2550 tab->var[j].is_nonneg = 1;
2551 n++;
2553 isl_int_set_si(ineq->el[1 + i], 0);
2554 if (isl_tab_rollback(context_tab, snap) < 0)
2555 goto error;
2558 if (context_tab->M && n == context_tab->n_var) {
2559 context_tab->mat = isl_mat_drop_cols(context_tab->mat, 2, 1);
2560 context_tab->M = 0;
2563 isl_vec_free(ineq);
2564 return tab;
2565 error:
2566 isl_vec_free(ineq);
2567 isl_tab_free(tab);
2568 return NULL;
2571 static struct isl_tab *context_lex_detect_nonnegative_parameters(
2572 struct isl_context *context, struct isl_tab *tab)
2574 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2575 struct isl_tab_undo *snap;
2577 if (!tab)
2578 return NULL;
2580 snap = isl_tab_snap(clex->tab);
2581 if (isl_tab_push_basis(clex->tab) < 0)
2582 goto error;
2584 tab = tab_detect_nonnegative_parameters(tab, clex->tab);
2586 if (isl_tab_rollback(clex->tab, snap) < 0)
2587 goto error;
2589 return tab;
2590 error:
2591 isl_tab_free(tab);
2592 return NULL;
2595 static void context_lex_invalidate(struct isl_context *context)
2597 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2598 isl_tab_free(clex->tab);
2599 clex->tab = NULL;
2602 static __isl_null struct isl_context *context_lex_free(
2603 struct isl_context *context)
2605 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2606 isl_tab_free(clex->tab);
2607 free(clex);
2609 return NULL;
2612 struct isl_context_op isl_context_lex_op = {
2613 context_lex_detect_nonnegative_parameters,
2614 context_lex_peek_basic_set,
2615 context_lex_peek_tab,
2616 context_lex_add_eq,
2617 context_lex_add_ineq,
2618 context_lex_ineq_sign,
2619 context_lex_test_ineq,
2620 context_lex_get_div,
2621 context_lex_insert_div,
2622 context_lex_detect_equalities,
2623 context_lex_best_split,
2624 context_lex_is_empty,
2625 context_lex_is_ok,
2626 context_lex_save,
2627 context_lex_restore,
2628 context_lex_discard,
2629 context_lex_invalidate,
2630 context_lex_free,
2633 static struct isl_tab *context_tab_for_lexmin(struct isl_basic_set *bset)
2635 struct isl_tab *tab;
2637 if (!bset)
2638 return NULL;
2639 tab = tab_for_lexmin(bset_to_bmap(bset), NULL, 1, 0);
2640 if (!tab)
2641 goto error;
2642 if (isl_tab_track_bset(tab, bset) < 0)
2643 goto error;
2644 tab = isl_tab_init_samples(tab);
2645 return tab;
2646 error:
2647 isl_basic_set_free(bset);
2648 return NULL;
2651 static struct isl_context *isl_context_lex_alloc(struct isl_basic_set *dom)
2653 struct isl_context_lex *clex;
2655 if (!dom)
2656 return NULL;
2658 clex = isl_alloc_type(dom->ctx, struct isl_context_lex);
2659 if (!clex)
2660 return NULL;
2662 clex->context.op = &isl_context_lex_op;
2664 clex->tab = context_tab_for_lexmin(isl_basic_set_copy(dom));
2665 if (restore_lexmin(clex->tab) < 0)
2666 goto error;
2667 clex->tab = check_integer_feasible(clex->tab);
2668 if (!clex->tab)
2669 goto error;
2671 return &clex->context;
2672 error:
2673 clex->context.op->free(&clex->context);
2674 return NULL;
2677 /* Representation of the context when using generalized basis reduction.
2679 * "shifted" contains the offsets of the unit hypercubes that lie inside the
2680 * context. Any rational point in "shifted" can therefore be rounded
2681 * up to an integer point in the context.
2682 * If the context is constrained by any equality, then "shifted" is not used
2683 * as it would be empty.
2685 struct isl_context_gbr {
2686 struct isl_context context;
2687 struct isl_tab *tab;
2688 struct isl_tab *shifted;
2689 struct isl_tab *cone;
2692 static struct isl_tab *context_gbr_detect_nonnegative_parameters(
2693 struct isl_context *context, struct isl_tab *tab)
2695 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2696 if (!tab)
2697 return NULL;
2698 return tab_detect_nonnegative_parameters(tab, cgbr->tab);
2701 static struct isl_basic_set *context_gbr_peek_basic_set(
2702 struct isl_context *context)
2704 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2705 if (!cgbr->tab)
2706 return NULL;
2707 return isl_tab_peek_bset(cgbr->tab);
2710 static struct isl_tab *context_gbr_peek_tab(struct isl_context *context)
2712 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2713 return cgbr->tab;
2716 /* Initialize the "shifted" tableau of the context, which
2717 * contains the constraints of the original tableau shifted
2718 * by the sum of all negative coefficients. This ensures
2719 * that any rational point in the shifted tableau can
2720 * be rounded up to yield an integer point in the original tableau.
2722 static void gbr_init_shifted(struct isl_context_gbr *cgbr)
2724 int i, j;
2725 struct isl_vec *cst;
2726 struct isl_basic_set *bset = isl_tab_peek_bset(cgbr->tab);
2727 unsigned dim = isl_basic_set_total_dim(bset);
2729 cst = isl_vec_alloc(cgbr->tab->mat->ctx, bset->n_ineq);
2730 if (!cst)
2731 return;
2733 for (i = 0; i < bset->n_ineq; ++i) {
2734 isl_int_set(cst->el[i], bset->ineq[i][0]);
2735 for (j = 0; j < dim; ++j) {
2736 if (!isl_int_is_neg(bset->ineq[i][1 + j]))
2737 continue;
2738 isl_int_add(bset->ineq[i][0], bset->ineq[i][0],
2739 bset->ineq[i][1 + j]);
2743 cgbr->shifted = isl_tab_from_basic_set(bset, 0);
2745 for (i = 0; i < bset->n_ineq; ++i)
2746 isl_int_set(bset->ineq[i][0], cst->el[i]);
2748 isl_vec_free(cst);
2751 /* Check if the shifted tableau is non-empty, and if so
2752 * use the sample point to construct an integer point
2753 * of the context tableau.
2755 static struct isl_vec *gbr_get_shifted_sample(struct isl_context_gbr *cgbr)
2757 struct isl_vec *sample;
2759 if (!cgbr->shifted)
2760 gbr_init_shifted(cgbr);
2761 if (!cgbr->shifted)
2762 return NULL;
2763 if (cgbr->shifted->empty)
2764 return isl_vec_alloc(cgbr->tab->mat->ctx, 0);
2766 sample = isl_tab_get_sample_value(cgbr->shifted);
2767 sample = isl_vec_ceil(sample);
2769 return sample;
2772 static struct isl_basic_set *drop_constant_terms(struct isl_basic_set *bset)
2774 int i;
2776 if (!bset)
2777 return NULL;
2779 for (i = 0; i < bset->n_eq; ++i)
2780 isl_int_set_si(bset->eq[i][0], 0);
2782 for (i = 0; i < bset->n_ineq; ++i)
2783 isl_int_set_si(bset->ineq[i][0], 0);
2785 return bset;
2788 static int use_shifted(struct isl_context_gbr *cgbr)
2790 if (!cgbr->tab)
2791 return 0;
2792 return cgbr->tab->bmap->n_eq == 0 && cgbr->tab->bmap->n_div == 0;
2795 static struct isl_vec *gbr_get_sample(struct isl_context_gbr *cgbr)
2797 struct isl_basic_set *bset;
2798 struct isl_basic_set *cone;
2800 if (isl_tab_sample_is_integer(cgbr->tab))
2801 return isl_tab_get_sample_value(cgbr->tab);
2803 if (use_shifted(cgbr)) {
2804 struct isl_vec *sample;
2806 sample = gbr_get_shifted_sample(cgbr);
2807 if (!sample || sample->size > 0)
2808 return sample;
2810 isl_vec_free(sample);
2813 if (!cgbr->cone) {
2814 bset = isl_tab_peek_bset(cgbr->tab);
2815 cgbr->cone = isl_tab_from_recession_cone(bset, 0);
2816 if (!cgbr->cone)
2817 return NULL;
2818 if (isl_tab_track_bset(cgbr->cone,
2819 isl_basic_set_copy(bset)) < 0)
2820 return NULL;
2822 if (isl_tab_detect_implicit_equalities(cgbr->cone) < 0)
2823 return NULL;
2825 if (cgbr->cone->n_dead == cgbr->cone->n_col) {
2826 struct isl_vec *sample;
2827 struct isl_tab_undo *snap;
2829 if (cgbr->tab->basis) {
2830 if (cgbr->tab->basis->n_col != 1 + cgbr->tab->n_var) {
2831 isl_mat_free(cgbr->tab->basis);
2832 cgbr->tab->basis = NULL;
2834 cgbr->tab->n_zero = 0;
2835 cgbr->tab->n_unbounded = 0;
2838 snap = isl_tab_snap(cgbr->tab);
2840 sample = isl_tab_sample(cgbr->tab);
2842 if (!sample || isl_tab_rollback(cgbr->tab, snap) < 0) {
2843 isl_vec_free(sample);
2844 return NULL;
2847 return sample;
2850 cone = isl_basic_set_dup(isl_tab_peek_bset(cgbr->cone));
2851 cone = drop_constant_terms(cone);
2852 cone = isl_basic_set_update_from_tab(cone, cgbr->cone);
2853 cone = isl_basic_set_underlying_set(cone);
2854 cone = isl_basic_set_gauss(cone, NULL);
2856 bset = isl_basic_set_dup(isl_tab_peek_bset(cgbr->tab));
2857 bset = isl_basic_set_update_from_tab(bset, cgbr->tab);
2858 bset = isl_basic_set_underlying_set(bset);
2859 bset = isl_basic_set_gauss(bset, NULL);
2861 return isl_basic_set_sample_with_cone(bset, cone);
2864 static void check_gbr_integer_feasible(struct isl_context_gbr *cgbr)
2866 struct isl_vec *sample;
2868 if (!cgbr->tab)
2869 return;
2871 if (cgbr->tab->empty)
2872 return;
2874 sample = gbr_get_sample(cgbr);
2875 if (!sample)
2876 goto error;
2878 if (sample->size == 0) {
2879 isl_vec_free(sample);
2880 if (isl_tab_mark_empty(cgbr->tab) < 0)
2881 goto error;
2882 return;
2885 if (isl_tab_add_sample(cgbr->tab, sample) < 0)
2886 goto error;
2888 return;
2889 error:
2890 isl_tab_free(cgbr->tab);
2891 cgbr->tab = NULL;
2894 static struct isl_tab *add_gbr_eq(struct isl_tab *tab, isl_int *eq)
2896 if (!tab)
2897 return NULL;
2899 if (isl_tab_extend_cons(tab, 2) < 0)
2900 goto error;
2902 if (isl_tab_add_eq(tab, eq) < 0)
2903 goto error;
2905 return tab;
2906 error:
2907 isl_tab_free(tab);
2908 return NULL;
2911 /* Add the equality described by "eq" to the context.
2912 * If "check" is set, then we check if the context is empty after
2913 * adding the equality.
2914 * If "update" is set, then we check if the samples are still valid.
2916 * We do not explicitly add shifted copies of the equality to
2917 * cgbr->shifted since they would conflict with each other.
2918 * Instead, we directly mark cgbr->shifted empty.
2920 static void context_gbr_add_eq(struct isl_context *context, isl_int *eq,
2921 int check, int update)
2923 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2925 cgbr->tab = add_gbr_eq(cgbr->tab, eq);
2927 if (cgbr->shifted && !cgbr->shifted->empty && use_shifted(cgbr)) {
2928 if (isl_tab_mark_empty(cgbr->shifted) < 0)
2929 goto error;
2932 if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
2933 if (isl_tab_extend_cons(cgbr->cone, 2) < 0)
2934 goto error;
2935 if (isl_tab_add_eq(cgbr->cone, eq) < 0)
2936 goto error;
2939 if (check) {
2940 int v = tab_has_valid_sample(cgbr->tab, eq, 1);
2941 if (v < 0)
2942 goto error;
2943 if (!v)
2944 check_gbr_integer_feasible(cgbr);
2946 if (update)
2947 cgbr->tab = check_samples(cgbr->tab, eq, 1);
2948 return;
2949 error:
2950 isl_tab_free(cgbr->tab);
2951 cgbr->tab = NULL;
2954 static void add_gbr_ineq(struct isl_context_gbr *cgbr, isl_int *ineq)
2956 if (!cgbr->tab)
2957 return;
2959 if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
2960 goto error;
2962 if (isl_tab_add_ineq(cgbr->tab, ineq) < 0)
2963 goto error;
2965 if (cgbr->shifted && !cgbr->shifted->empty && use_shifted(cgbr)) {
2966 int i;
2967 unsigned dim;
2968 dim = isl_basic_map_total_dim(cgbr->tab->bmap);
2970 if (isl_tab_extend_cons(cgbr->shifted, 1) < 0)
2971 goto error;
2973 for (i = 0; i < dim; ++i) {
2974 if (!isl_int_is_neg(ineq[1 + i]))
2975 continue;
2976 isl_int_add(ineq[0], ineq[0], ineq[1 + i]);
2979 if (isl_tab_add_ineq(cgbr->shifted, ineq) < 0)
2980 goto error;
2982 for (i = 0; i < dim; ++i) {
2983 if (!isl_int_is_neg(ineq[1 + i]))
2984 continue;
2985 isl_int_sub(ineq[0], ineq[0], ineq[1 + i]);
2989 if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
2990 if (isl_tab_extend_cons(cgbr->cone, 1) < 0)
2991 goto error;
2992 if (isl_tab_add_ineq(cgbr->cone, ineq) < 0)
2993 goto error;
2996 return;
2997 error:
2998 isl_tab_free(cgbr->tab);
2999 cgbr->tab = NULL;
3002 static void context_gbr_add_ineq(struct isl_context *context, isl_int *ineq,
3003 int check, int update)
3005 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3007 add_gbr_ineq(cgbr, ineq);
3008 if (!cgbr->tab)
3009 return;
3011 if (check) {
3012 int v = tab_has_valid_sample(cgbr->tab, ineq, 0);
3013 if (v < 0)
3014 goto error;
3015 if (!v)
3016 check_gbr_integer_feasible(cgbr);
3018 if (update)
3019 cgbr->tab = check_samples(cgbr->tab, ineq, 0);
3020 return;
3021 error:
3022 isl_tab_free(cgbr->tab);
3023 cgbr->tab = NULL;
3026 static int context_gbr_add_ineq_wrap(void *user, isl_int *ineq)
3028 struct isl_context *context = (struct isl_context *)user;
3029 context_gbr_add_ineq(context, ineq, 0, 0);
3030 return context->op->is_ok(context) ? 0 : -1;
3033 static enum isl_tab_row_sign context_gbr_ineq_sign(struct isl_context *context,
3034 isl_int *ineq, int strict)
3036 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3037 return tab_ineq_sign(cgbr->tab, ineq, strict);
3040 /* Check whether "ineq" can be added to the tableau without rendering
3041 * it infeasible.
3043 static int context_gbr_test_ineq(struct isl_context *context, isl_int *ineq)
3045 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3046 struct isl_tab_undo *snap;
3047 struct isl_tab_undo *shifted_snap = NULL;
3048 struct isl_tab_undo *cone_snap = NULL;
3049 int feasible;
3051 if (!cgbr->tab)
3052 return -1;
3054 if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
3055 return -1;
3057 snap = isl_tab_snap(cgbr->tab);
3058 if (cgbr->shifted)
3059 shifted_snap = isl_tab_snap(cgbr->shifted);
3060 if (cgbr->cone)
3061 cone_snap = isl_tab_snap(cgbr->cone);
3062 add_gbr_ineq(cgbr, ineq);
3063 check_gbr_integer_feasible(cgbr);
3064 if (!cgbr->tab)
3065 return -1;
3066 feasible = !cgbr->tab->empty;
3067 if (isl_tab_rollback(cgbr->tab, snap) < 0)
3068 return -1;
3069 if (shifted_snap) {
3070 if (isl_tab_rollback(cgbr->shifted, shifted_snap))
3071 return -1;
3072 } else if (cgbr->shifted) {
3073 isl_tab_free(cgbr->shifted);
3074 cgbr->shifted = NULL;
3076 if (cone_snap) {
3077 if (isl_tab_rollback(cgbr->cone, cone_snap))
3078 return -1;
3079 } else if (cgbr->cone) {
3080 isl_tab_free(cgbr->cone);
3081 cgbr->cone = NULL;
3084 return feasible;
3087 /* Return the column of the last of the variables associated to
3088 * a column that has a non-zero coefficient.
3089 * This function is called in a context where only coefficients
3090 * of parameters or divs can be non-zero.
3092 static int last_non_zero_var_col(struct isl_tab *tab, isl_int *p)
3094 int i;
3095 int col;
3097 if (tab->n_var == 0)
3098 return -1;
3100 for (i = tab->n_var - 1; i >= 0; --i) {
3101 if (i >= tab->n_param && i < tab->n_var - tab->n_div)
3102 continue;
3103 if (tab->var[i].is_row)
3104 continue;
3105 col = tab->var[i].index;
3106 if (!isl_int_is_zero(p[col]))
3107 return col;
3110 return -1;
3113 /* Look through all the recently added equalities in the context
3114 * to see if we can propagate any of them to the main tableau.
3116 * The newly added equalities in the context are encoded as pairs
3117 * of inequalities starting at inequality "first".
3119 * We tentatively add each of these equalities to the main tableau
3120 * and if this happens to result in a row with a final coefficient
3121 * that is one or negative one, we use it to kill a column
3122 * in the main tableau. Otherwise, we discard the tentatively
3123 * added row.
3124 * This tentative addition of equality constraints turns
3125 * on the undo facility of the tableau. Turn it off again
3126 * at the end, assuming it was turned off to begin with.
3128 * Return 0 on success and -1 on failure.
3130 static int propagate_equalities(struct isl_context_gbr *cgbr,
3131 struct isl_tab *tab, unsigned first)
3133 int i;
3134 struct isl_vec *eq = NULL;
3135 isl_bool needs_undo;
3137 needs_undo = isl_tab_need_undo(tab);
3138 if (needs_undo < 0)
3139 goto error;
3140 eq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
3141 if (!eq)
3142 goto error;
3144 if (isl_tab_extend_cons(tab, (cgbr->tab->bmap->n_ineq - first)/2) < 0)
3145 goto error;
3147 isl_seq_clr(eq->el + 1 + tab->n_param,
3148 tab->n_var - tab->n_param - tab->n_div);
3149 for (i = first; i < cgbr->tab->bmap->n_ineq; i += 2) {
3150 int j;
3151 int r;
3152 struct isl_tab_undo *snap;
3153 snap = isl_tab_snap(tab);
3155 isl_seq_cpy(eq->el, cgbr->tab->bmap->ineq[i], 1 + tab->n_param);
3156 isl_seq_cpy(eq->el + 1 + tab->n_var - tab->n_div,
3157 cgbr->tab->bmap->ineq[i] + 1 + tab->n_param,
3158 tab->n_div);
3160 r = isl_tab_add_row(tab, eq->el);
3161 if (r < 0)
3162 goto error;
3163 r = tab->con[r].index;
3164 j = last_non_zero_var_col(tab, tab->mat->row[r] + 2 + tab->M);
3165 if (j < 0 || j < tab->n_dead ||
3166 !isl_int_is_one(tab->mat->row[r][0]) ||
3167 (!isl_int_is_one(tab->mat->row[r][2 + tab->M + j]) &&
3168 !isl_int_is_negone(tab->mat->row[r][2 + tab->M + j]))) {
3169 if (isl_tab_rollback(tab, snap) < 0)
3170 goto error;
3171 continue;
3173 if (isl_tab_pivot(tab, r, j) < 0)
3174 goto error;
3175 if (isl_tab_kill_col(tab, j) < 0)
3176 goto error;
3178 if (restore_lexmin(tab) < 0)
3179 goto error;
3182 if (!needs_undo)
3183 isl_tab_clear_undo(tab);
3184 isl_vec_free(eq);
3186 return 0;
3187 error:
3188 isl_vec_free(eq);
3189 isl_tab_free(cgbr->tab);
3190 cgbr->tab = NULL;
3191 return -1;
3194 static int context_gbr_detect_equalities(struct isl_context *context,
3195 struct isl_tab *tab)
3197 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3198 unsigned n_ineq;
3200 if (!cgbr->cone) {
3201 struct isl_basic_set *bset = isl_tab_peek_bset(cgbr->tab);
3202 cgbr->cone = isl_tab_from_recession_cone(bset, 0);
3203 if (!cgbr->cone)
3204 goto error;
3205 if (isl_tab_track_bset(cgbr->cone,
3206 isl_basic_set_copy(bset)) < 0)
3207 goto error;
3209 if (isl_tab_detect_implicit_equalities(cgbr->cone) < 0)
3210 goto error;
3212 n_ineq = cgbr->tab->bmap->n_ineq;
3213 cgbr->tab = isl_tab_detect_equalities(cgbr->tab, cgbr->cone);
3214 if (!cgbr->tab)
3215 return -1;
3216 if (cgbr->tab->bmap->n_ineq > n_ineq &&
3217 propagate_equalities(cgbr, tab, n_ineq) < 0)
3218 return -1;
3220 return 0;
3221 error:
3222 isl_tab_free(cgbr->tab);
3223 cgbr->tab = NULL;
3224 return -1;
3227 static int context_gbr_get_div(struct isl_context *context, struct isl_tab *tab,
3228 struct isl_vec *div)
3230 return get_div(tab, context, div);
3233 static isl_bool context_gbr_insert_div(struct isl_context *context, int pos,
3234 __isl_keep isl_vec *div)
3236 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3237 if (cgbr->cone) {
3238 int r, n_div, o_div;
3240 n_div = isl_basic_map_dim(cgbr->cone->bmap, isl_dim_div);
3241 o_div = cgbr->cone->n_var - n_div;
3243 if (isl_tab_extend_cons(cgbr->cone, 3) < 0)
3244 return isl_bool_error;
3245 if (isl_tab_extend_vars(cgbr->cone, 1) < 0)
3246 return isl_bool_error;
3247 if ((r = isl_tab_insert_var(cgbr->cone, pos)) <0)
3248 return isl_bool_error;
3250 cgbr->cone->bmap = isl_basic_map_insert_div(cgbr->cone->bmap,
3251 r - o_div, div);
3252 if (!cgbr->cone->bmap)
3253 return isl_bool_error;
3254 if (isl_tab_push_var(cgbr->cone, isl_tab_undo_bmap_div,
3255 &cgbr->cone->var[r]) < 0)
3256 return isl_bool_error;
3258 return context_tab_insert_div(cgbr->tab, pos, div,
3259 context_gbr_add_ineq_wrap, context);
3262 static int context_gbr_best_split(struct isl_context *context,
3263 struct isl_tab *tab)
3265 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3266 struct isl_tab_undo *snap;
3267 int r;
3269 snap = isl_tab_snap(cgbr->tab);
3270 r = best_split(tab, cgbr->tab);
3272 if (r >= 0 && isl_tab_rollback(cgbr->tab, snap) < 0)
3273 return -1;
3275 return r;
3278 static int context_gbr_is_empty(struct isl_context *context)
3280 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3281 if (!cgbr->tab)
3282 return -1;
3283 return cgbr->tab->empty;
3286 struct isl_gbr_tab_undo {
3287 struct isl_tab_undo *tab_snap;
3288 struct isl_tab_undo *shifted_snap;
3289 struct isl_tab_undo *cone_snap;
3292 static void *context_gbr_save(struct isl_context *context)
3294 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3295 struct isl_gbr_tab_undo *snap;
3297 if (!cgbr->tab)
3298 return NULL;
3300 snap = isl_alloc_type(cgbr->tab->mat->ctx, struct isl_gbr_tab_undo);
3301 if (!snap)
3302 return NULL;
3304 snap->tab_snap = isl_tab_snap(cgbr->tab);
3305 if (isl_tab_save_samples(cgbr->tab) < 0)
3306 goto error;
3308 if (cgbr->shifted)
3309 snap->shifted_snap = isl_tab_snap(cgbr->shifted);
3310 else
3311 snap->shifted_snap = NULL;
3313 if (cgbr->cone)
3314 snap->cone_snap = isl_tab_snap(cgbr->cone);
3315 else
3316 snap->cone_snap = NULL;
3318 return snap;
3319 error:
3320 free(snap);
3321 return NULL;
3324 static void context_gbr_restore(struct isl_context *context, void *save)
3326 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3327 struct isl_gbr_tab_undo *snap = (struct isl_gbr_tab_undo *)save;
3328 if (!snap)
3329 goto error;
3330 if (isl_tab_rollback(cgbr->tab, snap->tab_snap) < 0)
3331 goto error;
3333 if (snap->shifted_snap) {
3334 if (isl_tab_rollback(cgbr->shifted, snap->shifted_snap) < 0)
3335 goto error;
3336 } else if (cgbr->shifted) {
3337 isl_tab_free(cgbr->shifted);
3338 cgbr->shifted = NULL;
3341 if (snap->cone_snap) {
3342 if (isl_tab_rollback(cgbr->cone, snap->cone_snap) < 0)
3343 goto error;
3344 } else if (cgbr->cone) {
3345 isl_tab_free(cgbr->cone);
3346 cgbr->cone = NULL;
3349 free(snap);
3351 return;
3352 error:
3353 free(snap);
3354 isl_tab_free(cgbr->tab);
3355 cgbr->tab = NULL;
3358 static void context_gbr_discard(void *save)
3360 struct isl_gbr_tab_undo *snap = (struct isl_gbr_tab_undo *)save;
3361 free(snap);
3364 static int context_gbr_is_ok(struct isl_context *context)
3366 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3367 return !!cgbr->tab;
3370 static void context_gbr_invalidate(struct isl_context *context)
3372 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3373 isl_tab_free(cgbr->tab);
3374 cgbr->tab = NULL;
3377 static __isl_null struct isl_context *context_gbr_free(
3378 struct isl_context *context)
3380 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3381 isl_tab_free(cgbr->tab);
3382 isl_tab_free(cgbr->shifted);
3383 isl_tab_free(cgbr->cone);
3384 free(cgbr);
3386 return NULL;
3389 struct isl_context_op isl_context_gbr_op = {
3390 context_gbr_detect_nonnegative_parameters,
3391 context_gbr_peek_basic_set,
3392 context_gbr_peek_tab,
3393 context_gbr_add_eq,
3394 context_gbr_add_ineq,
3395 context_gbr_ineq_sign,
3396 context_gbr_test_ineq,
3397 context_gbr_get_div,
3398 context_gbr_insert_div,
3399 context_gbr_detect_equalities,
3400 context_gbr_best_split,
3401 context_gbr_is_empty,
3402 context_gbr_is_ok,
3403 context_gbr_save,
3404 context_gbr_restore,
3405 context_gbr_discard,
3406 context_gbr_invalidate,
3407 context_gbr_free,
3410 static struct isl_context *isl_context_gbr_alloc(__isl_keep isl_basic_set *dom)
3412 struct isl_context_gbr *cgbr;
3414 if (!dom)
3415 return NULL;
3417 cgbr = isl_calloc_type(dom->ctx, struct isl_context_gbr);
3418 if (!cgbr)
3419 return NULL;
3421 cgbr->context.op = &isl_context_gbr_op;
3423 cgbr->shifted = NULL;
3424 cgbr->cone = NULL;
3425 cgbr->tab = isl_tab_from_basic_set(dom, 1);
3426 cgbr->tab = isl_tab_init_samples(cgbr->tab);
3427 if (!cgbr->tab)
3428 goto error;
3429 check_gbr_integer_feasible(cgbr);
3431 return &cgbr->context;
3432 error:
3433 cgbr->context.op->free(&cgbr->context);
3434 return NULL;
3437 /* Allocate a context corresponding to "dom".
3438 * The representation specific fields are initialized by
3439 * isl_context_lex_alloc or isl_context_gbr_alloc.
3440 * The shared "n_unknown" field is initialized to the number
3441 * of final unknown integer divisions in "dom".
3443 static struct isl_context *isl_context_alloc(__isl_keep isl_basic_set *dom)
3445 struct isl_context *context;
3446 int first;
3448 if (!dom)
3449 return NULL;
3451 if (dom->ctx->opt->context == ISL_CONTEXT_LEXMIN)
3452 context = isl_context_lex_alloc(dom);
3453 else
3454 context = isl_context_gbr_alloc(dom);
3456 if (!context)
3457 return NULL;
3459 first = isl_basic_set_first_unknown_div(dom);
3460 if (first < 0)
3461 return context->op->free(context);
3462 context->n_unknown = isl_basic_set_dim(dom, isl_dim_div) - first;
3464 return context;
3467 /* Construct an isl_sol_map structure for accumulating the solution.
3468 * If track_empty is set, then we also keep track of the parts
3469 * of the context where there is no solution.
3470 * If max is set, then we are solving a maximization, rather than
3471 * a minimization problem, which means that the variables in the
3472 * tableau have value "M - x" rather than "M + x".
3474 static struct isl_sol *sol_map_init(struct isl_basic_map *bmap,
3475 struct isl_basic_set *dom, int track_empty, int max)
3477 struct isl_sol_map *sol_map = NULL;
3479 if (!bmap)
3480 goto error;
3482 sol_map = isl_calloc_type(bmap->ctx, struct isl_sol_map);
3483 if (!sol_map)
3484 goto error;
3486 sol_map->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
3487 sol_map->sol.dec_level.callback.run = &sol_dec_level_wrap;
3488 sol_map->sol.dec_level.sol = &sol_map->sol;
3489 sol_map->sol.max = max;
3490 sol_map->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
3491 sol_map->sol.add = &sol_map_add_wrap;
3492 sol_map->sol.add_empty = track_empty ? &sol_map_add_empty_wrap : NULL;
3493 sol_map->sol.free = &sol_map_free_wrap;
3494 sol_map->map = isl_map_alloc_space(isl_basic_map_get_space(bmap), 1,
3495 ISL_MAP_DISJOINT);
3496 if (!sol_map->map)
3497 goto error;
3499 sol_map->sol.context = isl_context_alloc(dom);
3500 if (!sol_map->sol.context)
3501 goto error;
3503 if (track_empty) {
3504 sol_map->empty = isl_set_alloc_space(isl_basic_set_get_space(dom),
3505 1, ISL_SET_DISJOINT);
3506 if (!sol_map->empty)
3507 goto error;
3510 isl_basic_set_free(dom);
3511 return &sol_map->sol;
3512 error:
3513 isl_basic_set_free(dom);
3514 sol_map_free(sol_map);
3515 return NULL;
3518 /* Check whether all coefficients of (non-parameter) variables
3519 * are non-positive, meaning that no pivots can be performed on the row.
3521 static int is_critical(struct isl_tab *tab, int row)
3523 int j;
3524 unsigned off = 2 + tab->M;
3526 for (j = tab->n_dead; j < tab->n_col; ++j) {
3527 if (tab->col_var[j] >= 0 &&
3528 (tab->col_var[j] < tab->n_param ||
3529 tab->col_var[j] >= tab->n_var - tab->n_div))
3530 continue;
3532 if (isl_int_is_pos(tab->mat->row[row][off + j]))
3533 return 0;
3536 return 1;
3539 /* Check whether the inequality represented by vec is strict over the integers,
3540 * i.e., there are no integer values satisfying the constraint with
3541 * equality. This happens if the gcd of the coefficients is not a divisor
3542 * of the constant term. If so, scale the constraint down by the gcd
3543 * of the coefficients.
3545 static int is_strict(struct isl_vec *vec)
3547 isl_int gcd;
3548 int strict = 0;
3550 isl_int_init(gcd);
3551 isl_seq_gcd(vec->el + 1, vec->size - 1, &gcd);
3552 if (!isl_int_is_one(gcd)) {
3553 strict = !isl_int_is_divisible_by(vec->el[0], gcd);
3554 isl_int_fdiv_q(vec->el[0], vec->el[0], gcd);
3555 isl_seq_scale_down(vec->el + 1, vec->el + 1, gcd, vec->size-1);
3557 isl_int_clear(gcd);
3559 return strict;
3562 /* Determine the sign of the given row of the main tableau.
3563 * The result is one of
3564 * isl_tab_row_pos: always non-negative; no pivot needed
3565 * isl_tab_row_neg: always non-positive; pivot
3566 * isl_tab_row_any: can be both positive and negative; split
3568 * We first handle some simple cases
3569 * - the row sign may be known already
3570 * - the row may be obviously non-negative
3571 * - the parametric constant may be equal to that of another row
3572 * for which we know the sign. This sign will be either "pos" or
3573 * "any". If it had been "neg" then we would have pivoted before.
3575 * If none of these cases hold, we check the value of the row for each
3576 * of the currently active samples. Based on the signs of these values
3577 * we make an initial determination of the sign of the row.
3579 * all zero -> unk(nown)
3580 * all non-negative -> pos
3581 * all non-positive -> neg
3582 * both negative and positive -> all
3584 * If we end up with "all", we are done.
3585 * Otherwise, we perform a check for positive and/or negative
3586 * values as follows.
3588 * samples neg unk pos
3589 * <0 ? Y N Y N
3590 * pos any pos
3591 * >0 ? Y N Y N
3592 * any neg any neg
3594 * There is no special sign for "zero", because we can usually treat zero
3595 * as either non-negative or non-positive, whatever works out best.
3596 * However, if the row is "critical", meaning that pivoting is impossible
3597 * then we don't want to limp zero with the non-positive case, because
3598 * then we we would lose the solution for those values of the parameters
3599 * where the value of the row is zero. Instead, we treat 0 as non-negative
3600 * ensuring a split if the row can attain both zero and negative values.
3601 * The same happens when the original constraint was one that could not
3602 * be satisfied with equality by any integer values of the parameters.
3603 * In this case, we normalize the constraint, but then a value of zero
3604 * for the normalized constraint is actually a positive value for the
3605 * original constraint, so again we need to treat zero as non-negative.
3606 * In both these cases, we have the following decision tree instead:
3608 * all non-negative -> pos
3609 * all negative -> neg
3610 * both negative and non-negative -> all
3612 * samples neg pos
3613 * <0 ? Y N
3614 * any pos
3615 * >=0 ? Y N
3616 * any neg
3618 static enum isl_tab_row_sign row_sign(struct isl_tab *tab,
3619 struct isl_sol *sol, int row)
3621 struct isl_vec *ineq = NULL;
3622 enum isl_tab_row_sign res = isl_tab_row_unknown;
3623 int critical;
3624 int strict;
3625 int row2;
3627 if (tab->row_sign[row] != isl_tab_row_unknown)
3628 return tab->row_sign[row];
3629 if (is_obviously_nonneg(tab, row))
3630 return isl_tab_row_pos;
3631 for (row2 = tab->n_redundant; row2 < tab->n_row; ++row2) {
3632 if (tab->row_sign[row2] == isl_tab_row_unknown)
3633 continue;
3634 if (identical_parameter_line(tab, row, row2))
3635 return tab->row_sign[row2];
3638 critical = is_critical(tab, row);
3640 ineq = get_row_parameter_ineq(tab, row);
3641 if (!ineq)
3642 goto error;
3644 strict = is_strict(ineq);
3646 res = sol->context->op->ineq_sign(sol->context, ineq->el,
3647 critical || strict);
3649 if (res == isl_tab_row_unknown || res == isl_tab_row_pos) {
3650 /* test for negative values */
3651 int feasible;
3652 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3653 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3655 feasible = sol->context->op->test_ineq(sol->context, ineq->el);
3656 if (feasible < 0)
3657 goto error;
3658 if (!feasible)
3659 res = isl_tab_row_pos;
3660 else
3661 res = (res == isl_tab_row_unknown) ? isl_tab_row_neg
3662 : isl_tab_row_any;
3663 if (res == isl_tab_row_neg) {
3664 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3665 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3669 if (res == isl_tab_row_neg) {
3670 /* test for positive values */
3671 int feasible;
3672 if (!critical && !strict)
3673 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3675 feasible = sol->context->op->test_ineq(sol->context, ineq->el);
3676 if (feasible < 0)
3677 goto error;
3678 if (feasible)
3679 res = isl_tab_row_any;
3682 isl_vec_free(ineq);
3683 return res;
3684 error:
3685 isl_vec_free(ineq);
3686 return isl_tab_row_unknown;
3689 static void find_solutions(struct isl_sol *sol, struct isl_tab *tab);
3691 /* Find solutions for values of the parameters that satisfy the given
3692 * inequality.
3694 * We currently take a snapshot of the context tableau that is reset
3695 * when we return from this function, while we make a copy of the main
3696 * tableau, leaving the original main tableau untouched.
3697 * These are fairly arbitrary choices. Making a copy also of the context
3698 * tableau would obviate the need to undo any changes made to it later,
3699 * while taking a snapshot of the main tableau could reduce memory usage.
3700 * If we were to switch to taking a snapshot of the main tableau,
3701 * we would have to keep in mind that we need to save the row signs
3702 * and that we need to do this before saving the current basis
3703 * such that the basis has been restore before we restore the row signs.
3705 static void find_in_pos(struct isl_sol *sol, struct isl_tab *tab, isl_int *ineq)
3707 void *saved;
3709 if (!sol->context)
3710 goto error;
3711 saved = sol->context->op->save(sol->context);
3713 tab = isl_tab_dup(tab);
3714 if (!tab)
3715 goto error;
3717 sol->context->op->add_ineq(sol->context, ineq, 0, 1);
3719 find_solutions(sol, tab);
3721 if (!sol->error)
3722 sol->context->op->restore(sol->context, saved);
3723 else
3724 sol->context->op->discard(saved);
3725 return;
3726 error:
3727 sol->error = 1;
3730 /* Record the absence of solutions for those values of the parameters
3731 * that do not satisfy the given inequality with equality.
3733 static void no_sol_in_strict(struct isl_sol *sol,
3734 struct isl_tab *tab, struct isl_vec *ineq)
3736 int empty;
3737 void *saved;
3739 if (!sol->context || sol->error)
3740 goto error;
3741 saved = sol->context->op->save(sol->context);
3743 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3745 sol->context->op->add_ineq(sol->context, ineq->el, 1, 0);
3746 if (!sol->context)
3747 goto error;
3749 empty = tab->empty;
3750 tab->empty = 1;
3751 sol_add(sol, tab);
3752 tab->empty = empty;
3754 isl_int_add_ui(ineq->el[0], ineq->el[0], 1);
3756 sol->context->op->restore(sol->context, saved);
3757 return;
3758 error:
3759 sol->error = 1;
3762 /* Reset all row variables that are marked to have a sign that may
3763 * be both positive and negative to have an unknown sign.
3765 static void reset_any_to_unknown(struct isl_tab *tab)
3767 int row;
3769 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3770 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
3771 continue;
3772 if (tab->row_sign[row] == isl_tab_row_any)
3773 tab->row_sign[row] = isl_tab_row_unknown;
3777 /* Compute the lexicographic minimum of the set represented by the main
3778 * tableau "tab" within the context "sol->context_tab".
3779 * On entry the sample value of the main tableau is lexicographically
3780 * less than or equal to this lexicographic minimum.
3781 * Pivots are performed until a feasible point is found, which is then
3782 * necessarily equal to the minimum, or until the tableau is found to
3783 * be infeasible. Some pivots may need to be performed for only some
3784 * feasible values of the context tableau. If so, the context tableau
3785 * is split into a part where the pivot is needed and a part where it is not.
3787 * Whenever we enter the main loop, the main tableau is such that no
3788 * "obvious" pivots need to be performed on it, where "obvious" means
3789 * that the given row can be seen to be negative without looking at
3790 * the context tableau. In particular, for non-parametric problems,
3791 * no pivots need to be performed on the main tableau.
3792 * The caller of find_solutions is responsible for making this property
3793 * hold prior to the first iteration of the loop, while restore_lexmin
3794 * is called before every other iteration.
3796 * Inside the main loop, we first examine the signs of the rows of
3797 * the main tableau within the context of the context tableau.
3798 * If we find a row that is always non-positive for all values of
3799 * the parameters satisfying the context tableau and negative for at
3800 * least one value of the parameters, we perform the appropriate pivot
3801 * and start over. An exception is the case where no pivot can be
3802 * performed on the row. In this case, we require that the sign of
3803 * the row is negative for all values of the parameters (rather than just
3804 * non-positive). This special case is handled inside row_sign, which
3805 * will say that the row can have any sign if it determines that it can
3806 * attain both negative and zero values.
3808 * If we can't find a row that always requires a pivot, but we can find
3809 * one or more rows that require a pivot for some values of the parameters
3810 * (i.e., the row can attain both positive and negative signs), then we split
3811 * the context tableau into two parts, one where we force the sign to be
3812 * non-negative and one where we force is to be negative.
3813 * The non-negative part is handled by a recursive call (through find_in_pos).
3814 * Upon returning from this call, we continue with the negative part and
3815 * perform the required pivot.
3817 * If no such rows can be found, all rows are non-negative and we have
3818 * found a (rational) feasible point. If we only wanted a rational point
3819 * then we are done.
3820 * Otherwise, we check if all values of the sample point of the tableau
3821 * are integral for the variables. If so, we have found the minimal
3822 * integral point and we are done.
3823 * If the sample point is not integral, then we need to make a distinction
3824 * based on whether the constant term is non-integral or the coefficients
3825 * of the parameters. Furthermore, in order to decide how to handle
3826 * the non-integrality, we also need to know whether the coefficients
3827 * of the other columns in the tableau are integral. This leads
3828 * to the following table. The first two rows do not correspond
3829 * to a non-integral sample point and are only mentioned for completeness.
3831 * constant parameters other
3833 * int int int |
3834 * int int rat | -> no problem
3836 * rat int int -> fail
3838 * rat int rat -> cut
3840 * int rat rat |
3841 * rat rat rat | -> parametric cut
3843 * int rat int |
3844 * rat rat int | -> split context
3846 * If the parametric constant is completely integral, then there is nothing
3847 * to be done. If the constant term is non-integral, but all the other
3848 * coefficient are integral, then there is nothing that can be done
3849 * and the tableau has no integral solution.
3850 * If, on the other hand, one or more of the other columns have rational
3851 * coefficients, but the parameter coefficients are all integral, then
3852 * we can perform a regular (non-parametric) cut.
3853 * Finally, if there is any parameter coefficient that is non-integral,
3854 * then we need to involve the context tableau. There are two cases here.
3855 * If at least one other column has a rational coefficient, then we
3856 * can perform a parametric cut in the main tableau by adding a new
3857 * integer division in the context tableau.
3858 * If all other columns have integral coefficients, then we need to
3859 * enforce that the rational combination of parameters (c + \sum a_i y_i)/m
3860 * is always integral. We do this by introducing an integer division
3861 * q = floor((c + \sum a_i y_i)/m) and stipulating that its argument should
3862 * always be integral in the context tableau, i.e., m q = c + \sum a_i y_i.
3863 * Since q is expressed in the tableau as
3864 * c + \sum a_i y_i - m q >= 0
3865 * -c - \sum a_i y_i + m q + m - 1 >= 0
3866 * it is sufficient to add the inequality
3867 * -c - \sum a_i y_i + m q >= 0
3868 * In the part of the context where this inequality does not hold, the
3869 * main tableau is marked as being empty.
3871 static void find_solutions(struct isl_sol *sol, struct isl_tab *tab)
3873 struct isl_context *context;
3874 int r;
3876 if (!tab || sol->error)
3877 goto error;
3879 context = sol->context;
3881 if (tab->empty)
3882 goto done;
3883 if (context->op->is_empty(context))
3884 goto done;
3886 for (r = 0; r >= 0 && tab && !tab->empty; r = restore_lexmin(tab)) {
3887 int flags;
3888 int row;
3889 enum isl_tab_row_sign sgn;
3890 int split = -1;
3891 int n_split = 0;
3893 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3894 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
3895 continue;
3896 sgn = row_sign(tab, sol, row);
3897 if (!sgn)
3898 goto error;
3899 tab->row_sign[row] = sgn;
3900 if (sgn == isl_tab_row_any)
3901 n_split++;
3902 if (sgn == isl_tab_row_any && split == -1)
3903 split = row;
3904 if (sgn == isl_tab_row_neg)
3905 break;
3907 if (row < tab->n_row)
3908 continue;
3909 if (split != -1) {
3910 struct isl_vec *ineq;
3911 if (n_split != 1)
3912 split = context->op->best_split(context, tab);
3913 if (split < 0)
3914 goto error;
3915 ineq = get_row_parameter_ineq(tab, split);
3916 if (!ineq)
3917 goto error;
3918 is_strict(ineq);
3919 reset_any_to_unknown(tab);
3920 tab->row_sign[split] = isl_tab_row_pos;
3921 sol_inc_level(sol);
3922 find_in_pos(sol, tab, ineq->el);
3923 tab->row_sign[split] = isl_tab_row_neg;
3924 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3925 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3926 if (!sol->error)
3927 context->op->add_ineq(context, ineq->el, 0, 1);
3928 isl_vec_free(ineq);
3929 if (sol->error)
3930 goto error;
3931 continue;
3933 if (tab->rational)
3934 break;
3935 row = first_non_integer_row(tab, &flags);
3936 if (row < 0)
3937 break;
3938 if (ISL_FL_ISSET(flags, I_PAR)) {
3939 if (ISL_FL_ISSET(flags, I_VAR)) {
3940 if (isl_tab_mark_empty(tab) < 0)
3941 goto error;
3942 break;
3944 row = add_cut(tab, row);
3945 } else if (ISL_FL_ISSET(flags, I_VAR)) {
3946 struct isl_vec *div;
3947 struct isl_vec *ineq;
3948 int d;
3949 div = get_row_split_div(tab, row);
3950 if (!div)
3951 goto error;
3952 d = context->op->get_div(context, tab, div);
3953 isl_vec_free(div);
3954 if (d < 0)
3955 goto error;
3956 ineq = ineq_for_div(context->op->peek_basic_set(context), d);
3957 if (!ineq)
3958 goto error;
3959 sol_inc_level(sol);
3960 no_sol_in_strict(sol, tab, ineq);
3961 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3962 context->op->add_ineq(context, ineq->el, 1, 1);
3963 isl_vec_free(ineq);
3964 if (sol->error || !context->op->is_ok(context))
3965 goto error;
3966 tab = set_row_cst_to_div(tab, row, d);
3967 if (context->op->is_empty(context))
3968 break;
3969 } else
3970 row = add_parametric_cut(tab, row, context);
3971 if (row < 0)
3972 goto error;
3974 if (r < 0)
3975 goto error;
3976 done:
3977 sol_add(sol, tab);
3978 isl_tab_free(tab);
3979 return;
3980 error:
3981 isl_tab_free(tab);
3982 sol->error = 1;
3985 /* Does "sol" contain a pair of partial solutions that could potentially
3986 * be merged?
3988 * We currently only check that "sol" is not in an error state
3989 * and that there are at least two partial solutions of which the final two
3990 * are defined at the same level.
3992 static int sol_has_mergeable_solutions(struct isl_sol *sol)
3994 if (sol->error)
3995 return 0;
3996 if (!sol->partial)
3997 return 0;
3998 if (!sol->partial->next)
3999 return 0;
4000 return sol->partial->level == sol->partial->next->level;
4003 /* Compute the lexicographic minimum of the set represented by the main
4004 * tableau "tab" within the context "sol->context_tab".
4006 * As a preprocessing step, we first transfer all the purely parametric
4007 * equalities from the main tableau to the context tableau, i.e.,
4008 * parameters that have been pivoted to a row.
4009 * These equalities are ignored by the main algorithm, because the
4010 * corresponding rows may not be marked as being non-negative.
4011 * In parts of the context where the added equality does not hold,
4012 * the main tableau is marked as being empty.
4014 * Before we embark on the actual computation, we save a copy
4015 * of the context. When we return, we check if there are any
4016 * partial solutions that can potentially be merged. If so,
4017 * we perform a rollback to the initial state of the context.
4018 * The merging of partial solutions happens inside calls to
4019 * sol_dec_level that are pushed onto the undo stack of the context.
4020 * If there are no partial solutions that can potentially be merged
4021 * then the rollback is skipped as it would just be wasted effort.
4023 static void find_solutions_main(struct isl_sol *sol, struct isl_tab *tab)
4025 int row;
4026 void *saved;
4028 if (!tab)
4029 goto error;
4031 sol->level = 0;
4033 for (row = tab->n_redundant; row < tab->n_row; ++row) {
4034 int p;
4035 struct isl_vec *eq;
4037 if (tab->row_var[row] < 0)
4038 continue;
4039 if (tab->row_var[row] >= tab->n_param &&
4040 tab->row_var[row] < tab->n_var - tab->n_div)
4041 continue;
4042 if (tab->row_var[row] < tab->n_param)
4043 p = tab->row_var[row];
4044 else
4045 p = tab->row_var[row]
4046 + tab->n_param - (tab->n_var - tab->n_div);
4048 eq = isl_vec_alloc(tab->mat->ctx, 1+tab->n_param+tab->n_div);
4049 if (!eq)
4050 goto error;
4051 get_row_parameter_line(tab, row, eq->el);
4052 isl_int_neg(eq->el[1 + p], tab->mat->row[row][0]);
4053 eq = isl_vec_normalize(eq);
4055 sol_inc_level(sol);
4056 no_sol_in_strict(sol, tab, eq);
4058 isl_seq_neg(eq->el, eq->el, eq->size);
4059 sol_inc_level(sol);
4060 no_sol_in_strict(sol, tab, eq);
4061 isl_seq_neg(eq->el, eq->el, eq->size);
4063 sol->context->op->add_eq(sol->context, eq->el, 1, 1);
4065 isl_vec_free(eq);
4067 if (isl_tab_mark_redundant(tab, row) < 0)
4068 goto error;
4070 if (sol->context->op->is_empty(sol->context))
4071 break;
4073 row = tab->n_redundant - 1;
4076 saved = sol->context->op->save(sol->context);
4078 find_solutions(sol, tab);
4080 if (sol_has_mergeable_solutions(sol))
4081 sol->context->op->restore(sol->context, saved);
4082 else
4083 sol->context->op->discard(saved);
4085 sol->level = 0;
4086 sol_pop(sol);
4088 return;
4089 error:
4090 isl_tab_free(tab);
4091 sol->error = 1;
4094 /* Check if integer division "div" of "dom" also occurs in "bmap".
4095 * If so, return its position within the divs.
4096 * If not, return -1.
4098 static int find_context_div(struct isl_basic_map *bmap,
4099 struct isl_basic_set *dom, unsigned div)
4101 int i;
4102 unsigned b_dim = isl_space_dim(bmap->dim, isl_dim_all);
4103 unsigned d_dim = isl_space_dim(dom->dim, isl_dim_all);
4105 if (isl_int_is_zero(dom->div[div][0]))
4106 return -1;
4107 if (isl_seq_first_non_zero(dom->div[div] + 2 + d_dim, dom->n_div) != -1)
4108 return -1;
4110 for (i = 0; i < bmap->n_div; ++i) {
4111 if (isl_int_is_zero(bmap->div[i][0]))
4112 continue;
4113 if (isl_seq_first_non_zero(bmap->div[i] + 2 + d_dim,
4114 (b_dim - d_dim) + bmap->n_div) != -1)
4115 continue;
4116 if (isl_seq_eq(bmap->div[i], dom->div[div], 2 + d_dim))
4117 return i;
4119 return -1;
4122 /* The correspondence between the variables in the main tableau,
4123 * the context tableau, and the input map and domain is as follows.
4124 * The first n_param and the last n_div variables of the main tableau
4125 * form the variables of the context tableau.
4126 * In the basic map, these n_param variables correspond to the
4127 * parameters and the input dimensions. In the domain, they correspond
4128 * to the parameters and the set dimensions.
4129 * The n_div variables correspond to the integer divisions in the domain.
4130 * To ensure that everything lines up, we may need to copy some of the
4131 * integer divisions of the domain to the map. These have to be placed
4132 * in the same order as those in the context and they have to be placed
4133 * after any other integer divisions that the map may have.
4134 * This function performs the required reordering.
4136 static struct isl_basic_map *align_context_divs(struct isl_basic_map *bmap,
4137 struct isl_basic_set *dom)
4139 int i;
4140 int common = 0;
4141 int other;
4143 for (i = 0; i < dom->n_div; ++i)
4144 if (find_context_div(bmap, dom, i) != -1)
4145 common++;
4146 other = bmap->n_div - common;
4147 if (dom->n_div - common > 0) {
4148 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
4149 dom->n_div - common, 0, 0);
4150 if (!bmap)
4151 return NULL;
4153 for (i = 0; i < dom->n_div; ++i) {
4154 int pos = find_context_div(bmap, dom, i);
4155 if (pos < 0) {
4156 pos = isl_basic_map_alloc_div(bmap);
4157 if (pos < 0)
4158 goto error;
4159 isl_int_set_si(bmap->div[pos][0], 0);
4161 if (pos != other + i)
4162 isl_basic_map_swap_div(bmap, pos, other + i);
4164 return bmap;
4165 error:
4166 isl_basic_map_free(bmap);
4167 return NULL;
4170 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4171 * some obvious symmetries.
4173 * We make sure the divs in the domain are properly ordered,
4174 * because they will be added one by one in the given order
4175 * during the construction of the solution map.
4176 * Furthermore, make sure that the known integer divisions
4177 * appear before any unknown integer division because the solution
4178 * may depend on the known integer divisions, while anything that
4179 * depends on any variable starting from the first unknown integer
4180 * division is ignored in sol_pma_add.
4182 static struct isl_sol *basic_map_partial_lexopt_base_sol(
4183 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4184 __isl_give isl_set **empty, int max,
4185 struct isl_sol *(*init)(__isl_keep isl_basic_map *bmap,
4186 __isl_take isl_basic_set *dom, int track_empty, int max))
4188 struct isl_tab *tab;
4189 struct isl_sol *sol = NULL;
4190 struct isl_context *context;
4192 if (dom->n_div) {
4193 dom = isl_basic_set_sort_divs(dom);
4194 bmap = align_context_divs(bmap, dom);
4196 sol = init(bmap, dom, !!empty, max);
4197 if (!sol)
4198 goto error;
4200 context = sol->context;
4201 if (isl_basic_set_plain_is_empty(context->op->peek_basic_set(context)))
4202 /* nothing */;
4203 else if (isl_basic_map_plain_is_empty(bmap)) {
4204 if (sol->add_empty)
4205 sol->add_empty(sol,
4206 isl_basic_set_copy(context->op->peek_basic_set(context)));
4207 } else {
4208 tab = tab_for_lexmin(bmap,
4209 context->op->peek_basic_set(context), 1, max);
4210 tab = context->op->detect_nonnegative_parameters(context, tab);
4211 find_solutions_main(sol, tab);
4213 if (sol->error)
4214 goto error;
4216 isl_basic_map_free(bmap);
4217 return sol;
4218 error:
4219 sol_free(sol);
4220 isl_basic_map_free(bmap);
4221 return NULL;
4224 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4225 * some obvious symmetries.
4227 * We call basic_map_partial_lexopt_base_sol and extract the results.
4229 static __isl_give isl_map *basic_map_partial_lexopt_base(
4230 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4231 __isl_give isl_set **empty, int max)
4233 isl_map *result = NULL;
4234 struct isl_sol *sol;
4235 struct isl_sol_map *sol_map;
4237 sol = basic_map_partial_lexopt_base_sol(bmap, dom, empty, max,
4238 &sol_map_init);
4239 if (!sol)
4240 return NULL;
4241 sol_map = (struct isl_sol_map *) sol;
4243 result = isl_map_copy(sol_map->map);
4244 if (empty)
4245 *empty = isl_set_copy(sol_map->empty);
4246 sol_free(&sol_map->sol);
4247 return result;
4250 /* Return a count of the number of occurrences of the "n" first
4251 * variables in the inequality constraints of "bmap".
4253 static __isl_give int *count_occurrences(__isl_keep isl_basic_map *bmap,
4254 int n)
4256 int i, j;
4257 isl_ctx *ctx;
4258 int *occurrences;
4260 if (!bmap)
4261 return NULL;
4262 ctx = isl_basic_map_get_ctx(bmap);
4263 occurrences = isl_calloc_array(ctx, int, n);
4264 if (!occurrences)
4265 return NULL;
4267 for (i = 0; i < bmap->n_ineq; ++i) {
4268 for (j = 0; j < n; ++j) {
4269 if (!isl_int_is_zero(bmap->ineq[i][1 + j]))
4270 occurrences[j]++;
4274 return occurrences;
4277 /* Do all of the "n" variables with non-zero coefficients in "c"
4278 * occur in exactly a single constraint.
4279 * "occurrences" is an array of length "n" containing the number
4280 * of occurrences of each of the variables in the inequality constraints.
4282 static int single_occurrence(int n, isl_int *c, int *occurrences)
4284 int i;
4286 for (i = 0; i < n; ++i) {
4287 if (isl_int_is_zero(c[i]))
4288 continue;
4289 if (occurrences[i] != 1)
4290 return 0;
4293 return 1;
4296 /* Do all of the "n" initial variables that occur in inequality constraint
4297 * "ineq" of "bmap" only occur in that constraint?
4299 static int all_single_occurrence(__isl_keep isl_basic_map *bmap, int ineq,
4300 int n)
4302 int i, j;
4304 for (i = 0; i < n; ++i) {
4305 if (isl_int_is_zero(bmap->ineq[ineq][1 + i]))
4306 continue;
4307 for (j = 0; j < bmap->n_ineq; ++j) {
4308 if (j == ineq)
4309 continue;
4310 if (!isl_int_is_zero(bmap->ineq[j][1 + i]))
4311 return 0;
4315 return 1;
4318 /* Structure used during detection of parallel constraints.
4319 * n_in: number of "input" variables: isl_dim_param + isl_dim_in
4320 * n_out: number of "output" variables: isl_dim_out + isl_dim_div
4321 * val: the coefficients of the output variables
4323 struct isl_constraint_equal_info {
4324 isl_basic_map *bmap;
4325 unsigned n_in;
4326 unsigned n_out;
4327 isl_int *val;
4330 /* Check whether the coefficients of the output variables
4331 * of the constraint in "entry" are equal to info->val.
4333 static int constraint_equal(const void *entry, const void *val)
4335 isl_int **row = (isl_int **)entry;
4336 const struct isl_constraint_equal_info *info = val;
4338 return isl_seq_eq((*row) + 1 + info->n_in, info->val, info->n_out);
4341 /* Check whether "bmap" has a pair of constraints that have
4342 * the same coefficients for the output variables.
4343 * Note that the coefficients of the existentially quantified
4344 * variables need to be zero since the existentially quantified
4345 * of the result are usually not the same as those of the input.
4346 * Furthermore, check that each of the input variables that occur
4347 * in those constraints does not occur in any other constraint.
4348 * If so, return 1 and return the row indices of the two constraints
4349 * in *first and *second.
4351 static int parallel_constraints(__isl_keep isl_basic_map *bmap,
4352 int *first, int *second)
4354 int i;
4355 isl_ctx *ctx;
4356 int *occurrences = NULL;
4357 struct isl_hash_table *table = NULL;
4358 struct isl_hash_table_entry *entry;
4359 struct isl_constraint_equal_info info;
4360 unsigned n_out;
4361 unsigned n_div;
4363 ctx = isl_basic_map_get_ctx(bmap);
4364 table = isl_hash_table_alloc(ctx, bmap->n_ineq);
4365 if (!table)
4366 goto error;
4368 info.n_in = isl_basic_map_dim(bmap, isl_dim_param) +
4369 isl_basic_map_dim(bmap, isl_dim_in);
4370 occurrences = count_occurrences(bmap, info.n_in);
4371 if (info.n_in && !occurrences)
4372 goto error;
4373 info.bmap = bmap;
4374 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4375 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4376 info.n_out = n_out + n_div;
4377 for (i = 0; i < bmap->n_ineq; ++i) {
4378 uint32_t hash;
4380 info.val = bmap->ineq[i] + 1 + info.n_in;
4381 if (isl_seq_first_non_zero(info.val, n_out) < 0)
4382 continue;
4383 if (isl_seq_first_non_zero(info.val + n_out, n_div) >= 0)
4384 continue;
4385 if (!single_occurrence(info.n_in, bmap->ineq[i] + 1,
4386 occurrences))
4387 continue;
4388 hash = isl_seq_get_hash(info.val, info.n_out);
4389 entry = isl_hash_table_find(ctx, table, hash,
4390 constraint_equal, &info, 1);
4391 if (!entry)
4392 goto error;
4393 if (entry->data)
4394 break;
4395 entry->data = &bmap->ineq[i];
4398 if (i < bmap->n_ineq) {
4399 *first = ((isl_int **)entry->data) - bmap->ineq;
4400 *second = i;
4403 isl_hash_table_free(ctx, table);
4404 free(occurrences);
4406 return i < bmap->n_ineq;
4407 error:
4408 isl_hash_table_free(ctx, table);
4409 free(occurrences);
4410 return -1;
4413 /* Given a set of upper bounds in "var", add constraints to "bset"
4414 * that make the i-th bound smallest.
4416 * In particular, if there are n bounds b_i, then add the constraints
4418 * b_i <= b_j for j > i
4419 * b_i < b_j for j < i
4421 static __isl_give isl_basic_set *select_minimum(__isl_take isl_basic_set *bset,
4422 __isl_keep isl_mat *var, int i)
4424 isl_ctx *ctx;
4425 int j, k;
4427 ctx = isl_mat_get_ctx(var);
4429 for (j = 0; j < var->n_row; ++j) {
4430 if (j == i)
4431 continue;
4432 k = isl_basic_set_alloc_inequality(bset);
4433 if (k < 0)
4434 goto error;
4435 isl_seq_combine(bset->ineq[k], ctx->one, var->row[j],
4436 ctx->negone, var->row[i], var->n_col);
4437 isl_int_set_si(bset->ineq[k][var->n_col], 0);
4438 if (j < i)
4439 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
4442 bset = isl_basic_set_finalize(bset);
4444 return bset;
4445 error:
4446 isl_basic_set_free(bset);
4447 return NULL;
4450 /* Given a set of upper bounds on the last "input" variable m,
4451 * construct a set that assigns the minimal upper bound to m, i.e.,
4452 * construct a set that divides the space into cells where one
4453 * of the upper bounds is smaller than all the others and assign
4454 * this upper bound to m.
4456 * In particular, if there are n bounds b_i, then the result
4457 * consists of n basic sets, each one of the form
4459 * m = b_i
4460 * b_i <= b_j for j > i
4461 * b_i < b_j for j < i
4463 static __isl_give isl_set *set_minimum(__isl_take isl_space *dim,
4464 __isl_take isl_mat *var)
4466 int i, k;
4467 isl_basic_set *bset = NULL;
4468 isl_set *set = NULL;
4470 if (!dim || !var)
4471 goto error;
4473 set = isl_set_alloc_space(isl_space_copy(dim),
4474 var->n_row, ISL_SET_DISJOINT);
4476 for (i = 0; i < var->n_row; ++i) {
4477 bset = isl_basic_set_alloc_space(isl_space_copy(dim), 0,
4478 1, var->n_row - 1);
4479 k = isl_basic_set_alloc_equality(bset);
4480 if (k < 0)
4481 goto error;
4482 isl_seq_cpy(bset->eq[k], var->row[i], var->n_col);
4483 isl_int_set_si(bset->eq[k][var->n_col], -1);
4484 bset = select_minimum(bset, var, i);
4485 set = isl_set_add_basic_set(set, bset);
4488 isl_space_free(dim);
4489 isl_mat_free(var);
4490 return set;
4491 error:
4492 isl_basic_set_free(bset);
4493 isl_set_free(set);
4494 isl_space_free(dim);
4495 isl_mat_free(var);
4496 return NULL;
4499 /* Given that the last input variable of "bmap" represents the minimum
4500 * of the bounds in "cst", check whether we need to split the domain
4501 * based on which bound attains the minimum.
4503 * A split is needed when the minimum appears in an integer division
4504 * or in an equality. Otherwise, it is only needed if it appears in
4505 * an upper bound that is different from the upper bounds on which it
4506 * is defined.
4508 static int need_split_basic_map(__isl_keep isl_basic_map *bmap,
4509 __isl_keep isl_mat *cst)
4511 int i, j;
4512 unsigned total;
4513 unsigned pos;
4515 pos = cst->n_col - 1;
4516 total = isl_basic_map_dim(bmap, isl_dim_all);
4518 for (i = 0; i < bmap->n_div; ++i)
4519 if (!isl_int_is_zero(bmap->div[i][2 + pos]))
4520 return 1;
4522 for (i = 0; i < bmap->n_eq; ++i)
4523 if (!isl_int_is_zero(bmap->eq[i][1 + pos]))
4524 return 1;
4526 for (i = 0; i < bmap->n_ineq; ++i) {
4527 if (isl_int_is_nonneg(bmap->ineq[i][1 + pos]))
4528 continue;
4529 if (!isl_int_is_negone(bmap->ineq[i][1 + pos]))
4530 return 1;
4531 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + pos + 1,
4532 total - pos - 1) >= 0)
4533 return 1;
4535 for (j = 0; j < cst->n_row; ++j)
4536 if (isl_seq_eq(bmap->ineq[i], cst->row[j], cst->n_col))
4537 break;
4538 if (j >= cst->n_row)
4539 return 1;
4542 return 0;
4545 /* Given that the last set variable of "bset" represents the minimum
4546 * of the bounds in "cst", check whether we need to split the domain
4547 * based on which bound attains the minimum.
4549 * We simply call need_split_basic_map here. This is safe because
4550 * the position of the minimum is computed from "cst" and not
4551 * from "bmap".
4553 static int need_split_basic_set(__isl_keep isl_basic_set *bset,
4554 __isl_keep isl_mat *cst)
4556 return need_split_basic_map(bset_to_bmap(bset), cst);
4559 /* Given that the last set variable of "set" represents the minimum
4560 * of the bounds in "cst", check whether we need to split the domain
4561 * based on which bound attains the minimum.
4563 static int need_split_set(__isl_keep isl_set *set, __isl_keep isl_mat *cst)
4565 int i;
4567 for (i = 0; i < set->n; ++i)
4568 if (need_split_basic_set(set->p[i], cst))
4569 return 1;
4571 return 0;
4574 /* Given a set of which the last set variable is the minimum
4575 * of the bounds in "cst", split each basic set in the set
4576 * in pieces where one of the bounds is (strictly) smaller than the others.
4577 * This subdivision is given in "min_expr".
4578 * The variable is subsequently projected out.
4580 * We only do the split when it is needed.
4581 * For example if the last input variable m = min(a,b) and the only
4582 * constraints in the given basic set are lower bounds on m,
4583 * i.e., l <= m = min(a,b), then we can simply project out m
4584 * to obtain l <= a and l <= b, without having to split on whether
4585 * m is equal to a or b.
4587 static __isl_give isl_set *split(__isl_take isl_set *empty,
4588 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
4590 int n_in;
4591 int i;
4592 isl_space *dim;
4593 isl_set *res;
4595 if (!empty || !min_expr || !cst)
4596 goto error;
4598 n_in = isl_set_dim(empty, isl_dim_set);
4599 dim = isl_set_get_space(empty);
4600 dim = isl_space_drop_dims(dim, isl_dim_set, n_in - 1, 1);
4601 res = isl_set_empty(dim);
4603 for (i = 0; i < empty->n; ++i) {
4604 isl_set *set;
4606 set = isl_set_from_basic_set(isl_basic_set_copy(empty->p[i]));
4607 if (need_split_basic_set(empty->p[i], cst))
4608 set = isl_set_intersect(set, isl_set_copy(min_expr));
4609 set = isl_set_remove_dims(set, isl_dim_set, n_in - 1, 1);
4611 res = isl_set_union_disjoint(res, set);
4614 isl_set_free(empty);
4615 isl_set_free(min_expr);
4616 isl_mat_free(cst);
4617 return res;
4618 error:
4619 isl_set_free(empty);
4620 isl_set_free(min_expr);
4621 isl_mat_free(cst);
4622 return NULL;
4625 /* Given a map of which the last input variable is the minimum
4626 * of the bounds in "cst", split each basic set in the set
4627 * in pieces where one of the bounds is (strictly) smaller than the others.
4628 * This subdivision is given in "min_expr".
4629 * The variable is subsequently projected out.
4631 * The implementation is essentially the same as that of "split".
4633 static __isl_give isl_map *split_domain(__isl_take isl_map *opt,
4634 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
4636 int n_in;
4637 int i;
4638 isl_space *dim;
4639 isl_map *res;
4641 if (!opt || !min_expr || !cst)
4642 goto error;
4644 n_in = isl_map_dim(opt, isl_dim_in);
4645 dim = isl_map_get_space(opt);
4646 dim = isl_space_drop_dims(dim, isl_dim_in, n_in - 1, 1);
4647 res = isl_map_empty(dim);
4649 for (i = 0; i < opt->n; ++i) {
4650 isl_map *map;
4652 map = isl_map_from_basic_map(isl_basic_map_copy(opt->p[i]));
4653 if (need_split_basic_map(opt->p[i], cst))
4654 map = isl_map_intersect_domain(map,
4655 isl_set_copy(min_expr));
4656 map = isl_map_remove_dims(map, isl_dim_in, n_in - 1, 1);
4658 res = isl_map_union_disjoint(res, map);
4661 isl_map_free(opt);
4662 isl_set_free(min_expr);
4663 isl_mat_free(cst);
4664 return res;
4665 error:
4666 isl_map_free(opt);
4667 isl_set_free(min_expr);
4668 isl_mat_free(cst);
4669 return NULL;
4672 static __isl_give isl_map *basic_map_partial_lexopt(
4673 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4674 __isl_give isl_set **empty, int max);
4676 /* This function is called from basic_map_partial_lexopt_symm.
4677 * The last variable of "bmap" and "dom" corresponds to the minimum
4678 * of the bounds in "cst". "map_space" is the space of the original
4679 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
4680 * is the space of the original domain.
4682 * We recursively call basic_map_partial_lexopt and then plug in
4683 * the definition of the minimum in the result.
4685 static __isl_give isl_map *basic_map_partial_lexopt_symm_core(
4686 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4687 __isl_give isl_set **empty, int max, __isl_take isl_mat *cst,
4688 __isl_take isl_space *map_space, __isl_take isl_space *set_space)
4690 isl_map *opt;
4691 isl_set *min_expr;
4693 min_expr = set_minimum(isl_basic_set_get_space(dom), isl_mat_copy(cst));
4695 opt = basic_map_partial_lexopt(bmap, dom, empty, max);
4697 if (empty) {
4698 *empty = split(*empty,
4699 isl_set_copy(min_expr), isl_mat_copy(cst));
4700 *empty = isl_set_reset_space(*empty, set_space);
4703 opt = split_domain(opt, min_expr, cst);
4704 opt = isl_map_reset_space(opt, map_space);
4706 return opt;
4709 /* Extract a domain from "bmap" for the purpose of computing
4710 * a lexicographic optimum.
4712 * This function is only called when the caller wants to compute a full
4713 * lexicographic optimum, i.e., without specifying a domain. In this case,
4714 * the caller is not interested in the part of the domain space where
4715 * there is no solution and the domain can be initialized to those constraints
4716 * of "bmap" that only involve the parameters and the input dimensions.
4717 * This relieves the parametric programming engine from detecting those
4718 * inequalities and transferring them to the context. More importantly,
4719 * it ensures that those inequalities are transferred first and not
4720 * intermixed with inequalities that actually split the domain.
4722 * If the caller does not require the absence of existentially quantified
4723 * variables in the result (i.e., if ISL_OPT_QE is not set in "flags"),
4724 * then the actual domain of "bmap" can be used. This ensures that
4725 * the domain does not need to be split at all just to separate out
4726 * pieces of the domain that do not have a solution from piece that do.
4727 * This domain cannot be used in general because it may involve
4728 * (unknown) existentially quantified variables which will then also
4729 * appear in the solution.
4731 static __isl_give isl_basic_set *extract_domain(__isl_keep isl_basic_map *bmap,
4732 unsigned flags)
4734 int n_div;
4735 int n_out;
4737 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4738 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4739 bmap = isl_basic_map_copy(bmap);
4740 if (ISL_FL_ISSET(flags, ISL_OPT_QE)) {
4741 bmap = isl_basic_map_drop_constraints_involving_dims(bmap,
4742 isl_dim_div, 0, n_div);
4743 bmap = isl_basic_map_drop_constraints_involving_dims(bmap,
4744 isl_dim_out, 0, n_out);
4746 return isl_basic_map_domain(bmap);
4749 #undef TYPE
4750 #define TYPE isl_map
4751 #undef SUFFIX
4752 #define SUFFIX
4753 #include "isl_tab_lexopt_templ.c"
4755 struct isl_sol_for {
4756 struct isl_sol sol;
4757 int (*fn)(__isl_take isl_basic_set *dom,
4758 __isl_take isl_aff_list *list, void *user);
4759 void *user;
4762 static void sol_for_free(struct isl_sol_for *sol_for)
4764 if (!sol_for)
4765 return;
4766 if (sol_for->sol.context)
4767 sol_for->sol.context->op->free(sol_for->sol.context);
4768 free(sol_for);
4771 static void sol_for_free_wrap(struct isl_sol *sol)
4773 sol_for_free((struct isl_sol_for *)sol);
4776 /* Add the solution identified by the tableau and the context tableau.
4778 * See documentation of sol_add for more details.
4780 * Instead of constructing a basic map, this function calls a user
4781 * defined function with the current context as a basic set and
4782 * a list of affine expressions representing the relation between
4783 * the input and output. The space over which the affine expressions
4784 * are defined is the same as that of the domain. The number of
4785 * affine expressions in the list is equal to the number of output variables.
4787 static void sol_for_add(struct isl_sol_for *sol,
4788 struct isl_basic_set *dom, struct isl_mat *M)
4790 int i;
4791 isl_ctx *ctx;
4792 isl_local_space *ls;
4793 isl_aff *aff;
4794 isl_aff_list *list;
4796 if (sol->sol.error || !dom || !M)
4797 goto error;
4799 ctx = isl_basic_set_get_ctx(dom);
4800 ls = isl_basic_set_get_local_space(dom);
4801 list = isl_aff_list_alloc(ctx, M->n_row - 1);
4802 for (i = 1; i < M->n_row; ++i) {
4803 aff = isl_aff_alloc(isl_local_space_copy(ls));
4804 if (aff) {
4805 isl_int_set(aff->v->el[0], M->row[0][0]);
4806 isl_seq_cpy(aff->v->el + 1, M->row[i], M->n_col);
4808 aff = isl_aff_normalize(aff);
4809 list = isl_aff_list_add(list, aff);
4811 isl_local_space_free(ls);
4813 dom = isl_basic_set_finalize(dom);
4815 if (sol->fn(isl_basic_set_copy(dom), list, sol->user) < 0)
4816 goto error;
4818 isl_basic_set_free(dom);
4819 isl_mat_free(M);
4820 return;
4821 error:
4822 isl_basic_set_free(dom);
4823 isl_mat_free(M);
4824 sol->sol.error = 1;
4827 static void sol_for_add_wrap(struct isl_sol *sol,
4828 struct isl_basic_set *dom, struct isl_mat *M)
4830 sol_for_add((struct isl_sol_for *)sol, dom, M);
4833 static struct isl_sol_for *sol_for_init(struct isl_basic_map *bmap, int max,
4834 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4835 void *user),
4836 void *user)
4838 struct isl_sol_for *sol_for = NULL;
4839 isl_space *dom_dim;
4840 struct isl_basic_set *dom = NULL;
4842 sol_for = isl_calloc_type(bmap->ctx, struct isl_sol_for);
4843 if (!sol_for)
4844 goto error;
4846 dom_dim = isl_space_domain(isl_space_copy(bmap->dim));
4847 dom = isl_basic_set_universe(dom_dim);
4849 sol_for->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
4850 sol_for->sol.dec_level.callback.run = &sol_dec_level_wrap;
4851 sol_for->sol.dec_level.sol = &sol_for->sol;
4852 sol_for->fn = fn;
4853 sol_for->user = user;
4854 sol_for->sol.max = max;
4855 sol_for->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
4856 sol_for->sol.add = &sol_for_add_wrap;
4857 sol_for->sol.add_empty = NULL;
4858 sol_for->sol.free = &sol_for_free_wrap;
4860 sol_for->sol.context = isl_context_alloc(dom);
4861 if (!sol_for->sol.context)
4862 goto error;
4864 isl_basic_set_free(dom);
4865 return sol_for;
4866 error:
4867 isl_basic_set_free(dom);
4868 sol_for_free(sol_for);
4869 return NULL;
4872 static void sol_for_find_solutions(struct isl_sol_for *sol_for,
4873 struct isl_tab *tab)
4875 find_solutions_main(&sol_for->sol, tab);
4878 int isl_basic_map_foreach_lexopt(__isl_keep isl_basic_map *bmap, int max,
4879 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4880 void *user),
4881 void *user)
4883 struct isl_sol_for *sol_for = NULL;
4885 bmap = isl_basic_map_copy(bmap);
4886 bmap = isl_basic_map_detect_equalities(bmap);
4887 if (!bmap)
4888 return -1;
4890 sol_for = sol_for_init(bmap, max, fn, user);
4891 if (!sol_for)
4892 goto error;
4894 if (isl_basic_map_plain_is_empty(bmap))
4895 /* nothing */;
4896 else {
4897 struct isl_tab *tab;
4898 struct isl_context *context = sol_for->sol.context;
4899 tab = tab_for_lexmin(bmap,
4900 context->op->peek_basic_set(context), 1, max);
4901 tab = context->op->detect_nonnegative_parameters(context, tab);
4902 sol_for_find_solutions(sol_for, tab);
4903 if (sol_for->sol.error)
4904 goto error;
4907 sol_free(&sol_for->sol);
4908 isl_basic_map_free(bmap);
4909 return 0;
4910 error:
4911 sol_free(&sol_for->sol);
4912 isl_basic_map_free(bmap);
4913 return -1;
4916 int isl_basic_set_foreach_lexopt(__isl_keep isl_basic_set *bset, int max,
4917 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4918 void *user),
4919 void *user)
4921 return isl_basic_map_foreach_lexopt(bset, max, fn, user);
4924 /* Check if the given sequence of len variables starting at pos
4925 * represents a trivial (i.e., zero) solution.
4926 * The variables are assumed to be non-negative and to come in pairs,
4927 * with each pair representing a variable of unrestricted sign.
4928 * The solution is trivial if each such pair in the sequence consists
4929 * of two identical values, meaning that the variable being represented
4930 * has value zero.
4932 static int region_is_trivial(struct isl_tab *tab, int pos, int len)
4934 int i;
4936 if (len == 0)
4937 return 0;
4939 for (i = 0; i < len; i += 2) {
4940 int neg_row;
4941 int pos_row;
4943 neg_row = tab->var[pos + i].is_row ?
4944 tab->var[pos + i].index : -1;
4945 pos_row = tab->var[pos + i + 1].is_row ?
4946 tab->var[pos + i + 1].index : -1;
4948 if ((neg_row < 0 ||
4949 isl_int_is_zero(tab->mat->row[neg_row][1])) &&
4950 (pos_row < 0 ||
4951 isl_int_is_zero(tab->mat->row[pos_row][1])))
4952 continue;
4954 if (neg_row < 0 || pos_row < 0)
4955 return 0;
4956 if (isl_int_ne(tab->mat->row[neg_row][1],
4957 tab->mat->row[pos_row][1]))
4958 return 0;
4961 return 1;
4964 /* Return the index of the first trivial region or -1 if all regions
4965 * are non-trivial.
4967 static int first_trivial_region(struct isl_tab *tab,
4968 int n_region, struct isl_region *region)
4970 int i;
4972 for (i = 0; i < n_region; ++i) {
4973 if (region_is_trivial(tab, region[i].pos, region[i].len))
4974 return i;
4977 return -1;
4980 /* Check if the solution is optimal, i.e., whether the first
4981 * n_op entries are zero.
4983 static int is_optimal(__isl_keep isl_vec *sol, int n_op)
4985 int i;
4987 for (i = 0; i < n_op; ++i)
4988 if (!isl_int_is_zero(sol->el[1 + i]))
4989 return 0;
4990 return 1;
4993 /* Add constraints to "tab" that ensure that any solution is significantly
4994 * better than that represented by "sol". That is, find the first
4995 * relevant (within first n_op) non-zero coefficient and force it (along
4996 * with all previous coefficients) to be zero.
4997 * If the solution is already optimal (all relevant coefficients are zero),
4998 * then just mark the table as empty.
5000 * This function assumes that at least 2 * n_op more rows and at least
5001 * 2 * n_op more elements in the constraint array are available in the tableau.
5003 static int force_better_solution(struct isl_tab *tab,
5004 __isl_keep isl_vec *sol, int n_op)
5006 int i;
5007 isl_ctx *ctx;
5008 isl_vec *v = NULL;
5010 if (!sol)
5011 return -1;
5013 for (i = 0; i < n_op; ++i)
5014 if (!isl_int_is_zero(sol->el[1 + i]))
5015 break;
5017 if (i == n_op) {
5018 if (isl_tab_mark_empty(tab) < 0)
5019 return -1;
5020 return 0;
5023 ctx = isl_vec_get_ctx(sol);
5024 v = isl_vec_alloc(ctx, 1 + tab->n_var);
5025 if (!v)
5026 return -1;
5028 for (; i >= 0; --i) {
5029 v = isl_vec_clr(v);
5030 isl_int_set_si(v->el[1 + i], -1);
5031 if (add_lexmin_eq(tab, v->el) < 0)
5032 goto error;
5035 isl_vec_free(v);
5036 return 0;
5037 error:
5038 isl_vec_free(v);
5039 return -1;
5042 struct isl_trivial {
5043 int update;
5044 int region;
5045 int side;
5046 struct isl_tab_undo *snap;
5049 /* Return the lexicographically smallest non-trivial solution of the
5050 * given ILP problem.
5052 * All variables are assumed to be non-negative.
5054 * n_op is the number of initial coordinates to optimize.
5055 * That is, once a solution has been found, we will only continue looking
5056 * for solution that result in significantly better values for those
5057 * initial coordinates. That is, we only continue looking for solutions
5058 * that increase the number of initial zeros in this sequence.
5060 * A solution is non-trivial, if it is non-trivial on each of the
5061 * specified regions. Each region represents a sequence of pairs
5062 * of variables. A solution is non-trivial on such a region if
5063 * at least one of these pairs consists of different values, i.e.,
5064 * such that the non-negative variable represented by the pair is non-zero.
5066 * Whenever a conflict is encountered, all constraints involved are
5067 * reported to the caller through a call to "conflict".
5069 * We perform a simple branch-and-bound backtracking search.
5070 * Each level in the search represents initially trivial region that is forced
5071 * to be non-trivial.
5072 * At each level we consider n cases, where n is the length of the region.
5073 * In terms of the n/2 variables of unrestricted signs being encoded by
5074 * the region, we consider the cases
5075 * x_0 >= 1
5076 * x_0 <= -1
5077 * x_0 = 0 and x_1 >= 1
5078 * x_0 = 0 and x_1 <= -1
5079 * x_0 = 0 and x_1 = 0 and x_2 >= 1
5080 * x_0 = 0 and x_1 = 0 and x_2 <= -1
5081 * ...
5082 * The cases are considered in this order, assuming that each pair
5083 * x_i_a x_i_b represents the value x_i_b - x_i_a.
5084 * That is, x_0 >= 1 is enforced by adding the constraint
5085 * x_0_b - x_0_a >= 1
5087 __isl_give isl_vec *isl_tab_basic_set_non_trivial_lexmin(
5088 __isl_take isl_basic_set *bset, int n_op, int n_region,
5089 struct isl_region *region,
5090 int (*conflict)(int con, void *user), void *user)
5092 int i, j;
5093 int r;
5094 isl_ctx *ctx;
5095 isl_vec *v = NULL;
5096 isl_vec *sol = NULL;
5097 struct isl_tab *tab;
5098 struct isl_trivial *triv = NULL;
5099 int level, init;
5101 if (!bset)
5102 return NULL;
5104 ctx = isl_basic_set_get_ctx(bset);
5105 sol = isl_vec_alloc(ctx, 0);
5107 tab = tab_for_lexmin(bset, NULL, 0, 0);
5108 if (!tab)
5109 goto error;
5110 tab->conflict = conflict;
5111 tab->conflict_user = user;
5113 v = isl_vec_alloc(ctx, 1 + tab->n_var);
5114 triv = isl_calloc_array(ctx, struct isl_trivial, n_region);
5115 if (!v || (n_region && !triv))
5116 goto error;
5118 level = 0;
5119 init = 1;
5121 while (level >= 0) {
5122 int side, base;
5124 if (init) {
5125 tab = cut_to_integer_lexmin(tab, CUT_ONE);
5126 if (!tab)
5127 goto error;
5128 if (tab->empty)
5129 goto backtrack;
5130 r = first_trivial_region(tab, n_region, region);
5131 if (r < 0) {
5132 for (i = 0; i < level; ++i)
5133 triv[i].update = 1;
5134 isl_vec_free(sol);
5135 sol = isl_tab_get_sample_value(tab);
5136 if (!sol)
5137 goto error;
5138 if (is_optimal(sol, n_op))
5139 break;
5140 goto backtrack;
5142 if (level >= n_region)
5143 isl_die(ctx, isl_error_internal,
5144 "nesting level too deep", goto error);
5145 if (isl_tab_extend_cons(tab,
5146 2 * region[r].len + 2 * n_op) < 0)
5147 goto error;
5148 triv[level].region = r;
5149 triv[level].side = 0;
5152 r = triv[level].region;
5153 side = triv[level].side;
5154 base = 2 * (side/2);
5156 if (side >= region[r].len) {
5157 backtrack:
5158 level--;
5159 init = 0;
5160 if (level >= 0)
5161 if (isl_tab_rollback(tab, triv[level].snap) < 0)
5162 goto error;
5163 continue;
5166 if (triv[level].update) {
5167 if (force_better_solution(tab, sol, n_op) < 0)
5168 goto error;
5169 triv[level].update = 0;
5172 if (side == base && base >= 2) {
5173 for (j = base - 2; j < base; ++j) {
5174 v = isl_vec_clr(v);
5175 isl_int_set_si(v->el[1 + region[r].pos + j], 1);
5176 if (add_lexmin_eq(tab, v->el) < 0)
5177 goto error;
5181 triv[level].snap = isl_tab_snap(tab);
5182 if (isl_tab_push_basis(tab) < 0)
5183 goto error;
5185 v = isl_vec_clr(v);
5186 isl_int_set_si(v->el[0], -1);
5187 isl_int_set_si(v->el[1 + region[r].pos + side], -1);
5188 isl_int_set_si(v->el[1 + region[r].pos + (side ^ 1)], 1);
5189 tab = add_lexmin_ineq(tab, v->el);
5191 triv[level].side++;
5192 level++;
5193 init = 1;
5196 free(triv);
5197 isl_vec_free(v);
5198 isl_tab_free(tab);
5199 isl_basic_set_free(bset);
5201 return sol;
5202 error:
5203 free(triv);
5204 isl_vec_free(v);
5205 isl_tab_free(tab);
5206 isl_basic_set_free(bset);
5207 isl_vec_free(sol);
5208 return NULL;
5211 /* Wrapper for a tableau that is used for computing
5212 * the lexicographically smallest rational point of a non-negative set.
5213 * This point is represented by the sample value of "tab",
5214 * unless "tab" is empty.
5216 struct isl_tab_lexmin {
5217 isl_ctx *ctx;
5218 struct isl_tab *tab;
5221 /* Free "tl" and return NULL.
5223 __isl_null isl_tab_lexmin *isl_tab_lexmin_free(__isl_take isl_tab_lexmin *tl)
5225 if (!tl)
5226 return NULL;
5227 isl_ctx_deref(tl->ctx);
5228 isl_tab_free(tl->tab);
5229 free(tl);
5231 return NULL;
5234 /* Construct an isl_tab_lexmin for computing
5235 * the lexicographically smallest rational point in "bset",
5236 * assuming that all variables are non-negative.
5238 __isl_give isl_tab_lexmin *isl_tab_lexmin_from_basic_set(
5239 __isl_take isl_basic_set *bset)
5241 isl_ctx *ctx;
5242 isl_tab_lexmin *tl;
5244 if (!bset)
5245 return NULL;
5247 ctx = isl_basic_set_get_ctx(bset);
5248 tl = isl_calloc_type(ctx, struct isl_tab_lexmin);
5249 if (!tl)
5250 goto error;
5251 tl->ctx = ctx;
5252 isl_ctx_ref(ctx);
5253 tl->tab = tab_for_lexmin(bset, NULL, 0, 0);
5254 isl_basic_set_free(bset);
5255 if (!tl->tab)
5256 return isl_tab_lexmin_free(tl);
5257 return tl;
5258 error:
5259 isl_basic_set_free(bset);
5260 isl_tab_lexmin_free(tl);
5261 return NULL;
5264 /* Return the dimension of the set represented by "tl".
5266 int isl_tab_lexmin_dim(__isl_keep isl_tab_lexmin *tl)
5268 return tl ? tl->tab->n_var : -1;
5271 /* Add the equality with coefficients "eq" to "tl", updating the optimal
5272 * solution if needed.
5273 * The equality is added as two opposite inequality constraints.
5275 __isl_give isl_tab_lexmin *isl_tab_lexmin_add_eq(__isl_take isl_tab_lexmin *tl,
5276 isl_int *eq)
5278 unsigned n_var;
5280 if (!tl || !eq)
5281 return isl_tab_lexmin_free(tl);
5283 if (isl_tab_extend_cons(tl->tab, 2) < 0)
5284 return isl_tab_lexmin_free(tl);
5285 n_var = tl->tab->n_var;
5286 isl_seq_neg(eq, eq, 1 + n_var);
5287 tl->tab = add_lexmin_ineq(tl->tab, eq);
5288 isl_seq_neg(eq, eq, 1 + n_var);
5289 tl->tab = add_lexmin_ineq(tl->tab, eq);
5291 if (!tl->tab)
5292 return isl_tab_lexmin_free(tl);
5294 return tl;
5297 /* Return the lexicographically smallest rational point in the basic set
5298 * from which "tl" was constructed.
5299 * If the original input was empty, then return a zero-length vector.
5301 __isl_give isl_vec *isl_tab_lexmin_get_solution(__isl_keep isl_tab_lexmin *tl)
5303 if (!tl)
5304 return NULL;
5305 if (tl->tab->empty)
5306 return isl_vec_alloc(tl->ctx, 0);
5307 else
5308 return isl_tab_get_sample_value(tl->tab);
5311 /* Return the lexicographically smallest rational point in "bset",
5312 * assuming that all variables are non-negative.
5313 * If "bset" is empty, then return a zero-length vector.
5315 __isl_give isl_vec *isl_tab_basic_set_non_neg_lexmin(
5316 __isl_take isl_basic_set *bset)
5318 isl_tab_lexmin *tl;
5319 isl_vec *sol;
5321 tl = isl_tab_lexmin_from_basic_set(bset);
5322 sol = isl_tab_lexmin_get_solution(tl);
5323 isl_tab_lexmin_free(tl);
5324 return sol;
5327 struct isl_sol_pma {
5328 struct isl_sol sol;
5329 isl_pw_multi_aff *pma;
5330 isl_set *empty;
5333 static void sol_pma_free(struct isl_sol_pma *sol_pma)
5335 if (!sol_pma)
5336 return;
5337 if (sol_pma->sol.context)
5338 sol_pma->sol.context->op->free(sol_pma->sol.context);
5339 isl_pw_multi_aff_free(sol_pma->pma);
5340 isl_set_free(sol_pma->empty);
5341 free(sol_pma);
5344 /* This function is called for parts of the context where there is
5345 * no solution, with "bset" corresponding to the context tableau.
5346 * Simply add the basic set to the set "empty".
5348 static void sol_pma_add_empty(struct isl_sol_pma *sol,
5349 __isl_take isl_basic_set *bset)
5351 if (!bset || !sol->empty)
5352 goto error;
5354 sol->empty = isl_set_grow(sol->empty, 1);
5355 bset = isl_basic_set_simplify(bset);
5356 bset = isl_basic_set_finalize(bset);
5357 sol->empty = isl_set_add_basic_set(sol->empty, bset);
5358 if (!sol->empty)
5359 sol->sol.error = 1;
5360 return;
5361 error:
5362 isl_basic_set_free(bset);
5363 sol->sol.error = 1;
5366 /* Check that the final columns of "M", starting at "first", are zero.
5368 static isl_stat check_final_columns_are_zero(__isl_keep isl_mat *M,
5369 unsigned first)
5371 int i;
5372 unsigned rows, cols, n;
5374 if (!M)
5375 return isl_stat_error;
5376 rows = isl_mat_rows(M);
5377 cols = isl_mat_cols(M);
5378 n = cols - first;
5379 for (i = 0; i < rows; ++i)
5380 if (isl_seq_first_non_zero(M->row[i] + first, n) != -1)
5381 isl_die(isl_mat_get_ctx(M), isl_error_internal,
5382 "final columns should be zero",
5383 return isl_stat_error);
5384 return isl_stat_ok;
5387 /* Set the affine expressions in "ma" according to the rows in "M", which
5388 * are defined over the local space "ls".
5389 * The matrix "M" may have extra (zero) columns beyond the number
5390 * of variables in "ls".
5392 static __isl_give isl_multi_aff *set_from_affine_matrix(
5393 __isl_take isl_multi_aff *ma, __isl_take isl_local_space *ls,
5394 __isl_take isl_mat *M)
5396 int i, dim;
5397 isl_aff *aff;
5399 if (!ma || !ls || !M)
5400 goto error;
5402 dim = isl_local_space_dim(ls, isl_dim_all);
5403 if (check_final_columns_are_zero(M, 1 + dim) < 0)
5404 goto error;
5405 for (i = 1; i < M->n_row; ++i) {
5406 aff = isl_aff_alloc(isl_local_space_copy(ls));
5407 if (aff) {
5408 isl_int_set(aff->v->el[0], M->row[0][0]);
5409 isl_seq_cpy(aff->v->el + 1, M->row[i], 1 + dim);
5411 aff = isl_aff_normalize(aff);
5412 ma = isl_multi_aff_set_aff(ma, i - 1, aff);
5414 isl_local_space_free(ls);
5415 isl_mat_free(M);
5417 return ma;
5418 error:
5419 isl_local_space_free(ls);
5420 isl_mat_free(M);
5421 isl_multi_aff_free(ma);
5422 return NULL;
5425 /* Given a basic set "dom" that represents the context and an affine
5426 * matrix "M" that maps the dimensions of the context to the
5427 * output variables, construct an isl_pw_multi_aff with a single
5428 * cell corresponding to "dom" and affine expressions copied from "M".
5430 * Note that the description of the initial context may have involved
5431 * existentially quantified variables, in which case they also appear
5432 * in "dom". These need to be removed before creating the affine
5433 * expression because an affine expression cannot be defined in terms
5434 * of existentially quantified variables without a known representation.
5435 * Since newly added integer divisions are inserted before these
5436 * existentially quantified variables, they are still in the final
5437 * positions and the corresponding final columns of "M" are zero
5438 * because align_context_divs adds the existentially quantified
5439 * variables of the context to the main tableau without any constraints and
5440 * any equality constraints that are added later on can only serve
5441 * to eliminate these existentially quantified variables.
5443 static void sol_pma_add(struct isl_sol_pma *sol,
5444 __isl_take isl_basic_set *dom, __isl_take isl_mat *M)
5446 isl_local_space *ls;
5447 isl_multi_aff *maff;
5448 isl_pw_multi_aff *pma;
5449 int n_div, n_known;
5451 n_div = isl_basic_set_dim(dom, isl_dim_div);
5452 n_known = n_div - sol->sol.context->n_unknown;
5454 maff = isl_multi_aff_alloc(isl_pw_multi_aff_get_space(sol->pma));
5455 ls = isl_basic_set_get_local_space(dom);
5456 ls = isl_local_space_drop_dims(ls, isl_dim_div,
5457 n_known, n_div - n_known);
5458 maff = set_from_affine_matrix(maff, ls, M);
5459 dom = isl_basic_set_simplify(dom);
5460 dom = isl_basic_set_finalize(dom);
5461 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(dom), maff);
5462 sol->pma = isl_pw_multi_aff_add_disjoint(sol->pma, pma);
5463 if (!sol->pma)
5464 sol->sol.error = 1;
5467 static void sol_pma_free_wrap(struct isl_sol *sol)
5469 sol_pma_free((struct isl_sol_pma *)sol);
5472 static void sol_pma_add_empty_wrap(struct isl_sol *sol,
5473 __isl_take isl_basic_set *bset)
5475 sol_pma_add_empty((struct isl_sol_pma *)sol, bset);
5478 static void sol_pma_add_wrap(struct isl_sol *sol,
5479 __isl_take isl_basic_set *dom, __isl_take isl_mat *M)
5481 sol_pma_add((struct isl_sol_pma *)sol, dom, M);
5484 /* Construct an isl_sol_pma structure for accumulating the solution.
5485 * If track_empty is set, then we also keep track of the parts
5486 * of the context where there is no solution.
5487 * If max is set, then we are solving a maximization, rather than
5488 * a minimization problem, which means that the variables in the
5489 * tableau have value "M - x" rather than "M + x".
5491 static struct isl_sol *sol_pma_init(__isl_keep isl_basic_map *bmap,
5492 __isl_take isl_basic_set *dom, int track_empty, int max)
5494 struct isl_sol_pma *sol_pma = NULL;
5496 if (!bmap)
5497 goto error;
5499 sol_pma = isl_calloc_type(bmap->ctx, struct isl_sol_pma);
5500 if (!sol_pma)
5501 goto error;
5503 sol_pma->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
5504 sol_pma->sol.dec_level.callback.run = &sol_dec_level_wrap;
5505 sol_pma->sol.dec_level.sol = &sol_pma->sol;
5506 sol_pma->sol.max = max;
5507 sol_pma->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
5508 sol_pma->sol.add = &sol_pma_add_wrap;
5509 sol_pma->sol.add_empty = track_empty ? &sol_pma_add_empty_wrap : NULL;
5510 sol_pma->sol.free = &sol_pma_free_wrap;
5511 sol_pma->pma = isl_pw_multi_aff_empty(isl_basic_map_get_space(bmap));
5512 if (!sol_pma->pma)
5513 goto error;
5515 sol_pma->sol.context = isl_context_alloc(dom);
5516 if (!sol_pma->sol.context)
5517 goto error;
5519 if (track_empty) {
5520 sol_pma->empty = isl_set_alloc_space(isl_basic_set_get_space(dom),
5521 1, ISL_SET_DISJOINT);
5522 if (!sol_pma->empty)
5523 goto error;
5526 isl_basic_set_free(dom);
5527 return &sol_pma->sol;
5528 error:
5529 isl_basic_set_free(dom);
5530 sol_pma_free(sol_pma);
5531 return NULL;
5534 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
5535 * some obvious symmetries.
5537 * We call basic_map_partial_lexopt_base_sol and extract the results.
5539 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_base_pw_multi_aff(
5540 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5541 __isl_give isl_set **empty, int max)
5543 isl_pw_multi_aff *result = NULL;
5544 struct isl_sol *sol;
5545 struct isl_sol_pma *sol_pma;
5547 sol = basic_map_partial_lexopt_base_sol(bmap, dom, empty, max,
5548 &sol_pma_init);
5549 if (!sol)
5550 return NULL;
5551 sol_pma = (struct isl_sol_pma *) sol;
5553 result = isl_pw_multi_aff_copy(sol_pma->pma);
5554 if (empty)
5555 *empty = isl_set_copy(sol_pma->empty);
5556 sol_free(&sol_pma->sol);
5557 return result;
5560 /* Given that the last input variable of "maff" represents the minimum
5561 * of some bounds, check whether we need to plug in the expression
5562 * of the minimum.
5564 * In particular, check if the last input variable appears in any
5565 * of the expressions in "maff".
5567 static int need_substitution(__isl_keep isl_multi_aff *maff)
5569 int i;
5570 unsigned pos;
5572 pos = isl_multi_aff_dim(maff, isl_dim_in) - 1;
5574 for (i = 0; i < maff->n; ++i)
5575 if (isl_aff_involves_dims(maff->p[i], isl_dim_in, pos, 1))
5576 return 1;
5578 return 0;
5581 /* Given a set of upper bounds on the last "input" variable m,
5582 * construct a piecewise affine expression that selects
5583 * the minimal upper bound to m, i.e.,
5584 * divide the space into cells where one
5585 * of the upper bounds is smaller than all the others and select
5586 * this upper bound on that cell.
5588 * In particular, if there are n bounds b_i, then the result
5589 * consists of n cell, each one of the form
5591 * b_i <= b_j for j > i
5592 * b_i < b_j for j < i
5594 * The affine expression on this cell is
5596 * b_i
5598 static __isl_give isl_pw_aff *set_minimum_pa(__isl_take isl_space *space,
5599 __isl_take isl_mat *var)
5601 int i;
5602 isl_aff *aff = NULL;
5603 isl_basic_set *bset = NULL;
5604 isl_pw_aff *paff = NULL;
5605 isl_space *pw_space;
5606 isl_local_space *ls = NULL;
5608 if (!space || !var)
5609 goto error;
5611 ls = isl_local_space_from_space(isl_space_copy(space));
5612 pw_space = isl_space_copy(space);
5613 pw_space = isl_space_from_domain(pw_space);
5614 pw_space = isl_space_add_dims(pw_space, isl_dim_out, 1);
5615 paff = isl_pw_aff_alloc_size(pw_space, var->n_row);
5617 for (i = 0; i < var->n_row; ++i) {
5618 isl_pw_aff *paff_i;
5620 aff = isl_aff_alloc(isl_local_space_copy(ls));
5621 bset = isl_basic_set_alloc_space(isl_space_copy(space), 0,
5622 0, var->n_row - 1);
5623 if (!aff || !bset)
5624 goto error;
5625 isl_int_set_si(aff->v->el[0], 1);
5626 isl_seq_cpy(aff->v->el + 1, var->row[i], var->n_col);
5627 isl_int_set_si(aff->v->el[1 + var->n_col], 0);
5628 bset = select_minimum(bset, var, i);
5629 paff_i = isl_pw_aff_alloc(isl_set_from_basic_set(bset), aff);
5630 paff = isl_pw_aff_add_disjoint(paff, paff_i);
5633 isl_local_space_free(ls);
5634 isl_space_free(space);
5635 isl_mat_free(var);
5636 return paff;
5637 error:
5638 isl_aff_free(aff);
5639 isl_basic_set_free(bset);
5640 isl_pw_aff_free(paff);
5641 isl_local_space_free(ls);
5642 isl_space_free(space);
5643 isl_mat_free(var);
5644 return NULL;
5647 /* Given a piecewise multi-affine expression of which the last input variable
5648 * is the minimum of the bounds in "cst", plug in the value of the minimum.
5649 * This minimum expression is given in "min_expr_pa".
5650 * The set "min_expr" contains the same information, but in the form of a set.
5651 * The variable is subsequently projected out.
5653 * The implementation is similar to those of "split" and "split_domain".
5654 * If the variable appears in a given expression, then minimum expression
5655 * is plugged in. Otherwise, if the variable appears in the constraints
5656 * and a split is required, then the domain is split. Otherwise, no split
5657 * is performed.
5659 static __isl_give isl_pw_multi_aff *split_domain_pma(
5660 __isl_take isl_pw_multi_aff *opt, __isl_take isl_pw_aff *min_expr_pa,
5661 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
5663 int n_in;
5664 int i;
5665 isl_space *space;
5666 isl_pw_multi_aff *res;
5668 if (!opt || !min_expr || !cst)
5669 goto error;
5671 n_in = isl_pw_multi_aff_dim(opt, isl_dim_in);
5672 space = isl_pw_multi_aff_get_space(opt);
5673 space = isl_space_drop_dims(space, isl_dim_in, n_in - 1, 1);
5674 res = isl_pw_multi_aff_empty(space);
5676 for (i = 0; i < opt->n; ++i) {
5677 isl_pw_multi_aff *pma;
5679 pma = isl_pw_multi_aff_alloc(isl_set_copy(opt->p[i].set),
5680 isl_multi_aff_copy(opt->p[i].maff));
5681 if (need_substitution(opt->p[i].maff))
5682 pma = isl_pw_multi_aff_substitute(pma,
5683 isl_dim_in, n_in - 1, min_expr_pa);
5684 else if (need_split_set(opt->p[i].set, cst))
5685 pma = isl_pw_multi_aff_intersect_domain(pma,
5686 isl_set_copy(min_expr));
5687 pma = isl_pw_multi_aff_project_out(pma,
5688 isl_dim_in, n_in - 1, 1);
5690 res = isl_pw_multi_aff_add_disjoint(res, pma);
5693 isl_pw_multi_aff_free(opt);
5694 isl_pw_aff_free(min_expr_pa);
5695 isl_set_free(min_expr);
5696 isl_mat_free(cst);
5697 return res;
5698 error:
5699 isl_pw_multi_aff_free(opt);
5700 isl_pw_aff_free(min_expr_pa);
5701 isl_set_free(min_expr);
5702 isl_mat_free(cst);
5703 return NULL;
5706 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_pw_multi_aff(
5707 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5708 __isl_give isl_set **empty, int max);
5710 /* This function is called from basic_map_partial_lexopt_symm.
5711 * The last variable of "bmap" and "dom" corresponds to the minimum
5712 * of the bounds in "cst". "map_space" is the space of the original
5713 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
5714 * is the space of the original domain.
5716 * We recursively call basic_map_partial_lexopt and then plug in
5717 * the definition of the minimum in the result.
5719 static __isl_give isl_pw_multi_aff *
5720 basic_map_partial_lexopt_symm_core_pw_multi_aff(
5721 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5722 __isl_give isl_set **empty, int max, __isl_take isl_mat *cst,
5723 __isl_take isl_space *map_space, __isl_take isl_space *set_space)
5725 isl_pw_multi_aff *opt;
5726 isl_pw_aff *min_expr_pa;
5727 isl_set *min_expr;
5729 min_expr = set_minimum(isl_basic_set_get_space(dom), isl_mat_copy(cst));
5730 min_expr_pa = set_minimum_pa(isl_basic_set_get_space(dom),
5731 isl_mat_copy(cst));
5733 opt = basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, max);
5735 if (empty) {
5736 *empty = split(*empty,
5737 isl_set_copy(min_expr), isl_mat_copy(cst));
5738 *empty = isl_set_reset_space(*empty, set_space);
5741 opt = split_domain_pma(opt, min_expr_pa, min_expr, cst);
5742 opt = isl_pw_multi_aff_reset_space(opt, map_space);
5744 return opt;
5747 #undef TYPE
5748 #define TYPE isl_pw_multi_aff
5749 #undef SUFFIX
5750 #define SUFFIX _pw_multi_aff
5751 #include "isl_tab_lexopt_templ.c"