isl_tab_pip.c: same_solution: return isl_bool
[isl.git] / isl_tab_pip.c
blob2fb9267e9bb50441b45c43e348fb7c80906829b5
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 __isl_take isl_basic_set *dom, __isl_take 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 __isl_take isl_basic_set *dom, __isl_take 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 isl_bool same_solution(struct isl_partial_sol *s1,
267 struct isl_partial_sol *s2, unsigned n_div)
269 int i;
270 unsigned dim;
272 if (!s1->M != !s2->M)
273 return isl_bool_false;
274 if (!s1->M)
275 return isl_bool_true;
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 isl_bool_false;
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 isl_bool_false;
286 if (!isl_seq_eq(s1->M->row[i], s2->M->row[i], 1+dim+n_div))
287 return isl_bool_false;
289 return isl_bool_true;
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 isl_bool same;
324 n_div = isl_basic_set_dim(
325 sol->context->op->peek_basic_set(sol->context),
326 isl_dim_div);
328 same = same_solution(partial, partial->next, n_div);
329 if (same < 0)
330 goto error;
331 if (!same) {
332 sol_pop_one(sol);
333 sol_pop_one(sol);
334 } else {
335 struct isl_basic_set *bset;
336 isl_mat *M;
337 unsigned n;
339 n = isl_basic_set_dim(partial->next->dom, isl_dim_div);
340 n -= n_div;
341 bset = sol_domain(sol);
342 isl_basic_set_free(partial->next->dom);
343 partial->next->dom = bset;
344 M = partial->next->M;
345 if (M) {
346 M = isl_mat_drop_cols(M, M->n_col - n, n);
347 partial->next->M = M;
348 if (!M)
349 goto error;
351 partial->next->level = sol->level;
353 if (!bset)
354 goto error;
356 sol->partial = partial->next;
357 isl_basic_set_free(partial->dom);
358 isl_mat_free(partial->M);
359 free(partial);
361 } else
362 sol_pop_one(sol);
364 if (sol->level == 0) {
365 for (partial = sol->partial; partial; partial = sol->partial)
366 sol_pop_one(sol);
367 return;
370 if (0)
371 error: sol->error = 1;
374 static void sol_dec_level(struct isl_sol *sol)
376 if (sol->error)
377 return;
379 sol->level--;
381 sol_pop(sol);
384 static int sol_dec_level_wrap(struct isl_tab_callback *cb)
386 struct isl_sol_callback *callback = (struct isl_sol_callback *)cb;
388 sol_dec_level(callback->sol);
390 return callback->sol->error ? -1 : 0;
393 /* Move down to next level and push callback onto context tableau
394 * to decrease the level again when it gets rolled back across
395 * the current state. That is, dec_level will be called with
396 * the context tableau in the same state as it is when inc_level
397 * is called.
399 static void sol_inc_level(struct isl_sol *sol)
401 struct isl_tab *tab;
403 if (sol->error)
404 return;
406 sol->level++;
407 tab = sol->context->op->peek_tab(sol->context);
408 if (isl_tab_push_callback(tab, &sol->dec_level.callback) < 0)
409 sol->error = 1;
412 static void scale_rows(struct isl_mat *mat, isl_int m, int n_row)
414 int i;
416 if (isl_int_is_one(m))
417 return;
419 for (i = 0; i < n_row; ++i)
420 isl_seq_scale(mat->row[i], mat->row[i], m, mat->n_col);
423 /* Add the solution identified by the tableau and the context tableau.
425 * The layout of the variables is as follows.
426 * tab->n_var is equal to the total number of variables in the input
427 * map (including divs that were copied from the context)
428 * + the number of extra divs constructed
429 * Of these, the first tab->n_param and the last tab->n_div variables
430 * correspond to the variables in the context, i.e.,
431 * tab->n_param + tab->n_div = context_tab->n_var
432 * tab->n_param is equal to the number of parameters and input
433 * dimensions in the input map
434 * tab->n_div is equal to the number of divs in the context
436 * If there is no solution, then call add_empty with a basic set
437 * that corresponds to the context tableau. (If add_empty is NULL,
438 * then do nothing).
440 * If there is a solution, then first construct a matrix that maps
441 * all dimensions of the context to the output variables, i.e.,
442 * the output dimensions in the input map.
443 * The divs in the input map (if any) that do not correspond to any
444 * div in the context do not appear in the solution.
445 * The algorithm will make sure that they have an integer value,
446 * but these values themselves are of no interest.
447 * We have to be careful not to drop or rearrange any divs in the
448 * context because that would change the meaning of the matrix.
450 * To extract the value of the output variables, it should be noted
451 * that we always use a big parameter M in the main tableau and so
452 * the variable stored in this tableau is not an output variable x itself, but
453 * x' = M + x (in case of minimization)
454 * or
455 * x' = M - x (in case of maximization)
456 * If x' appears in a column, then its optimal value is zero,
457 * which means that the optimal value of x is an unbounded number
458 * (-M for minimization and M for maximization).
459 * We currently assume that the output dimensions in the original map
460 * are bounded, so this cannot occur.
461 * Similarly, when x' appears in a row, then the coefficient of M in that
462 * row is necessarily 1.
463 * If the row in the tableau represents
464 * d x' = c + d M + e(y)
465 * then, in case of minimization, the corresponding row in the matrix
466 * will be
467 * a c + a e(y)
468 * with a d = m, the (updated) common denominator of the matrix.
469 * In case of maximization, the row will be
470 * -a c - a e(y)
472 static void sol_add(struct isl_sol *sol, struct isl_tab *tab)
474 struct isl_basic_set *bset = NULL;
475 struct isl_mat *mat = NULL;
476 unsigned off;
477 int row;
478 isl_int m;
480 if (sol->error || !tab)
481 goto error;
483 if (tab->empty && !sol->add_empty)
484 return;
485 if (sol->context->op->is_empty(sol->context))
486 return;
488 bset = sol_domain(sol);
490 if (tab->empty) {
491 sol_push_sol(sol, bset, NULL);
492 return;
495 off = 2 + tab->M;
497 mat = isl_mat_alloc(tab->mat->ctx, 1 + sol->n_out,
498 1 + tab->n_param + tab->n_div);
499 if (!mat)
500 goto error;
502 isl_int_init(m);
504 isl_seq_clr(mat->row[0] + 1, mat->n_col - 1);
505 isl_int_set_si(mat->row[0][0], 1);
506 for (row = 0; row < sol->n_out; ++row) {
507 int i = tab->n_param + row;
508 int r, j;
510 isl_seq_clr(mat->row[1 + row], mat->n_col);
511 if (!tab->var[i].is_row) {
512 if (tab->M)
513 isl_die(mat->ctx, isl_error_invalid,
514 "unbounded optimum", goto error2);
515 continue;
518 r = tab->var[i].index;
519 if (tab->M &&
520 isl_int_ne(tab->mat->row[r][2], tab->mat->row[r][0]))
521 isl_die(mat->ctx, isl_error_invalid,
522 "unbounded optimum", goto error2);
523 isl_int_gcd(m, mat->row[0][0], tab->mat->row[r][0]);
524 isl_int_divexact(m, tab->mat->row[r][0], m);
525 scale_rows(mat, m, 1 + row);
526 isl_int_divexact(m, mat->row[0][0], tab->mat->row[r][0]);
527 isl_int_mul(mat->row[1 + row][0], m, tab->mat->row[r][1]);
528 for (j = 0; j < tab->n_param; ++j) {
529 int col;
530 if (tab->var[j].is_row)
531 continue;
532 col = tab->var[j].index;
533 isl_int_mul(mat->row[1 + row][1 + j], m,
534 tab->mat->row[r][off + col]);
536 for (j = 0; j < tab->n_div; ++j) {
537 int col;
538 if (tab->var[tab->n_var - tab->n_div+j].is_row)
539 continue;
540 col = tab->var[tab->n_var - tab->n_div+j].index;
541 isl_int_mul(mat->row[1 + row][1 + tab->n_param + j], m,
542 tab->mat->row[r][off + col]);
544 if (sol->max)
545 isl_seq_neg(mat->row[1 + row], mat->row[1 + row],
546 mat->n_col);
549 isl_int_clear(m);
551 sol_push_sol(sol, bset, mat);
552 return;
553 error2:
554 isl_int_clear(m);
555 error:
556 isl_basic_set_free(bset);
557 isl_mat_free(mat);
558 sol->error = 1;
561 struct isl_sol_map {
562 struct isl_sol sol;
563 struct isl_map *map;
564 struct isl_set *empty;
567 static void sol_map_free(struct isl_sol_map *sol_map)
569 if (!sol_map)
570 return;
571 if (sol_map->sol.context)
572 sol_map->sol.context->op->free(sol_map->sol.context);
573 isl_map_free(sol_map->map);
574 isl_set_free(sol_map->empty);
575 free(sol_map);
578 static void sol_map_free_wrap(struct isl_sol *sol)
580 sol_map_free((struct isl_sol_map *)sol);
583 /* This function is called for parts of the context where there is
584 * no solution, with "bset" corresponding to the context tableau.
585 * Simply add the basic set to the set "empty".
587 static void sol_map_add_empty(struct isl_sol_map *sol,
588 struct isl_basic_set *bset)
590 if (!bset || !sol->empty)
591 goto error;
593 sol->empty = isl_set_grow(sol->empty, 1);
594 bset = isl_basic_set_simplify(bset);
595 bset = isl_basic_set_finalize(bset);
596 sol->empty = isl_set_add_basic_set(sol->empty, isl_basic_set_copy(bset));
597 if (!sol->empty)
598 goto error;
599 isl_basic_set_free(bset);
600 return;
601 error:
602 isl_basic_set_free(bset);
603 sol->sol.error = 1;
606 static void sol_map_add_empty_wrap(struct isl_sol *sol,
607 struct isl_basic_set *bset)
609 sol_map_add_empty((struct isl_sol_map *)sol, bset);
612 /* Given a basic set "dom" that represents the context and an affine
613 * matrix "M" that maps the dimensions of the context to the
614 * output variables, construct a basic map with the same parameters
615 * and divs as the context, the dimensions of the context as input
616 * dimensions and a number of output dimensions that is equal to
617 * the number of output dimensions in the input map.
619 * The constraints and divs of the context are simply copied
620 * from "dom". For each row
621 * x = c + e(y)
622 * an equality
623 * c + e(y) - d x = 0
624 * is added, with d the common denominator of M.
626 static void sol_map_add(struct isl_sol_map *sol,
627 __isl_take isl_basic_set *dom, __isl_take isl_mat *M)
629 int i;
630 struct isl_basic_map *bmap = NULL;
631 unsigned n_eq;
632 unsigned n_ineq;
633 unsigned nparam;
634 unsigned total;
635 unsigned n_div;
636 unsigned n_out;
638 if (sol->sol.error || !dom || !M)
639 goto error;
641 n_out = sol->sol.n_out;
642 n_eq = dom->n_eq + n_out;
643 n_ineq = dom->n_ineq;
644 n_div = dom->n_div;
645 nparam = isl_basic_set_total_dim(dom) - n_div;
646 total = isl_map_dim(sol->map, isl_dim_all);
647 bmap = isl_basic_map_alloc_space(isl_map_get_space(sol->map),
648 n_div, n_eq, 2 * n_div + n_ineq);
649 if (!bmap)
650 goto error;
651 if (sol->sol.rational)
652 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
653 for (i = 0; i < dom->n_div; ++i) {
654 int k = isl_basic_map_alloc_div(bmap);
655 if (k < 0)
656 goto error;
657 isl_seq_cpy(bmap->div[k], dom->div[i], 1 + 1 + nparam);
658 isl_seq_clr(bmap->div[k] + 1 + 1 + nparam, total - nparam);
659 isl_seq_cpy(bmap->div[k] + 1 + 1 + total,
660 dom->div[i] + 1 + 1 + nparam, i);
662 for (i = 0; i < dom->n_eq; ++i) {
663 int k = isl_basic_map_alloc_equality(bmap);
664 if (k < 0)
665 goto error;
666 isl_seq_cpy(bmap->eq[k], dom->eq[i], 1 + nparam);
667 isl_seq_clr(bmap->eq[k] + 1 + nparam, total - nparam);
668 isl_seq_cpy(bmap->eq[k] + 1 + total,
669 dom->eq[i] + 1 + nparam, n_div);
671 for (i = 0; i < dom->n_ineq; ++i) {
672 int k = isl_basic_map_alloc_inequality(bmap);
673 if (k < 0)
674 goto error;
675 isl_seq_cpy(bmap->ineq[k], dom->ineq[i], 1 + nparam);
676 isl_seq_clr(bmap->ineq[k] + 1 + nparam, total - nparam);
677 isl_seq_cpy(bmap->ineq[k] + 1 + total,
678 dom->ineq[i] + 1 + nparam, n_div);
680 for (i = 0; i < M->n_row - 1; ++i) {
681 int k = isl_basic_map_alloc_equality(bmap);
682 if (k < 0)
683 goto error;
684 isl_seq_cpy(bmap->eq[k], M->row[1 + i], 1 + nparam);
685 isl_seq_clr(bmap->eq[k] + 1 + nparam, n_out);
686 isl_int_neg(bmap->eq[k][1 + nparam + i], M->row[0][0]);
687 isl_seq_cpy(bmap->eq[k] + 1 + nparam + n_out,
688 M->row[1 + i] + 1 + nparam, n_div);
690 bmap = isl_basic_map_simplify(bmap);
691 bmap = isl_basic_map_finalize(bmap);
692 sol->map = isl_map_grow(sol->map, 1);
693 sol->map = isl_map_add_basic_map(sol->map, bmap);
694 isl_basic_set_free(dom);
695 isl_mat_free(M);
696 if (!sol->map)
697 sol->sol.error = 1;
698 return;
699 error:
700 isl_basic_set_free(dom);
701 isl_mat_free(M);
702 isl_basic_map_free(bmap);
703 sol->sol.error = 1;
706 static void sol_map_add_wrap(struct isl_sol *sol,
707 __isl_take isl_basic_set *dom, __isl_take isl_mat *M)
709 sol_map_add((struct isl_sol_map *)sol, dom, M);
713 /* Store the "parametric constant" of row "row" of tableau "tab" in "line",
714 * i.e., the constant term and the coefficients of all variables that
715 * appear in the context tableau.
716 * Note that the coefficient of the big parameter M is NOT copied.
717 * The context tableau may not have a big parameter and even when it
718 * does, it is a different big parameter.
720 static void get_row_parameter_line(struct isl_tab *tab, int row, isl_int *line)
722 int i;
723 unsigned off = 2 + tab->M;
725 isl_int_set(line[0], tab->mat->row[row][1]);
726 for (i = 0; i < tab->n_param; ++i) {
727 if (tab->var[i].is_row)
728 isl_int_set_si(line[1 + i], 0);
729 else {
730 int col = tab->var[i].index;
731 isl_int_set(line[1 + i], tab->mat->row[row][off + col]);
734 for (i = 0; i < tab->n_div; ++i) {
735 if (tab->var[tab->n_var - tab->n_div + i].is_row)
736 isl_int_set_si(line[1 + tab->n_param + i], 0);
737 else {
738 int col = tab->var[tab->n_var - tab->n_div + i].index;
739 isl_int_set(line[1 + tab->n_param + i],
740 tab->mat->row[row][off + col]);
745 /* Check if rows "row1" and "row2" have identical "parametric constants",
746 * as explained above.
747 * In this case, we also insist that the coefficients of the big parameter
748 * be the same as the values of the constants will only be the same
749 * if these coefficients are also the same.
751 static int identical_parameter_line(struct isl_tab *tab, int row1, int row2)
753 int i;
754 unsigned off = 2 + tab->M;
756 if (isl_int_ne(tab->mat->row[row1][1], tab->mat->row[row2][1]))
757 return 0;
759 if (tab->M && isl_int_ne(tab->mat->row[row1][2],
760 tab->mat->row[row2][2]))
761 return 0;
763 for (i = 0; i < tab->n_param + tab->n_div; ++i) {
764 int pos = i < tab->n_param ? i :
765 tab->n_var - tab->n_div + i - tab->n_param;
766 int col;
768 if (tab->var[pos].is_row)
769 continue;
770 col = tab->var[pos].index;
771 if (isl_int_ne(tab->mat->row[row1][off + col],
772 tab->mat->row[row2][off + col]))
773 return 0;
775 return 1;
778 /* Return an inequality that expresses that the "parametric constant"
779 * should be non-negative.
780 * This function is only called when the coefficient of the big parameter
781 * is equal to zero.
783 static struct isl_vec *get_row_parameter_ineq(struct isl_tab *tab, int row)
785 struct isl_vec *ineq;
787 ineq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_param + tab->n_div);
788 if (!ineq)
789 return NULL;
791 get_row_parameter_line(tab, row, ineq->el);
792 if (ineq)
793 ineq = isl_vec_normalize(ineq);
795 return ineq;
798 /* Normalize a div expression of the form
800 * [(g*f(x) + c)/(g * m)]
802 * with c the constant term and f(x) the remaining coefficients, to
804 * [(f(x) + [c/g])/m]
806 static void normalize_div(__isl_keep isl_vec *div)
808 isl_ctx *ctx = isl_vec_get_ctx(div);
809 int len = div->size - 2;
811 isl_seq_gcd(div->el + 2, len, &ctx->normalize_gcd);
812 isl_int_gcd(ctx->normalize_gcd, ctx->normalize_gcd, div->el[0]);
814 if (isl_int_is_one(ctx->normalize_gcd))
815 return;
817 isl_int_divexact(div->el[0], div->el[0], ctx->normalize_gcd);
818 isl_int_fdiv_q(div->el[1], div->el[1], ctx->normalize_gcd);
819 isl_seq_scale_down(div->el + 2, div->el + 2, ctx->normalize_gcd, len);
822 /* Return an integer division for use in a parametric cut based
823 * on the given row.
824 * In particular, let the parametric constant of the row be
826 * \sum_i a_i y_i
828 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
829 * The div returned is equal to
831 * floor(\sum_i {-a_i} y_i) = floor((\sum_i (-a_i mod d) y_i)/d)
833 static struct isl_vec *get_row_parameter_div(struct isl_tab *tab, int row)
835 struct isl_vec *div;
837 div = isl_vec_alloc(tab->mat->ctx, 1 + 1 + tab->n_param + tab->n_div);
838 if (!div)
839 return NULL;
841 isl_int_set(div->el[0], tab->mat->row[row][0]);
842 get_row_parameter_line(tab, row, div->el + 1);
843 isl_seq_neg(div->el + 1, div->el + 1, div->size - 1);
844 normalize_div(div);
845 isl_seq_fdiv_r(div->el + 1, div->el + 1, div->el[0], div->size - 1);
847 return div;
850 /* Return an integer division for use in transferring an integrality constraint
851 * to the context.
852 * In particular, let the parametric constant of the row be
854 * \sum_i a_i y_i
856 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
857 * The the returned div is equal to
859 * floor(\sum_i {a_i} y_i) = floor((\sum_i (a_i mod d) y_i)/d)
861 static struct isl_vec *get_row_split_div(struct isl_tab *tab, int row)
863 struct isl_vec *div;
865 div = isl_vec_alloc(tab->mat->ctx, 1 + 1 + tab->n_param + tab->n_div);
866 if (!div)
867 return NULL;
869 isl_int_set(div->el[0], tab->mat->row[row][0]);
870 get_row_parameter_line(tab, row, div->el + 1);
871 normalize_div(div);
872 isl_seq_fdiv_r(div->el + 1, div->el + 1, div->el[0], div->size - 1);
874 return div;
877 /* Construct and return an inequality that expresses an upper bound
878 * on the given div.
879 * In particular, if the div is given by
881 * d = floor(e/m)
883 * then the inequality expresses
885 * m d <= e
887 static struct isl_vec *ineq_for_div(struct isl_basic_set *bset, unsigned div)
889 unsigned total;
890 unsigned div_pos;
891 struct isl_vec *ineq;
893 if (!bset)
894 return NULL;
896 total = isl_basic_set_total_dim(bset);
897 div_pos = 1 + total - bset->n_div + div;
899 ineq = isl_vec_alloc(bset->ctx, 1 + total);
900 if (!ineq)
901 return NULL;
903 isl_seq_cpy(ineq->el, bset->div[div] + 1, 1 + total);
904 isl_int_neg(ineq->el[div_pos], bset->div[div][0]);
905 return ineq;
908 /* Given a row in the tableau and a div that was created
909 * using get_row_split_div and that has been constrained to equality, i.e.,
911 * d = floor(\sum_i {a_i} y_i) = \sum_i {a_i} y_i
913 * replace the expression "\sum_i {a_i} y_i" in the row by d,
914 * i.e., we subtract "\sum_i {a_i} y_i" and add 1 d.
915 * The coefficients of the non-parameters in the tableau have been
916 * verified to be integral. We can therefore simply replace coefficient b
917 * by floor(b). For the coefficients of the parameters we have
918 * floor(a_i) = a_i - {a_i}, while for the other coefficients, we have
919 * floor(b) = b.
921 static struct isl_tab *set_row_cst_to_div(struct isl_tab *tab, int row, int div)
923 isl_seq_fdiv_q(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
924 tab->mat->row[row][0], 1 + tab->M + tab->n_col);
926 isl_int_set_si(tab->mat->row[row][0], 1);
928 if (tab->var[tab->n_var - tab->n_div + div].is_row) {
929 int drow = tab->var[tab->n_var - tab->n_div + div].index;
931 isl_assert(tab->mat->ctx,
932 isl_int_is_one(tab->mat->row[drow][0]), goto error);
933 isl_seq_combine(tab->mat->row[row] + 1,
934 tab->mat->ctx->one, tab->mat->row[row] + 1,
935 tab->mat->ctx->one, tab->mat->row[drow] + 1,
936 1 + tab->M + tab->n_col);
937 } else {
938 int dcol = tab->var[tab->n_var - tab->n_div + div].index;
940 isl_int_add_ui(tab->mat->row[row][2 + tab->M + dcol],
941 tab->mat->row[row][2 + tab->M + dcol], 1);
944 return tab;
945 error:
946 isl_tab_free(tab);
947 return NULL;
950 /* Check if the (parametric) constant of the given row is obviously
951 * negative, meaning that we don't need to consult the context tableau.
952 * If there is a big parameter and its coefficient is non-zero,
953 * then this coefficient determines the outcome.
954 * Otherwise, we check whether the constant is negative and
955 * all non-zero coefficients of parameters are negative and
956 * belong to non-negative parameters.
958 static int is_obviously_neg(struct isl_tab *tab, int row)
960 int i;
961 int col;
962 unsigned off = 2 + tab->M;
964 if (tab->M) {
965 if (isl_int_is_pos(tab->mat->row[row][2]))
966 return 0;
967 if (isl_int_is_neg(tab->mat->row[row][2]))
968 return 1;
971 if (isl_int_is_nonneg(tab->mat->row[row][1]))
972 return 0;
973 for (i = 0; i < tab->n_param; ++i) {
974 /* Eliminated parameter */
975 if (tab->var[i].is_row)
976 continue;
977 col = tab->var[i].index;
978 if (isl_int_is_zero(tab->mat->row[row][off + col]))
979 continue;
980 if (!tab->var[i].is_nonneg)
981 return 0;
982 if (isl_int_is_pos(tab->mat->row[row][off + col]))
983 return 0;
985 for (i = 0; i < tab->n_div; ++i) {
986 if (tab->var[tab->n_var - tab->n_div + i].is_row)
987 continue;
988 col = tab->var[tab->n_var - tab->n_div + i].index;
989 if (isl_int_is_zero(tab->mat->row[row][off + col]))
990 continue;
991 if (!tab->var[tab->n_var - tab->n_div + i].is_nonneg)
992 return 0;
993 if (isl_int_is_pos(tab->mat->row[row][off + col]))
994 return 0;
996 return 1;
999 /* Check if the (parametric) constant of the given row is obviously
1000 * non-negative, meaning that we don't need to consult the context tableau.
1001 * If there is a big parameter and its coefficient is non-zero,
1002 * then this coefficient determines the outcome.
1003 * Otherwise, we check whether the constant is non-negative and
1004 * all non-zero coefficients of parameters are positive and
1005 * belong to non-negative parameters.
1007 static int is_obviously_nonneg(struct isl_tab *tab, int row)
1009 int i;
1010 int col;
1011 unsigned off = 2 + tab->M;
1013 if (tab->M) {
1014 if (isl_int_is_pos(tab->mat->row[row][2]))
1015 return 1;
1016 if (isl_int_is_neg(tab->mat->row[row][2]))
1017 return 0;
1020 if (isl_int_is_neg(tab->mat->row[row][1]))
1021 return 0;
1022 for (i = 0; i < tab->n_param; ++i) {
1023 /* Eliminated parameter */
1024 if (tab->var[i].is_row)
1025 continue;
1026 col = tab->var[i].index;
1027 if (isl_int_is_zero(tab->mat->row[row][off + col]))
1028 continue;
1029 if (!tab->var[i].is_nonneg)
1030 return 0;
1031 if (isl_int_is_neg(tab->mat->row[row][off + col]))
1032 return 0;
1034 for (i = 0; i < tab->n_div; ++i) {
1035 if (tab->var[tab->n_var - tab->n_div + i].is_row)
1036 continue;
1037 col = tab->var[tab->n_var - tab->n_div + i].index;
1038 if (isl_int_is_zero(tab->mat->row[row][off + col]))
1039 continue;
1040 if (!tab->var[tab->n_var - tab->n_div + i].is_nonneg)
1041 return 0;
1042 if (isl_int_is_neg(tab->mat->row[row][off + col]))
1043 return 0;
1045 return 1;
1048 /* Given a row r and two columns, return the column that would
1049 * lead to the lexicographically smallest increment in the sample
1050 * solution when leaving the basis in favor of the row.
1051 * Pivoting with column c will increment the sample value by a non-negative
1052 * constant times a_{V,c}/a_{r,c}, with a_{V,c} the elements of column c
1053 * corresponding to the non-parametric variables.
1054 * If variable v appears in a column c_v, the a_{v,c} = 1 iff c = c_v,
1055 * with all other entries in this virtual row equal to zero.
1056 * If variable v appears in a row, then a_{v,c} is the element in column c
1057 * of that row.
1059 * Let v be the first variable with a_{v,c1}/a_{r,c1} != a_{v,c2}/a_{r,c2}.
1060 * Then if a_{v,c1}/a_{r,c1} < a_{v,c2}/a_{r,c2}, i.e.,
1061 * a_{v,c2} a_{r,c1} - a_{v,c1} a_{r,c2} > 0, c1 results in the minimal
1062 * increment. Otherwise, it's c2.
1064 static int lexmin_col_pair(struct isl_tab *tab,
1065 int row, int col1, int col2, isl_int tmp)
1067 int i;
1068 isl_int *tr;
1070 tr = tab->mat->row[row] + 2 + tab->M;
1072 for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
1073 int s1, s2;
1074 isl_int *r;
1076 if (!tab->var[i].is_row) {
1077 if (tab->var[i].index == col1)
1078 return col2;
1079 if (tab->var[i].index == col2)
1080 return col1;
1081 continue;
1084 if (tab->var[i].index == row)
1085 continue;
1087 r = tab->mat->row[tab->var[i].index] + 2 + tab->M;
1088 s1 = isl_int_sgn(r[col1]);
1089 s2 = isl_int_sgn(r[col2]);
1090 if (s1 == 0 && s2 == 0)
1091 continue;
1092 if (s1 < s2)
1093 return col1;
1094 if (s2 < s1)
1095 return col2;
1097 isl_int_mul(tmp, r[col2], tr[col1]);
1098 isl_int_submul(tmp, r[col1], tr[col2]);
1099 if (isl_int_is_pos(tmp))
1100 return col1;
1101 if (isl_int_is_neg(tmp))
1102 return col2;
1104 return -1;
1107 /* Given a row in the tableau, find and return the column that would
1108 * result in the lexicographically smallest, but positive, increment
1109 * in the sample point.
1110 * If there is no such column, then return tab->n_col.
1111 * If anything goes wrong, return -1.
1113 static int lexmin_pivot_col(struct isl_tab *tab, int row)
1115 int j;
1116 int col = tab->n_col;
1117 isl_int *tr;
1118 isl_int tmp;
1120 tr = tab->mat->row[row] + 2 + tab->M;
1122 isl_int_init(tmp);
1124 for (j = tab->n_dead; j < tab->n_col; ++j) {
1125 if (tab->col_var[j] >= 0 &&
1126 (tab->col_var[j] < tab->n_param ||
1127 tab->col_var[j] >= tab->n_var - tab->n_div))
1128 continue;
1130 if (!isl_int_is_pos(tr[j]))
1131 continue;
1133 if (col == tab->n_col)
1134 col = j;
1135 else
1136 col = lexmin_col_pair(tab, row, col, j, tmp);
1137 isl_assert(tab->mat->ctx, col >= 0, goto error);
1140 isl_int_clear(tmp);
1141 return col;
1142 error:
1143 isl_int_clear(tmp);
1144 return -1;
1147 /* Return the first known violated constraint, i.e., a non-negative
1148 * constraint that currently has an either obviously negative value
1149 * or a previously determined to be negative value.
1151 * If any constraint has a negative coefficient for the big parameter,
1152 * if any, then we return one of these first.
1154 static int first_neg(struct isl_tab *tab)
1156 int row;
1158 if (tab->M)
1159 for (row = tab->n_redundant; row < tab->n_row; ++row) {
1160 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
1161 continue;
1162 if (!isl_int_is_neg(tab->mat->row[row][2]))
1163 continue;
1164 if (tab->row_sign)
1165 tab->row_sign[row] = isl_tab_row_neg;
1166 return row;
1168 for (row = tab->n_redundant; row < tab->n_row; ++row) {
1169 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
1170 continue;
1171 if (tab->row_sign) {
1172 if (tab->row_sign[row] == 0 &&
1173 is_obviously_neg(tab, row))
1174 tab->row_sign[row] = isl_tab_row_neg;
1175 if (tab->row_sign[row] != isl_tab_row_neg)
1176 continue;
1177 } else if (!is_obviously_neg(tab, row))
1178 continue;
1179 return row;
1181 return -1;
1184 /* Check whether the invariant that all columns are lexico-positive
1185 * is satisfied. This function is not called from the current code
1186 * but is useful during debugging.
1188 static void check_lexpos(struct isl_tab *tab) __attribute__ ((unused));
1189 static void check_lexpos(struct isl_tab *tab)
1191 unsigned off = 2 + tab->M;
1192 int col;
1193 int var;
1194 int row;
1196 for (col = tab->n_dead; col < tab->n_col; ++col) {
1197 if (tab->col_var[col] >= 0 &&
1198 (tab->col_var[col] < tab->n_param ||
1199 tab->col_var[col] >= tab->n_var - tab->n_div))
1200 continue;
1201 for (var = tab->n_param; var < tab->n_var - tab->n_div; ++var) {
1202 if (!tab->var[var].is_row) {
1203 if (tab->var[var].index == col)
1204 break;
1205 else
1206 continue;
1208 row = tab->var[var].index;
1209 if (isl_int_is_zero(tab->mat->row[row][off + col]))
1210 continue;
1211 if (isl_int_is_pos(tab->mat->row[row][off + col]))
1212 break;
1213 fprintf(stderr, "lexneg column %d (row %d)\n",
1214 col, row);
1216 if (var >= tab->n_var - tab->n_div)
1217 fprintf(stderr, "zero column %d\n", col);
1221 /* Report to the caller that the given constraint is part of an encountered
1222 * conflict.
1224 static int report_conflicting_constraint(struct isl_tab *tab, int con)
1226 return tab->conflict(con, tab->conflict_user);
1229 /* Given a conflicting row in the tableau, report all constraints
1230 * involved in the row to the caller. That is, the row itself
1231 * (if it represents a constraint) and all constraint columns with
1232 * non-zero (and therefore negative) coefficients.
1234 static int report_conflict(struct isl_tab *tab, int row)
1236 int j;
1237 isl_int *tr;
1239 if (!tab->conflict)
1240 return 0;
1242 if (tab->row_var[row] < 0 &&
1243 report_conflicting_constraint(tab, ~tab->row_var[row]) < 0)
1244 return -1;
1246 tr = tab->mat->row[row] + 2 + tab->M;
1248 for (j = tab->n_dead; j < tab->n_col; ++j) {
1249 if (tab->col_var[j] >= 0 &&
1250 (tab->col_var[j] < tab->n_param ||
1251 tab->col_var[j] >= tab->n_var - tab->n_div))
1252 continue;
1254 if (!isl_int_is_neg(tr[j]))
1255 continue;
1257 if (tab->col_var[j] < 0 &&
1258 report_conflicting_constraint(tab, ~tab->col_var[j]) < 0)
1259 return -1;
1262 return 0;
1265 /* Resolve all known or obviously violated constraints through pivoting.
1266 * In particular, as long as we can find any violated constraint, we
1267 * look for a pivoting column that would result in the lexicographically
1268 * smallest increment in the sample point. If there is no such column
1269 * then the tableau is infeasible.
1271 static int restore_lexmin(struct isl_tab *tab) WARN_UNUSED;
1272 static int restore_lexmin(struct isl_tab *tab)
1274 int row, col;
1276 if (!tab)
1277 return -1;
1278 if (tab->empty)
1279 return 0;
1280 while ((row = first_neg(tab)) != -1) {
1281 col = lexmin_pivot_col(tab, row);
1282 if (col >= tab->n_col) {
1283 if (report_conflict(tab, row) < 0)
1284 return -1;
1285 if (isl_tab_mark_empty(tab) < 0)
1286 return -1;
1287 return 0;
1289 if (col < 0)
1290 return -1;
1291 if (isl_tab_pivot(tab, row, col) < 0)
1292 return -1;
1294 return 0;
1297 /* Given a row that represents an equality, look for an appropriate
1298 * pivoting column.
1299 * In particular, if there are any non-zero coefficients among
1300 * the non-parameter variables, then we take the last of these
1301 * variables. Eliminating this variable in terms of the other
1302 * variables and/or parameters does not influence the property
1303 * that all column in the initial tableau are lexicographically
1304 * positive. The row corresponding to the eliminated variable
1305 * will only have non-zero entries below the diagonal of the
1306 * initial tableau. That is, we transform
1308 * I I
1309 * 1 into a
1310 * I I
1312 * If there is no such non-parameter variable, then we are dealing with
1313 * pure parameter equality and we pick any parameter with coefficient 1 or -1
1314 * for elimination. This will ensure that the eliminated parameter
1315 * always has an integer value whenever all the other parameters are integral.
1316 * If there is no such parameter then we return -1.
1318 static int last_var_col_or_int_par_col(struct isl_tab *tab, int row)
1320 unsigned off = 2 + tab->M;
1321 int i;
1323 for (i = tab->n_var - tab->n_div - 1; i >= 0 && i >= tab->n_param; --i) {
1324 int col;
1325 if (tab->var[i].is_row)
1326 continue;
1327 col = tab->var[i].index;
1328 if (col <= tab->n_dead)
1329 continue;
1330 if (!isl_int_is_zero(tab->mat->row[row][off + col]))
1331 return col;
1333 for (i = tab->n_dead; i < tab->n_col; ++i) {
1334 if (isl_int_is_one(tab->mat->row[row][off + i]))
1335 return i;
1336 if (isl_int_is_negone(tab->mat->row[row][off + i]))
1337 return i;
1339 return -1;
1342 /* Add an equality that is known to be valid to the tableau.
1343 * We first check if we can eliminate a variable or a parameter.
1344 * If not, we add the equality as two inequalities.
1345 * In this case, the equality was a pure parameter equality and there
1346 * is no need to resolve any constraint violations.
1348 * This function assumes that at least two more rows and at least
1349 * two more elements in the constraint array are available in the tableau.
1351 static struct isl_tab *add_lexmin_valid_eq(struct isl_tab *tab, isl_int *eq)
1353 int i;
1354 int r;
1356 if (!tab)
1357 return NULL;
1358 r = isl_tab_add_row(tab, eq);
1359 if (r < 0)
1360 goto error;
1362 r = tab->con[r].index;
1363 i = last_var_col_or_int_par_col(tab, r);
1364 if (i < 0) {
1365 tab->con[r].is_nonneg = 1;
1366 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1367 goto error;
1368 isl_seq_neg(eq, eq, 1 + tab->n_var);
1369 r = isl_tab_add_row(tab, eq);
1370 if (r < 0)
1371 goto error;
1372 tab->con[r].is_nonneg = 1;
1373 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1374 goto error;
1375 } else {
1376 if (isl_tab_pivot(tab, r, i) < 0)
1377 goto error;
1378 if (isl_tab_kill_col(tab, i) < 0)
1379 goto error;
1380 tab->n_eq++;
1383 return tab;
1384 error:
1385 isl_tab_free(tab);
1386 return NULL;
1389 /* Check if the given row is a pure constant.
1391 static int is_constant(struct isl_tab *tab, int row)
1393 unsigned off = 2 + tab->M;
1395 return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1396 tab->n_col - tab->n_dead) == -1;
1399 /* Add an equality that may or may not be valid to the tableau.
1400 * If the resulting row is a pure constant, then it must be zero.
1401 * Otherwise, the resulting tableau is empty.
1403 * If the row is not a pure constant, then we add two inequalities,
1404 * each time checking that they can be satisfied.
1405 * In the end we try to use one of the two constraints to eliminate
1406 * a column.
1408 * This function assumes that at least two more rows and at least
1409 * two more elements in the constraint array are available in the tableau.
1411 static int add_lexmin_eq(struct isl_tab *tab, isl_int *eq) WARN_UNUSED;
1412 static int add_lexmin_eq(struct isl_tab *tab, isl_int *eq)
1414 int r1, r2;
1415 int row;
1416 struct isl_tab_undo *snap;
1418 if (!tab)
1419 return -1;
1420 snap = isl_tab_snap(tab);
1421 r1 = isl_tab_add_row(tab, eq);
1422 if (r1 < 0)
1423 return -1;
1424 tab->con[r1].is_nonneg = 1;
1425 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r1]) < 0)
1426 return -1;
1428 row = tab->con[r1].index;
1429 if (is_constant(tab, row)) {
1430 if (!isl_int_is_zero(tab->mat->row[row][1]) ||
1431 (tab->M && !isl_int_is_zero(tab->mat->row[row][2]))) {
1432 if (isl_tab_mark_empty(tab) < 0)
1433 return -1;
1434 return 0;
1436 if (isl_tab_rollback(tab, snap) < 0)
1437 return -1;
1438 return 0;
1441 if (restore_lexmin(tab) < 0)
1442 return -1;
1443 if (tab->empty)
1444 return 0;
1446 isl_seq_neg(eq, eq, 1 + tab->n_var);
1448 r2 = isl_tab_add_row(tab, eq);
1449 if (r2 < 0)
1450 return -1;
1451 tab->con[r2].is_nonneg = 1;
1452 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r2]) < 0)
1453 return -1;
1455 if (restore_lexmin(tab) < 0)
1456 return -1;
1457 if (tab->empty)
1458 return 0;
1460 if (!tab->con[r1].is_row) {
1461 if (isl_tab_kill_col(tab, tab->con[r1].index) < 0)
1462 return -1;
1463 } else if (!tab->con[r2].is_row) {
1464 if (isl_tab_kill_col(tab, tab->con[r2].index) < 0)
1465 return -1;
1468 if (tab->bmap) {
1469 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1470 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1471 return -1;
1472 isl_seq_neg(eq, eq, 1 + tab->n_var);
1473 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1474 isl_seq_neg(eq, eq, 1 + tab->n_var);
1475 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1476 return -1;
1477 if (!tab->bmap)
1478 return -1;
1481 return 0;
1484 /* Add an inequality to the tableau, resolving violations using
1485 * restore_lexmin.
1487 * This function assumes that at least one more row and at least
1488 * one more element in the constraint array are available in the tableau.
1490 static struct isl_tab *add_lexmin_ineq(struct isl_tab *tab, isl_int *ineq)
1492 int r;
1494 if (!tab)
1495 return NULL;
1496 if (tab->bmap) {
1497 tab->bmap = isl_basic_map_add_ineq(tab->bmap, ineq);
1498 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1499 goto error;
1500 if (!tab->bmap)
1501 goto error;
1503 r = isl_tab_add_row(tab, ineq);
1504 if (r < 0)
1505 goto error;
1506 tab->con[r].is_nonneg = 1;
1507 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1508 goto error;
1509 if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1510 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1511 goto error;
1512 return tab;
1515 if (restore_lexmin(tab) < 0)
1516 goto error;
1517 if (!tab->empty && tab->con[r].is_row &&
1518 isl_tab_row_is_redundant(tab, tab->con[r].index))
1519 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1520 goto error;
1521 return tab;
1522 error:
1523 isl_tab_free(tab);
1524 return NULL;
1527 /* Check if the coefficients of the parameters are all integral.
1529 static int integer_parameter(struct isl_tab *tab, int row)
1531 int i;
1532 int col;
1533 unsigned off = 2 + tab->M;
1535 for (i = 0; i < tab->n_param; ++i) {
1536 /* Eliminated parameter */
1537 if (tab->var[i].is_row)
1538 continue;
1539 col = tab->var[i].index;
1540 if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
1541 tab->mat->row[row][0]))
1542 return 0;
1544 for (i = 0; i < tab->n_div; ++i) {
1545 if (tab->var[tab->n_var - tab->n_div + i].is_row)
1546 continue;
1547 col = tab->var[tab->n_var - tab->n_div + i].index;
1548 if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
1549 tab->mat->row[row][0]))
1550 return 0;
1552 return 1;
1555 /* Check if the coefficients of the non-parameter variables are all integral.
1557 static int integer_variable(struct isl_tab *tab, int row)
1559 int i;
1560 unsigned off = 2 + tab->M;
1562 for (i = tab->n_dead; i < tab->n_col; ++i) {
1563 if (tab->col_var[i] >= 0 &&
1564 (tab->col_var[i] < tab->n_param ||
1565 tab->col_var[i] >= tab->n_var - tab->n_div))
1566 continue;
1567 if (!isl_int_is_divisible_by(tab->mat->row[row][off + i],
1568 tab->mat->row[row][0]))
1569 return 0;
1571 return 1;
1574 /* Check if the constant term is integral.
1576 static int integer_constant(struct isl_tab *tab, int row)
1578 return isl_int_is_divisible_by(tab->mat->row[row][1],
1579 tab->mat->row[row][0]);
1582 #define I_CST 1 << 0
1583 #define I_PAR 1 << 1
1584 #define I_VAR 1 << 2
1586 /* Check for next (non-parameter) variable after "var" (first if var == -1)
1587 * that is non-integer and therefore requires a cut and return
1588 * the index of the variable.
1589 * For parametric tableaus, there are three parts in a row,
1590 * the constant, the coefficients of the parameters and the rest.
1591 * For each part, we check whether the coefficients in that part
1592 * are all integral and if so, set the corresponding flag in *f.
1593 * If the constant and the parameter part are integral, then the
1594 * current sample value is integral and no cut is required
1595 * (irrespective of whether the variable part is integral).
1597 static int next_non_integer_var(struct isl_tab *tab, int var, int *f)
1599 var = var < 0 ? tab->n_param : var + 1;
1601 for (; var < tab->n_var - tab->n_div; ++var) {
1602 int flags = 0;
1603 int row;
1604 if (!tab->var[var].is_row)
1605 continue;
1606 row = tab->var[var].index;
1607 if (integer_constant(tab, row))
1608 ISL_FL_SET(flags, I_CST);
1609 if (integer_parameter(tab, row))
1610 ISL_FL_SET(flags, I_PAR);
1611 if (ISL_FL_ISSET(flags, I_CST) && ISL_FL_ISSET(flags, I_PAR))
1612 continue;
1613 if (integer_variable(tab, row))
1614 ISL_FL_SET(flags, I_VAR);
1615 *f = flags;
1616 return var;
1618 return -1;
1621 /* Check for first (non-parameter) variable that is non-integer and
1622 * therefore requires a cut and return the corresponding row.
1623 * For parametric tableaus, there are three parts in a row,
1624 * the constant, the coefficients of the parameters and the rest.
1625 * For each part, we check whether the coefficients in that part
1626 * are all integral and if so, set the corresponding flag in *f.
1627 * If the constant and the parameter part are integral, then the
1628 * current sample value is integral and no cut is required
1629 * (irrespective of whether the variable part is integral).
1631 static int first_non_integer_row(struct isl_tab *tab, int *f)
1633 int var = next_non_integer_var(tab, -1, f);
1635 return var < 0 ? -1 : tab->var[var].index;
1638 /* Add a (non-parametric) cut to cut away the non-integral sample
1639 * value of the given row.
1641 * If the row is given by
1643 * m r = f + \sum_i a_i y_i
1645 * then the cut is
1647 * c = - {-f/m} + \sum_i {a_i/m} y_i >= 0
1649 * The big parameter, if any, is ignored, since it is assumed to be big
1650 * enough to be divisible by any integer.
1651 * If the tableau is actually a parametric tableau, then this function
1652 * is only called when all coefficients of the parameters are integral.
1653 * The cut therefore has zero coefficients for the parameters.
1655 * The current value is known to be negative, so row_sign, if it
1656 * exists, is set accordingly.
1658 * Return the row of the cut or -1.
1660 static int add_cut(struct isl_tab *tab, int row)
1662 int i;
1663 int r;
1664 isl_int *r_row;
1665 unsigned off = 2 + tab->M;
1667 if (isl_tab_extend_cons(tab, 1) < 0)
1668 return -1;
1669 r = isl_tab_allocate_con(tab);
1670 if (r < 0)
1671 return -1;
1673 r_row = tab->mat->row[tab->con[r].index];
1674 isl_int_set(r_row[0], tab->mat->row[row][0]);
1675 isl_int_neg(r_row[1], tab->mat->row[row][1]);
1676 isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
1677 isl_int_neg(r_row[1], r_row[1]);
1678 if (tab->M)
1679 isl_int_set_si(r_row[2], 0);
1680 for (i = 0; i < tab->n_col; ++i)
1681 isl_int_fdiv_r(r_row[off + i],
1682 tab->mat->row[row][off + i], tab->mat->row[row][0]);
1684 tab->con[r].is_nonneg = 1;
1685 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1686 return -1;
1687 if (tab->row_sign)
1688 tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
1690 return tab->con[r].index;
1693 #define CUT_ALL 1
1694 #define CUT_ONE 0
1696 /* Given a non-parametric tableau, add cuts until an integer
1697 * sample point is obtained or until the tableau is determined
1698 * to be integer infeasible.
1699 * As long as there is any non-integer value in the sample point,
1700 * we add appropriate cuts, if possible, for each of these
1701 * non-integer values and then resolve the violated
1702 * cut constraints using restore_lexmin.
1703 * If one of the corresponding rows is equal to an integral
1704 * combination of variables/constraints plus a non-integral constant,
1705 * then there is no way to obtain an integer point and we return
1706 * a tableau that is marked empty.
1707 * The parameter cutting_strategy controls the strategy used when adding cuts
1708 * to remove non-integer points. CUT_ALL adds all possible cuts
1709 * before continuing the search. CUT_ONE adds only one cut at a time.
1711 static struct isl_tab *cut_to_integer_lexmin(struct isl_tab *tab,
1712 int cutting_strategy)
1714 int var;
1715 int row;
1716 int flags;
1718 if (!tab)
1719 return NULL;
1720 if (tab->empty)
1721 return tab;
1723 while ((var = next_non_integer_var(tab, -1, &flags)) != -1) {
1724 do {
1725 if (ISL_FL_ISSET(flags, I_VAR)) {
1726 if (isl_tab_mark_empty(tab) < 0)
1727 goto error;
1728 return tab;
1730 row = tab->var[var].index;
1731 row = add_cut(tab, row);
1732 if (row < 0)
1733 goto error;
1734 if (cutting_strategy == CUT_ONE)
1735 break;
1736 } while ((var = next_non_integer_var(tab, var, &flags)) != -1);
1737 if (restore_lexmin(tab) < 0)
1738 goto error;
1739 if (tab->empty)
1740 break;
1742 return tab;
1743 error:
1744 isl_tab_free(tab);
1745 return NULL;
1748 /* Check whether all the currently active samples also satisfy the inequality
1749 * "ineq" (treated as an equality if eq is set).
1750 * Remove those samples that do not.
1752 static struct isl_tab *check_samples(struct isl_tab *tab, isl_int *ineq, int eq)
1754 int i;
1755 isl_int v;
1757 if (!tab)
1758 return NULL;
1760 isl_assert(tab->mat->ctx, tab->bmap, goto error);
1761 isl_assert(tab->mat->ctx, tab->samples, goto error);
1762 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, goto error);
1764 isl_int_init(v);
1765 for (i = tab->n_outside; i < tab->n_sample; ++i) {
1766 int sgn;
1767 isl_seq_inner_product(ineq, tab->samples->row[i],
1768 1 + tab->n_var, &v);
1769 sgn = isl_int_sgn(v);
1770 if (eq ? (sgn == 0) : (sgn >= 0))
1771 continue;
1772 tab = isl_tab_drop_sample(tab, i);
1773 if (!tab)
1774 break;
1776 isl_int_clear(v);
1778 return tab;
1779 error:
1780 isl_tab_free(tab);
1781 return NULL;
1784 /* Check whether the sample value of the tableau is finite,
1785 * i.e., either the tableau does not use a big parameter, or
1786 * all values of the variables are equal to the big parameter plus
1787 * some constant. This constant is the actual sample value.
1789 static int sample_is_finite(struct isl_tab *tab)
1791 int i;
1793 if (!tab->M)
1794 return 1;
1796 for (i = 0; i < tab->n_var; ++i) {
1797 int row;
1798 if (!tab->var[i].is_row)
1799 return 0;
1800 row = tab->var[i].index;
1801 if (isl_int_ne(tab->mat->row[row][0], tab->mat->row[row][2]))
1802 return 0;
1804 return 1;
1807 /* Check if the context tableau of sol has any integer points.
1808 * Leave tab in empty state if no integer point can be found.
1809 * If an integer point can be found and if moreover it is finite,
1810 * then it is added to the list of sample values.
1812 * This function is only called when none of the currently active sample
1813 * values satisfies the most recently added constraint.
1815 static struct isl_tab *check_integer_feasible(struct isl_tab *tab)
1817 struct isl_tab_undo *snap;
1819 if (!tab)
1820 return NULL;
1822 snap = isl_tab_snap(tab);
1823 if (isl_tab_push_basis(tab) < 0)
1824 goto error;
1826 tab = cut_to_integer_lexmin(tab, CUT_ALL);
1827 if (!tab)
1828 goto error;
1830 if (!tab->empty && sample_is_finite(tab)) {
1831 struct isl_vec *sample;
1833 sample = isl_tab_get_sample_value(tab);
1835 if (isl_tab_add_sample(tab, sample) < 0)
1836 goto error;
1839 if (!tab->empty && isl_tab_rollback(tab, snap) < 0)
1840 goto error;
1842 return tab;
1843 error:
1844 isl_tab_free(tab);
1845 return NULL;
1848 /* Check if any of the currently active sample values satisfies
1849 * the inequality "ineq" (an equality if eq is set).
1851 static int tab_has_valid_sample(struct isl_tab *tab, isl_int *ineq, int eq)
1853 int i;
1854 isl_int v;
1856 if (!tab)
1857 return -1;
1859 isl_assert(tab->mat->ctx, tab->bmap, return -1);
1860 isl_assert(tab->mat->ctx, tab->samples, return -1);
1861 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, return -1);
1863 isl_int_init(v);
1864 for (i = tab->n_outside; i < tab->n_sample; ++i) {
1865 int sgn;
1866 isl_seq_inner_product(ineq, tab->samples->row[i],
1867 1 + tab->n_var, &v);
1868 sgn = isl_int_sgn(v);
1869 if (eq ? (sgn == 0) : (sgn >= 0))
1870 break;
1872 isl_int_clear(v);
1874 return i < tab->n_sample;
1877 /* Insert a div specified by "div" to the tableau "tab" at position "pos" and
1878 * return isl_bool_true if the div is obviously non-negative.
1880 static isl_bool context_tab_insert_div(struct isl_tab *tab, int pos,
1881 __isl_keep isl_vec *div,
1882 int (*add_ineq)(void *user, isl_int *), void *user)
1884 int i;
1885 int r;
1886 struct isl_mat *samples;
1887 int nonneg;
1889 r = isl_tab_insert_div(tab, pos, div, add_ineq, user);
1890 if (r < 0)
1891 return isl_bool_error;
1892 nonneg = tab->var[r].is_nonneg;
1893 tab->var[r].frozen = 1;
1895 samples = isl_mat_extend(tab->samples,
1896 tab->n_sample, 1 + tab->n_var);
1897 tab->samples = samples;
1898 if (!samples)
1899 return isl_bool_error;
1900 for (i = tab->n_outside; i < samples->n_row; ++i) {
1901 isl_seq_inner_product(div->el + 1, samples->row[i],
1902 div->size - 1, &samples->row[i][samples->n_col - 1]);
1903 isl_int_fdiv_q(samples->row[i][samples->n_col - 1],
1904 samples->row[i][samples->n_col - 1], div->el[0]);
1906 tab->samples = isl_mat_move_cols(tab->samples, 1 + pos,
1907 1 + tab->n_var - 1, 1);
1908 if (!tab->samples)
1909 return isl_bool_error;
1911 return nonneg;
1914 /* Add a div specified by "div" to both the main tableau and
1915 * the context tableau. In case of the main tableau, we only
1916 * need to add an extra div. In the context tableau, we also
1917 * need to express the meaning of the div.
1918 * Return the index of the div or -1 if anything went wrong.
1920 * The new integer division is added before any unknown integer
1921 * divisions in the context to ensure that it does not get
1922 * equated to some linear combination involving unknown integer
1923 * divisions.
1925 static int add_div(struct isl_tab *tab, struct isl_context *context,
1926 __isl_keep isl_vec *div)
1928 int r;
1929 int pos;
1930 isl_bool nonneg;
1931 struct isl_tab *context_tab = context->op->peek_tab(context);
1933 if (!tab || !context_tab)
1934 goto error;
1936 pos = context_tab->n_var - context->n_unknown;
1937 if ((nonneg = context->op->insert_div(context, pos, div)) < 0)
1938 goto error;
1940 if (!context->op->is_ok(context))
1941 goto error;
1943 pos = tab->n_var - context->n_unknown;
1944 if (isl_tab_extend_vars(tab, 1) < 0)
1945 goto error;
1946 r = isl_tab_insert_var(tab, pos);
1947 if (r < 0)
1948 goto error;
1949 if (nonneg)
1950 tab->var[r].is_nonneg = 1;
1951 tab->var[r].frozen = 1;
1952 tab->n_div++;
1954 return tab->n_div - 1 - context->n_unknown;
1955 error:
1956 context->op->invalidate(context);
1957 return -1;
1960 static int find_div(struct isl_tab *tab, isl_int *div, isl_int denom)
1962 int i;
1963 unsigned total = isl_basic_map_total_dim(tab->bmap);
1965 for (i = 0; i < tab->bmap->n_div; ++i) {
1966 if (isl_int_ne(tab->bmap->div[i][0], denom))
1967 continue;
1968 if (!isl_seq_eq(tab->bmap->div[i] + 1, div, 1 + total))
1969 continue;
1970 return i;
1972 return -1;
1975 /* Return the index of a div that corresponds to "div".
1976 * We first check if we already have such a div and if not, we create one.
1978 static int get_div(struct isl_tab *tab, struct isl_context *context,
1979 struct isl_vec *div)
1981 int d;
1982 struct isl_tab *context_tab = context->op->peek_tab(context);
1984 if (!context_tab)
1985 return -1;
1987 d = find_div(context_tab, div->el + 1, div->el[0]);
1988 if (d != -1)
1989 return d;
1991 return add_div(tab, context, div);
1994 /* Add a parametric cut to cut away the non-integral sample value
1995 * of the give row.
1996 * Let a_i be the coefficients of the constant term and the parameters
1997 * and let b_i be the coefficients of the variables or constraints
1998 * in basis of the tableau.
1999 * Let q be the div q = floor(\sum_i {-a_i} y_i).
2001 * The cut is expressed as
2003 * c = \sum_i -{-a_i} y_i + \sum_i {b_i} x_i + q >= 0
2005 * If q did not already exist in the context tableau, then it is added first.
2006 * If q is in a column of the main tableau then the "+ q" can be accomplished
2007 * by setting the corresponding entry to the denominator of the constraint.
2008 * If q happens to be in a row of the main tableau, then the corresponding
2009 * row needs to be added instead (taking care of the denominators).
2010 * Note that this is very unlikely, but perhaps not entirely impossible.
2012 * The current value of the cut is known to be negative (or at least
2013 * non-positive), so row_sign is set accordingly.
2015 * Return the row of the cut or -1.
2017 static int add_parametric_cut(struct isl_tab *tab, int row,
2018 struct isl_context *context)
2020 struct isl_vec *div;
2021 int d;
2022 int i;
2023 int r;
2024 isl_int *r_row;
2025 int col;
2026 int n;
2027 unsigned off = 2 + tab->M;
2029 if (!context)
2030 return -1;
2032 div = get_row_parameter_div(tab, row);
2033 if (!div)
2034 return -1;
2036 n = tab->n_div - context->n_unknown;
2037 d = context->op->get_div(context, tab, div);
2038 isl_vec_free(div);
2039 if (d < 0)
2040 return -1;
2042 if (isl_tab_extend_cons(tab, 1) < 0)
2043 return -1;
2044 r = isl_tab_allocate_con(tab);
2045 if (r < 0)
2046 return -1;
2048 r_row = tab->mat->row[tab->con[r].index];
2049 isl_int_set(r_row[0], tab->mat->row[row][0]);
2050 isl_int_neg(r_row[1], tab->mat->row[row][1]);
2051 isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
2052 isl_int_neg(r_row[1], r_row[1]);
2053 if (tab->M)
2054 isl_int_set_si(r_row[2], 0);
2055 for (i = 0; i < tab->n_param; ++i) {
2056 if (tab->var[i].is_row)
2057 continue;
2058 col = tab->var[i].index;
2059 isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
2060 isl_int_fdiv_r(r_row[off + col], r_row[off + col],
2061 tab->mat->row[row][0]);
2062 isl_int_neg(r_row[off + col], r_row[off + col]);
2064 for (i = 0; i < tab->n_div; ++i) {
2065 if (tab->var[tab->n_var - tab->n_div + i].is_row)
2066 continue;
2067 col = tab->var[tab->n_var - tab->n_div + i].index;
2068 isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
2069 isl_int_fdiv_r(r_row[off + col], r_row[off + col],
2070 tab->mat->row[row][0]);
2071 isl_int_neg(r_row[off + col], r_row[off + col]);
2073 for (i = 0; i < tab->n_col; ++i) {
2074 if (tab->col_var[i] >= 0 &&
2075 (tab->col_var[i] < tab->n_param ||
2076 tab->col_var[i] >= tab->n_var - tab->n_div))
2077 continue;
2078 isl_int_fdiv_r(r_row[off + i],
2079 tab->mat->row[row][off + i], tab->mat->row[row][0]);
2081 if (tab->var[tab->n_var - tab->n_div + d].is_row) {
2082 isl_int gcd;
2083 int d_row = tab->var[tab->n_var - tab->n_div + d].index;
2084 isl_int_init(gcd);
2085 isl_int_gcd(gcd, tab->mat->row[d_row][0], r_row[0]);
2086 isl_int_divexact(r_row[0], r_row[0], gcd);
2087 isl_int_divexact(gcd, tab->mat->row[d_row][0], gcd);
2088 isl_seq_combine(r_row + 1, gcd, r_row + 1,
2089 r_row[0], tab->mat->row[d_row] + 1,
2090 off - 1 + tab->n_col);
2091 isl_int_mul(r_row[0], r_row[0], tab->mat->row[d_row][0]);
2092 isl_int_clear(gcd);
2093 } else {
2094 col = tab->var[tab->n_var - tab->n_div + d].index;
2095 isl_int_set(r_row[off + col], tab->mat->row[row][0]);
2098 tab->con[r].is_nonneg = 1;
2099 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
2100 return -1;
2101 if (tab->row_sign)
2102 tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
2104 row = tab->con[r].index;
2106 if (d >= n && context->op->detect_equalities(context, tab) < 0)
2107 return -1;
2109 return row;
2112 /* Construct a tableau for bmap that can be used for computing
2113 * the lexicographic minimum (or maximum) of bmap.
2114 * If not NULL, then dom is the domain where the minimum
2115 * should be computed. In this case, we set up a parametric
2116 * tableau with row signs (initialized to "unknown").
2117 * If M is set, then the tableau will use a big parameter.
2118 * If max is set, then a maximum should be computed instead of a minimum.
2119 * This means that for each variable x, the tableau will contain the variable
2120 * x' = M - x, rather than x' = M + x. This in turn means that the coefficient
2121 * of the variables in all constraints are negated prior to adding them
2122 * to the tableau.
2124 static struct isl_tab *tab_for_lexmin(struct isl_basic_map *bmap,
2125 struct isl_basic_set *dom, unsigned M, int max)
2127 int i;
2128 struct isl_tab *tab;
2129 unsigned n_var;
2130 unsigned o_var;
2132 tab = isl_tab_alloc(bmap->ctx, 2 * bmap->n_eq + bmap->n_ineq + 1,
2133 isl_basic_map_total_dim(bmap), M);
2134 if (!tab)
2135 return NULL;
2137 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
2138 if (dom) {
2139 tab->n_param = isl_basic_set_total_dim(dom) - dom->n_div;
2140 tab->n_div = dom->n_div;
2141 tab->row_sign = isl_calloc_array(bmap->ctx,
2142 enum isl_tab_row_sign, tab->mat->n_row);
2143 if (tab->mat->n_row && !tab->row_sign)
2144 goto error;
2146 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
2147 if (isl_tab_mark_empty(tab) < 0)
2148 goto error;
2149 return tab;
2152 for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
2153 tab->var[i].is_nonneg = 1;
2154 tab->var[i].frozen = 1;
2156 o_var = 1 + tab->n_param;
2157 n_var = tab->n_var - tab->n_param - tab->n_div;
2158 for (i = 0; i < bmap->n_eq; ++i) {
2159 if (max)
2160 isl_seq_neg(bmap->eq[i] + o_var,
2161 bmap->eq[i] + o_var, n_var);
2162 tab = add_lexmin_valid_eq(tab, bmap->eq[i]);
2163 if (max)
2164 isl_seq_neg(bmap->eq[i] + o_var,
2165 bmap->eq[i] + o_var, n_var);
2166 if (!tab || tab->empty)
2167 return tab;
2169 if (bmap->n_eq && restore_lexmin(tab) < 0)
2170 goto error;
2171 for (i = 0; i < bmap->n_ineq; ++i) {
2172 if (max)
2173 isl_seq_neg(bmap->ineq[i] + o_var,
2174 bmap->ineq[i] + o_var, n_var);
2175 tab = add_lexmin_ineq(tab, bmap->ineq[i]);
2176 if (max)
2177 isl_seq_neg(bmap->ineq[i] + o_var,
2178 bmap->ineq[i] + o_var, n_var);
2179 if (!tab || tab->empty)
2180 return tab;
2182 return tab;
2183 error:
2184 isl_tab_free(tab);
2185 return NULL;
2188 /* Given a main tableau where more than one row requires a split,
2189 * determine and return the "best" row to split on.
2191 * Given two rows in the main tableau, if the inequality corresponding
2192 * to the first row is redundant with respect to that of the second row
2193 * in the current tableau, then it is better to split on the second row,
2194 * since in the positive part, both rows will be positive.
2195 * (In the negative part a pivot will have to be performed and just about
2196 * anything can happen to the sign of the other row.)
2198 * As a simple heuristic, we therefore select the row that makes the most
2199 * of the other rows redundant.
2201 * Perhaps it would also be useful to look at the number of constraints
2202 * that conflict with any given constraint.
2204 * best is the best row so far (-1 when we have not found any row yet).
2205 * best_r is the number of other rows made redundant by row best.
2206 * When best is still -1, bset_r is meaningless, but it is initialized
2207 * to some arbitrary value (0) anyway. Without this redundant initialization
2208 * valgrind may warn about uninitialized memory accesses when isl
2209 * is compiled with some versions of gcc.
2211 static int best_split(struct isl_tab *tab, struct isl_tab *context_tab)
2213 struct isl_tab_undo *snap;
2214 int split;
2215 int row;
2216 int best = -1;
2217 int best_r = 0;
2219 if (isl_tab_extend_cons(context_tab, 2) < 0)
2220 return -1;
2222 snap = isl_tab_snap(context_tab);
2224 for (split = tab->n_redundant; split < tab->n_row; ++split) {
2225 struct isl_tab_undo *snap2;
2226 struct isl_vec *ineq = NULL;
2227 int r = 0;
2228 int ok;
2230 if (!isl_tab_var_from_row(tab, split)->is_nonneg)
2231 continue;
2232 if (tab->row_sign[split] != isl_tab_row_any)
2233 continue;
2235 ineq = get_row_parameter_ineq(tab, split);
2236 if (!ineq)
2237 return -1;
2238 ok = isl_tab_add_ineq(context_tab, ineq->el) >= 0;
2239 isl_vec_free(ineq);
2240 if (!ok)
2241 return -1;
2243 snap2 = isl_tab_snap(context_tab);
2245 for (row = tab->n_redundant; row < tab->n_row; ++row) {
2246 struct isl_tab_var *var;
2248 if (row == split)
2249 continue;
2250 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
2251 continue;
2252 if (tab->row_sign[row] != isl_tab_row_any)
2253 continue;
2255 ineq = get_row_parameter_ineq(tab, row);
2256 if (!ineq)
2257 return -1;
2258 ok = isl_tab_add_ineq(context_tab, ineq->el) >= 0;
2259 isl_vec_free(ineq);
2260 if (!ok)
2261 return -1;
2262 var = &context_tab->con[context_tab->n_con - 1];
2263 if (!context_tab->empty &&
2264 !isl_tab_min_at_most_neg_one(context_tab, var))
2265 r++;
2266 if (isl_tab_rollback(context_tab, snap2) < 0)
2267 return -1;
2269 if (best == -1 || r > best_r) {
2270 best = split;
2271 best_r = r;
2273 if (isl_tab_rollback(context_tab, snap) < 0)
2274 return -1;
2277 return best;
2280 static struct isl_basic_set *context_lex_peek_basic_set(
2281 struct isl_context *context)
2283 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2284 if (!clex->tab)
2285 return NULL;
2286 return isl_tab_peek_bset(clex->tab);
2289 static struct isl_tab *context_lex_peek_tab(struct isl_context *context)
2291 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2292 return clex->tab;
2295 static void context_lex_add_eq(struct isl_context *context, isl_int *eq,
2296 int check, int update)
2298 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2299 if (isl_tab_extend_cons(clex->tab, 2) < 0)
2300 goto error;
2301 if (add_lexmin_eq(clex->tab, eq) < 0)
2302 goto error;
2303 if (check) {
2304 int v = tab_has_valid_sample(clex->tab, eq, 1);
2305 if (v < 0)
2306 goto error;
2307 if (!v)
2308 clex->tab = check_integer_feasible(clex->tab);
2310 if (update)
2311 clex->tab = check_samples(clex->tab, eq, 1);
2312 return;
2313 error:
2314 isl_tab_free(clex->tab);
2315 clex->tab = NULL;
2318 static void context_lex_add_ineq(struct isl_context *context, isl_int *ineq,
2319 int check, int update)
2321 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2322 if (isl_tab_extend_cons(clex->tab, 1) < 0)
2323 goto error;
2324 clex->tab = add_lexmin_ineq(clex->tab, ineq);
2325 if (check) {
2326 int v = tab_has_valid_sample(clex->tab, ineq, 0);
2327 if (v < 0)
2328 goto error;
2329 if (!v)
2330 clex->tab = check_integer_feasible(clex->tab);
2332 if (update)
2333 clex->tab = check_samples(clex->tab, ineq, 0);
2334 return;
2335 error:
2336 isl_tab_free(clex->tab);
2337 clex->tab = NULL;
2340 static int context_lex_add_ineq_wrap(void *user, isl_int *ineq)
2342 struct isl_context *context = (struct isl_context *)user;
2343 context_lex_add_ineq(context, ineq, 0, 0);
2344 return context->op->is_ok(context) ? 0 : -1;
2347 /* Check which signs can be obtained by "ineq" on all the currently
2348 * active sample values. See row_sign for more information.
2350 static enum isl_tab_row_sign tab_ineq_sign(struct isl_tab *tab, isl_int *ineq,
2351 int strict)
2353 int i;
2354 int sgn;
2355 isl_int tmp;
2356 enum isl_tab_row_sign res = isl_tab_row_unknown;
2358 isl_assert(tab->mat->ctx, tab->samples, return isl_tab_row_unknown);
2359 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var,
2360 return isl_tab_row_unknown);
2362 isl_int_init(tmp);
2363 for (i = tab->n_outside; i < tab->n_sample; ++i) {
2364 isl_seq_inner_product(tab->samples->row[i], ineq,
2365 1 + tab->n_var, &tmp);
2366 sgn = isl_int_sgn(tmp);
2367 if (sgn > 0 || (sgn == 0 && strict)) {
2368 if (res == isl_tab_row_unknown)
2369 res = isl_tab_row_pos;
2370 if (res == isl_tab_row_neg)
2371 res = isl_tab_row_any;
2373 if (sgn < 0) {
2374 if (res == isl_tab_row_unknown)
2375 res = isl_tab_row_neg;
2376 if (res == isl_tab_row_pos)
2377 res = isl_tab_row_any;
2379 if (res == isl_tab_row_any)
2380 break;
2382 isl_int_clear(tmp);
2384 return res;
2387 static enum isl_tab_row_sign context_lex_ineq_sign(struct isl_context *context,
2388 isl_int *ineq, int strict)
2390 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2391 return tab_ineq_sign(clex->tab, ineq, strict);
2394 /* Check whether "ineq" can be added to the tableau without rendering
2395 * it infeasible.
2397 static int context_lex_test_ineq(struct isl_context *context, isl_int *ineq)
2399 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2400 struct isl_tab_undo *snap;
2401 int feasible;
2403 if (!clex->tab)
2404 return -1;
2406 if (isl_tab_extend_cons(clex->tab, 1) < 0)
2407 return -1;
2409 snap = isl_tab_snap(clex->tab);
2410 if (isl_tab_push_basis(clex->tab) < 0)
2411 return -1;
2412 clex->tab = add_lexmin_ineq(clex->tab, ineq);
2413 clex->tab = check_integer_feasible(clex->tab);
2414 if (!clex->tab)
2415 return -1;
2416 feasible = !clex->tab->empty;
2417 if (isl_tab_rollback(clex->tab, snap) < 0)
2418 return -1;
2420 return feasible;
2423 static int context_lex_get_div(struct isl_context *context, struct isl_tab *tab,
2424 struct isl_vec *div)
2426 return get_div(tab, context, div);
2429 /* Insert a div specified by "div" to the context tableau at position "pos" and
2430 * return isl_bool_true if the div is obviously non-negative.
2431 * context_tab_add_div will always return isl_bool_true, because all variables
2432 * in a isl_context_lex tableau are non-negative.
2433 * However, if we are using a big parameter in the context, then this only
2434 * reflects the non-negativity of the variable used to _encode_ the
2435 * div, i.e., div' = M + div, so we can't draw any conclusions.
2437 static isl_bool context_lex_insert_div(struct isl_context *context, int pos,
2438 __isl_keep isl_vec *div)
2440 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2441 isl_bool nonneg;
2442 nonneg = context_tab_insert_div(clex->tab, pos, div,
2443 context_lex_add_ineq_wrap, context);
2444 if (nonneg < 0)
2445 return isl_bool_error;
2446 if (clex->tab->M)
2447 return isl_bool_false;
2448 return nonneg;
2451 static int context_lex_detect_equalities(struct isl_context *context,
2452 struct isl_tab *tab)
2454 return 0;
2457 static int context_lex_best_split(struct isl_context *context,
2458 struct isl_tab *tab)
2460 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2461 struct isl_tab_undo *snap;
2462 int r;
2464 snap = isl_tab_snap(clex->tab);
2465 if (isl_tab_push_basis(clex->tab) < 0)
2466 return -1;
2467 r = best_split(tab, clex->tab);
2469 if (r >= 0 && isl_tab_rollback(clex->tab, snap) < 0)
2470 return -1;
2472 return r;
2475 static int context_lex_is_empty(struct isl_context *context)
2477 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2478 if (!clex->tab)
2479 return -1;
2480 return clex->tab->empty;
2483 static void *context_lex_save(struct isl_context *context)
2485 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2486 struct isl_tab_undo *snap;
2488 snap = isl_tab_snap(clex->tab);
2489 if (isl_tab_push_basis(clex->tab) < 0)
2490 return NULL;
2491 if (isl_tab_save_samples(clex->tab) < 0)
2492 return NULL;
2494 return snap;
2497 static void context_lex_restore(struct isl_context *context, void *save)
2499 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2500 if (isl_tab_rollback(clex->tab, (struct isl_tab_undo *)save) < 0) {
2501 isl_tab_free(clex->tab);
2502 clex->tab = NULL;
2506 static void context_lex_discard(void *save)
2510 static int context_lex_is_ok(struct isl_context *context)
2512 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2513 return !!clex->tab;
2516 /* For each variable in the context tableau, check if the variable can
2517 * only attain non-negative values. If so, mark the parameter as non-negative
2518 * in the main tableau. This allows for a more direct identification of some
2519 * cases of violated constraints.
2521 static struct isl_tab *tab_detect_nonnegative_parameters(struct isl_tab *tab,
2522 struct isl_tab *context_tab)
2524 int i;
2525 struct isl_tab_undo *snap;
2526 struct isl_vec *ineq = NULL;
2527 struct isl_tab_var *var;
2528 int n;
2530 if (context_tab->n_var == 0)
2531 return tab;
2533 ineq = isl_vec_alloc(tab->mat->ctx, 1 + context_tab->n_var);
2534 if (!ineq)
2535 goto error;
2537 if (isl_tab_extend_cons(context_tab, 1) < 0)
2538 goto error;
2540 snap = isl_tab_snap(context_tab);
2542 n = 0;
2543 isl_seq_clr(ineq->el, ineq->size);
2544 for (i = 0; i < context_tab->n_var; ++i) {
2545 isl_int_set_si(ineq->el[1 + i], 1);
2546 if (isl_tab_add_ineq(context_tab, ineq->el) < 0)
2547 goto error;
2548 var = &context_tab->con[context_tab->n_con - 1];
2549 if (!context_tab->empty &&
2550 !isl_tab_min_at_most_neg_one(context_tab, var)) {
2551 int j = i;
2552 if (i >= tab->n_param)
2553 j = i - tab->n_param + tab->n_var - tab->n_div;
2554 tab->var[j].is_nonneg = 1;
2555 n++;
2557 isl_int_set_si(ineq->el[1 + i], 0);
2558 if (isl_tab_rollback(context_tab, snap) < 0)
2559 goto error;
2562 if (context_tab->M && n == context_tab->n_var) {
2563 context_tab->mat = isl_mat_drop_cols(context_tab->mat, 2, 1);
2564 context_tab->M = 0;
2567 isl_vec_free(ineq);
2568 return tab;
2569 error:
2570 isl_vec_free(ineq);
2571 isl_tab_free(tab);
2572 return NULL;
2575 static struct isl_tab *context_lex_detect_nonnegative_parameters(
2576 struct isl_context *context, struct isl_tab *tab)
2578 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2579 struct isl_tab_undo *snap;
2581 if (!tab)
2582 return NULL;
2584 snap = isl_tab_snap(clex->tab);
2585 if (isl_tab_push_basis(clex->tab) < 0)
2586 goto error;
2588 tab = tab_detect_nonnegative_parameters(tab, clex->tab);
2590 if (isl_tab_rollback(clex->tab, snap) < 0)
2591 goto error;
2593 return tab;
2594 error:
2595 isl_tab_free(tab);
2596 return NULL;
2599 static void context_lex_invalidate(struct isl_context *context)
2601 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2602 isl_tab_free(clex->tab);
2603 clex->tab = NULL;
2606 static __isl_null struct isl_context *context_lex_free(
2607 struct isl_context *context)
2609 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2610 isl_tab_free(clex->tab);
2611 free(clex);
2613 return NULL;
2616 struct isl_context_op isl_context_lex_op = {
2617 context_lex_detect_nonnegative_parameters,
2618 context_lex_peek_basic_set,
2619 context_lex_peek_tab,
2620 context_lex_add_eq,
2621 context_lex_add_ineq,
2622 context_lex_ineq_sign,
2623 context_lex_test_ineq,
2624 context_lex_get_div,
2625 context_lex_insert_div,
2626 context_lex_detect_equalities,
2627 context_lex_best_split,
2628 context_lex_is_empty,
2629 context_lex_is_ok,
2630 context_lex_save,
2631 context_lex_restore,
2632 context_lex_discard,
2633 context_lex_invalidate,
2634 context_lex_free,
2637 static struct isl_tab *context_tab_for_lexmin(struct isl_basic_set *bset)
2639 struct isl_tab *tab;
2641 if (!bset)
2642 return NULL;
2643 tab = tab_for_lexmin(bset_to_bmap(bset), NULL, 1, 0);
2644 if (!tab)
2645 goto error;
2646 if (isl_tab_track_bset(tab, bset) < 0)
2647 goto error;
2648 tab = isl_tab_init_samples(tab);
2649 return tab;
2650 error:
2651 isl_basic_set_free(bset);
2652 return NULL;
2655 static struct isl_context *isl_context_lex_alloc(struct isl_basic_set *dom)
2657 struct isl_context_lex *clex;
2659 if (!dom)
2660 return NULL;
2662 clex = isl_alloc_type(dom->ctx, struct isl_context_lex);
2663 if (!clex)
2664 return NULL;
2666 clex->context.op = &isl_context_lex_op;
2668 clex->tab = context_tab_for_lexmin(isl_basic_set_copy(dom));
2669 if (restore_lexmin(clex->tab) < 0)
2670 goto error;
2671 clex->tab = check_integer_feasible(clex->tab);
2672 if (!clex->tab)
2673 goto error;
2675 return &clex->context;
2676 error:
2677 clex->context.op->free(&clex->context);
2678 return NULL;
2681 /* Representation of the context when using generalized basis reduction.
2683 * "shifted" contains the offsets of the unit hypercubes that lie inside the
2684 * context. Any rational point in "shifted" can therefore be rounded
2685 * up to an integer point in the context.
2686 * If the context is constrained by any equality, then "shifted" is not used
2687 * as it would be empty.
2689 struct isl_context_gbr {
2690 struct isl_context context;
2691 struct isl_tab *tab;
2692 struct isl_tab *shifted;
2693 struct isl_tab *cone;
2696 static struct isl_tab *context_gbr_detect_nonnegative_parameters(
2697 struct isl_context *context, struct isl_tab *tab)
2699 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2700 if (!tab)
2701 return NULL;
2702 return tab_detect_nonnegative_parameters(tab, cgbr->tab);
2705 static struct isl_basic_set *context_gbr_peek_basic_set(
2706 struct isl_context *context)
2708 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2709 if (!cgbr->tab)
2710 return NULL;
2711 return isl_tab_peek_bset(cgbr->tab);
2714 static struct isl_tab *context_gbr_peek_tab(struct isl_context *context)
2716 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2717 return cgbr->tab;
2720 /* Initialize the "shifted" tableau of the context, which
2721 * contains the constraints of the original tableau shifted
2722 * by the sum of all negative coefficients. This ensures
2723 * that any rational point in the shifted tableau can
2724 * be rounded up to yield an integer point in the original tableau.
2726 static void gbr_init_shifted(struct isl_context_gbr *cgbr)
2728 int i, j;
2729 struct isl_vec *cst;
2730 struct isl_basic_set *bset = isl_tab_peek_bset(cgbr->tab);
2731 unsigned dim = isl_basic_set_total_dim(bset);
2733 cst = isl_vec_alloc(cgbr->tab->mat->ctx, bset->n_ineq);
2734 if (!cst)
2735 return;
2737 for (i = 0; i < bset->n_ineq; ++i) {
2738 isl_int_set(cst->el[i], bset->ineq[i][0]);
2739 for (j = 0; j < dim; ++j) {
2740 if (!isl_int_is_neg(bset->ineq[i][1 + j]))
2741 continue;
2742 isl_int_add(bset->ineq[i][0], bset->ineq[i][0],
2743 bset->ineq[i][1 + j]);
2747 cgbr->shifted = isl_tab_from_basic_set(bset, 0);
2749 for (i = 0; i < bset->n_ineq; ++i)
2750 isl_int_set(bset->ineq[i][0], cst->el[i]);
2752 isl_vec_free(cst);
2755 /* Check if the shifted tableau is non-empty, and if so
2756 * use the sample point to construct an integer point
2757 * of the context tableau.
2759 static struct isl_vec *gbr_get_shifted_sample(struct isl_context_gbr *cgbr)
2761 struct isl_vec *sample;
2763 if (!cgbr->shifted)
2764 gbr_init_shifted(cgbr);
2765 if (!cgbr->shifted)
2766 return NULL;
2767 if (cgbr->shifted->empty)
2768 return isl_vec_alloc(cgbr->tab->mat->ctx, 0);
2770 sample = isl_tab_get_sample_value(cgbr->shifted);
2771 sample = isl_vec_ceil(sample);
2773 return sample;
2776 static struct isl_basic_set *drop_constant_terms(struct isl_basic_set *bset)
2778 int i;
2780 if (!bset)
2781 return NULL;
2783 for (i = 0; i < bset->n_eq; ++i)
2784 isl_int_set_si(bset->eq[i][0], 0);
2786 for (i = 0; i < bset->n_ineq; ++i)
2787 isl_int_set_si(bset->ineq[i][0], 0);
2789 return bset;
2792 static int use_shifted(struct isl_context_gbr *cgbr)
2794 if (!cgbr->tab)
2795 return 0;
2796 return cgbr->tab->bmap->n_eq == 0 && cgbr->tab->bmap->n_div == 0;
2799 static struct isl_vec *gbr_get_sample(struct isl_context_gbr *cgbr)
2801 struct isl_basic_set *bset;
2802 struct isl_basic_set *cone;
2804 if (isl_tab_sample_is_integer(cgbr->tab))
2805 return isl_tab_get_sample_value(cgbr->tab);
2807 if (use_shifted(cgbr)) {
2808 struct isl_vec *sample;
2810 sample = gbr_get_shifted_sample(cgbr);
2811 if (!sample || sample->size > 0)
2812 return sample;
2814 isl_vec_free(sample);
2817 if (!cgbr->cone) {
2818 bset = isl_tab_peek_bset(cgbr->tab);
2819 cgbr->cone = isl_tab_from_recession_cone(bset, 0);
2820 if (!cgbr->cone)
2821 return NULL;
2822 if (isl_tab_track_bset(cgbr->cone,
2823 isl_basic_set_copy(bset)) < 0)
2824 return NULL;
2826 if (isl_tab_detect_implicit_equalities(cgbr->cone) < 0)
2827 return NULL;
2829 if (cgbr->cone->n_dead == cgbr->cone->n_col) {
2830 struct isl_vec *sample;
2831 struct isl_tab_undo *snap;
2833 if (cgbr->tab->basis) {
2834 if (cgbr->tab->basis->n_col != 1 + cgbr->tab->n_var) {
2835 isl_mat_free(cgbr->tab->basis);
2836 cgbr->tab->basis = NULL;
2838 cgbr->tab->n_zero = 0;
2839 cgbr->tab->n_unbounded = 0;
2842 snap = isl_tab_snap(cgbr->tab);
2844 sample = isl_tab_sample(cgbr->tab);
2846 if (!sample || isl_tab_rollback(cgbr->tab, snap) < 0) {
2847 isl_vec_free(sample);
2848 return NULL;
2851 return sample;
2854 cone = isl_basic_set_dup(isl_tab_peek_bset(cgbr->cone));
2855 cone = drop_constant_terms(cone);
2856 cone = isl_basic_set_update_from_tab(cone, cgbr->cone);
2857 cone = isl_basic_set_underlying_set(cone);
2858 cone = isl_basic_set_gauss(cone, NULL);
2860 bset = isl_basic_set_dup(isl_tab_peek_bset(cgbr->tab));
2861 bset = isl_basic_set_update_from_tab(bset, cgbr->tab);
2862 bset = isl_basic_set_underlying_set(bset);
2863 bset = isl_basic_set_gauss(bset, NULL);
2865 return isl_basic_set_sample_with_cone(bset, cone);
2868 static void check_gbr_integer_feasible(struct isl_context_gbr *cgbr)
2870 struct isl_vec *sample;
2872 if (!cgbr->tab)
2873 return;
2875 if (cgbr->tab->empty)
2876 return;
2878 sample = gbr_get_sample(cgbr);
2879 if (!sample)
2880 goto error;
2882 if (sample->size == 0) {
2883 isl_vec_free(sample);
2884 if (isl_tab_mark_empty(cgbr->tab) < 0)
2885 goto error;
2886 return;
2889 if (isl_tab_add_sample(cgbr->tab, sample) < 0)
2890 goto error;
2892 return;
2893 error:
2894 isl_tab_free(cgbr->tab);
2895 cgbr->tab = NULL;
2898 static struct isl_tab *add_gbr_eq(struct isl_tab *tab, isl_int *eq)
2900 if (!tab)
2901 return NULL;
2903 if (isl_tab_extend_cons(tab, 2) < 0)
2904 goto error;
2906 if (isl_tab_add_eq(tab, eq) < 0)
2907 goto error;
2909 return tab;
2910 error:
2911 isl_tab_free(tab);
2912 return NULL;
2915 /* Add the equality described by "eq" to the context.
2916 * If "check" is set, then we check if the context is empty after
2917 * adding the equality.
2918 * If "update" is set, then we check if the samples are still valid.
2920 * We do not explicitly add shifted copies of the equality to
2921 * cgbr->shifted since they would conflict with each other.
2922 * Instead, we directly mark cgbr->shifted empty.
2924 static void context_gbr_add_eq(struct isl_context *context, isl_int *eq,
2925 int check, int update)
2927 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2929 cgbr->tab = add_gbr_eq(cgbr->tab, eq);
2931 if (cgbr->shifted && !cgbr->shifted->empty && use_shifted(cgbr)) {
2932 if (isl_tab_mark_empty(cgbr->shifted) < 0)
2933 goto error;
2936 if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
2937 if (isl_tab_extend_cons(cgbr->cone, 2) < 0)
2938 goto error;
2939 if (isl_tab_add_eq(cgbr->cone, eq) < 0)
2940 goto error;
2943 if (check) {
2944 int v = tab_has_valid_sample(cgbr->tab, eq, 1);
2945 if (v < 0)
2946 goto error;
2947 if (!v)
2948 check_gbr_integer_feasible(cgbr);
2950 if (update)
2951 cgbr->tab = check_samples(cgbr->tab, eq, 1);
2952 return;
2953 error:
2954 isl_tab_free(cgbr->tab);
2955 cgbr->tab = NULL;
2958 static void add_gbr_ineq(struct isl_context_gbr *cgbr, isl_int *ineq)
2960 if (!cgbr->tab)
2961 return;
2963 if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
2964 goto error;
2966 if (isl_tab_add_ineq(cgbr->tab, ineq) < 0)
2967 goto error;
2969 if (cgbr->shifted && !cgbr->shifted->empty && use_shifted(cgbr)) {
2970 int i;
2971 unsigned dim;
2972 dim = isl_basic_map_total_dim(cgbr->tab->bmap);
2974 if (isl_tab_extend_cons(cgbr->shifted, 1) < 0)
2975 goto error;
2977 for (i = 0; i < dim; ++i) {
2978 if (!isl_int_is_neg(ineq[1 + i]))
2979 continue;
2980 isl_int_add(ineq[0], ineq[0], ineq[1 + i]);
2983 if (isl_tab_add_ineq(cgbr->shifted, ineq) < 0)
2984 goto error;
2986 for (i = 0; i < dim; ++i) {
2987 if (!isl_int_is_neg(ineq[1 + i]))
2988 continue;
2989 isl_int_sub(ineq[0], ineq[0], ineq[1 + i]);
2993 if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
2994 if (isl_tab_extend_cons(cgbr->cone, 1) < 0)
2995 goto error;
2996 if (isl_tab_add_ineq(cgbr->cone, ineq) < 0)
2997 goto error;
3000 return;
3001 error:
3002 isl_tab_free(cgbr->tab);
3003 cgbr->tab = NULL;
3006 static void context_gbr_add_ineq(struct isl_context *context, isl_int *ineq,
3007 int check, int update)
3009 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3011 add_gbr_ineq(cgbr, ineq);
3012 if (!cgbr->tab)
3013 return;
3015 if (check) {
3016 int v = tab_has_valid_sample(cgbr->tab, ineq, 0);
3017 if (v < 0)
3018 goto error;
3019 if (!v)
3020 check_gbr_integer_feasible(cgbr);
3022 if (update)
3023 cgbr->tab = check_samples(cgbr->tab, ineq, 0);
3024 return;
3025 error:
3026 isl_tab_free(cgbr->tab);
3027 cgbr->tab = NULL;
3030 static int context_gbr_add_ineq_wrap(void *user, isl_int *ineq)
3032 struct isl_context *context = (struct isl_context *)user;
3033 context_gbr_add_ineq(context, ineq, 0, 0);
3034 return context->op->is_ok(context) ? 0 : -1;
3037 static enum isl_tab_row_sign context_gbr_ineq_sign(struct isl_context *context,
3038 isl_int *ineq, int strict)
3040 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3041 return tab_ineq_sign(cgbr->tab, ineq, strict);
3044 /* Check whether "ineq" can be added to the tableau without rendering
3045 * it infeasible.
3047 static int context_gbr_test_ineq(struct isl_context *context, isl_int *ineq)
3049 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3050 struct isl_tab_undo *snap;
3051 struct isl_tab_undo *shifted_snap = NULL;
3052 struct isl_tab_undo *cone_snap = NULL;
3053 int feasible;
3055 if (!cgbr->tab)
3056 return -1;
3058 if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
3059 return -1;
3061 snap = isl_tab_snap(cgbr->tab);
3062 if (cgbr->shifted)
3063 shifted_snap = isl_tab_snap(cgbr->shifted);
3064 if (cgbr->cone)
3065 cone_snap = isl_tab_snap(cgbr->cone);
3066 add_gbr_ineq(cgbr, ineq);
3067 check_gbr_integer_feasible(cgbr);
3068 if (!cgbr->tab)
3069 return -1;
3070 feasible = !cgbr->tab->empty;
3071 if (isl_tab_rollback(cgbr->tab, snap) < 0)
3072 return -1;
3073 if (shifted_snap) {
3074 if (isl_tab_rollback(cgbr->shifted, shifted_snap))
3075 return -1;
3076 } else if (cgbr->shifted) {
3077 isl_tab_free(cgbr->shifted);
3078 cgbr->shifted = NULL;
3080 if (cone_snap) {
3081 if (isl_tab_rollback(cgbr->cone, cone_snap))
3082 return -1;
3083 } else if (cgbr->cone) {
3084 isl_tab_free(cgbr->cone);
3085 cgbr->cone = NULL;
3088 return feasible;
3091 /* Return the column of the last of the variables associated to
3092 * a column that has a non-zero coefficient.
3093 * This function is called in a context where only coefficients
3094 * of parameters or divs can be non-zero.
3096 static int last_non_zero_var_col(struct isl_tab *tab, isl_int *p)
3098 int i;
3099 int col;
3101 if (tab->n_var == 0)
3102 return -1;
3104 for (i = tab->n_var - 1; i >= 0; --i) {
3105 if (i >= tab->n_param && i < tab->n_var - tab->n_div)
3106 continue;
3107 if (tab->var[i].is_row)
3108 continue;
3109 col = tab->var[i].index;
3110 if (!isl_int_is_zero(p[col]))
3111 return col;
3114 return -1;
3117 /* Look through all the recently added equalities in the context
3118 * to see if we can propagate any of them to the main tableau.
3120 * The newly added equalities in the context are encoded as pairs
3121 * of inequalities starting at inequality "first".
3123 * We tentatively add each of these equalities to the main tableau
3124 * and if this happens to result in a row with a final coefficient
3125 * that is one or negative one, we use it to kill a column
3126 * in the main tableau. Otherwise, we discard the tentatively
3127 * added row.
3128 * This tentative addition of equality constraints turns
3129 * on the undo facility of the tableau. Turn it off again
3130 * at the end, assuming it was turned off to begin with.
3132 * Return 0 on success and -1 on failure.
3134 static int propagate_equalities(struct isl_context_gbr *cgbr,
3135 struct isl_tab *tab, unsigned first)
3137 int i;
3138 struct isl_vec *eq = NULL;
3139 isl_bool needs_undo;
3141 needs_undo = isl_tab_need_undo(tab);
3142 if (needs_undo < 0)
3143 goto error;
3144 eq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
3145 if (!eq)
3146 goto error;
3148 if (isl_tab_extend_cons(tab, (cgbr->tab->bmap->n_ineq - first)/2) < 0)
3149 goto error;
3151 isl_seq_clr(eq->el + 1 + tab->n_param,
3152 tab->n_var - tab->n_param - tab->n_div);
3153 for (i = first; i < cgbr->tab->bmap->n_ineq; i += 2) {
3154 int j;
3155 int r;
3156 struct isl_tab_undo *snap;
3157 snap = isl_tab_snap(tab);
3159 isl_seq_cpy(eq->el, cgbr->tab->bmap->ineq[i], 1 + tab->n_param);
3160 isl_seq_cpy(eq->el + 1 + tab->n_var - tab->n_div,
3161 cgbr->tab->bmap->ineq[i] + 1 + tab->n_param,
3162 tab->n_div);
3164 r = isl_tab_add_row(tab, eq->el);
3165 if (r < 0)
3166 goto error;
3167 r = tab->con[r].index;
3168 j = last_non_zero_var_col(tab, tab->mat->row[r] + 2 + tab->M);
3169 if (j < 0 || j < tab->n_dead ||
3170 !isl_int_is_one(tab->mat->row[r][0]) ||
3171 (!isl_int_is_one(tab->mat->row[r][2 + tab->M + j]) &&
3172 !isl_int_is_negone(tab->mat->row[r][2 + tab->M + j]))) {
3173 if (isl_tab_rollback(tab, snap) < 0)
3174 goto error;
3175 continue;
3177 if (isl_tab_pivot(tab, r, j) < 0)
3178 goto error;
3179 if (isl_tab_kill_col(tab, j) < 0)
3180 goto error;
3182 if (restore_lexmin(tab) < 0)
3183 goto error;
3186 if (!needs_undo)
3187 isl_tab_clear_undo(tab);
3188 isl_vec_free(eq);
3190 return 0;
3191 error:
3192 isl_vec_free(eq);
3193 isl_tab_free(cgbr->tab);
3194 cgbr->tab = NULL;
3195 return -1;
3198 static int context_gbr_detect_equalities(struct isl_context *context,
3199 struct isl_tab *tab)
3201 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3202 unsigned n_ineq;
3204 if (!cgbr->cone) {
3205 struct isl_basic_set *bset = isl_tab_peek_bset(cgbr->tab);
3206 cgbr->cone = isl_tab_from_recession_cone(bset, 0);
3207 if (!cgbr->cone)
3208 goto error;
3209 if (isl_tab_track_bset(cgbr->cone,
3210 isl_basic_set_copy(bset)) < 0)
3211 goto error;
3213 if (isl_tab_detect_implicit_equalities(cgbr->cone) < 0)
3214 goto error;
3216 n_ineq = cgbr->tab->bmap->n_ineq;
3217 cgbr->tab = isl_tab_detect_equalities(cgbr->tab, cgbr->cone);
3218 if (!cgbr->tab)
3219 return -1;
3220 if (cgbr->tab->bmap->n_ineq > n_ineq &&
3221 propagate_equalities(cgbr, tab, n_ineq) < 0)
3222 return -1;
3224 return 0;
3225 error:
3226 isl_tab_free(cgbr->tab);
3227 cgbr->tab = NULL;
3228 return -1;
3231 static int context_gbr_get_div(struct isl_context *context, struct isl_tab *tab,
3232 struct isl_vec *div)
3234 return get_div(tab, context, div);
3237 static isl_bool context_gbr_insert_div(struct isl_context *context, int pos,
3238 __isl_keep isl_vec *div)
3240 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3241 if (cgbr->cone) {
3242 int r, n_div, o_div;
3244 n_div = isl_basic_map_dim(cgbr->cone->bmap, isl_dim_div);
3245 o_div = cgbr->cone->n_var - n_div;
3247 if (isl_tab_extend_cons(cgbr->cone, 3) < 0)
3248 return isl_bool_error;
3249 if (isl_tab_extend_vars(cgbr->cone, 1) < 0)
3250 return isl_bool_error;
3251 if ((r = isl_tab_insert_var(cgbr->cone, pos)) <0)
3252 return isl_bool_error;
3254 cgbr->cone->bmap = isl_basic_map_insert_div(cgbr->cone->bmap,
3255 r - o_div, div);
3256 if (!cgbr->cone->bmap)
3257 return isl_bool_error;
3258 if (isl_tab_push_var(cgbr->cone, isl_tab_undo_bmap_div,
3259 &cgbr->cone->var[r]) < 0)
3260 return isl_bool_error;
3262 return context_tab_insert_div(cgbr->tab, pos, div,
3263 context_gbr_add_ineq_wrap, context);
3266 static int context_gbr_best_split(struct isl_context *context,
3267 struct isl_tab *tab)
3269 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3270 struct isl_tab_undo *snap;
3271 int r;
3273 snap = isl_tab_snap(cgbr->tab);
3274 r = best_split(tab, cgbr->tab);
3276 if (r >= 0 && isl_tab_rollback(cgbr->tab, snap) < 0)
3277 return -1;
3279 return r;
3282 static int context_gbr_is_empty(struct isl_context *context)
3284 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3285 if (!cgbr->tab)
3286 return -1;
3287 return cgbr->tab->empty;
3290 struct isl_gbr_tab_undo {
3291 struct isl_tab_undo *tab_snap;
3292 struct isl_tab_undo *shifted_snap;
3293 struct isl_tab_undo *cone_snap;
3296 static void *context_gbr_save(struct isl_context *context)
3298 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3299 struct isl_gbr_tab_undo *snap;
3301 if (!cgbr->tab)
3302 return NULL;
3304 snap = isl_alloc_type(cgbr->tab->mat->ctx, struct isl_gbr_tab_undo);
3305 if (!snap)
3306 return NULL;
3308 snap->tab_snap = isl_tab_snap(cgbr->tab);
3309 if (isl_tab_save_samples(cgbr->tab) < 0)
3310 goto error;
3312 if (cgbr->shifted)
3313 snap->shifted_snap = isl_tab_snap(cgbr->shifted);
3314 else
3315 snap->shifted_snap = NULL;
3317 if (cgbr->cone)
3318 snap->cone_snap = isl_tab_snap(cgbr->cone);
3319 else
3320 snap->cone_snap = NULL;
3322 return snap;
3323 error:
3324 free(snap);
3325 return NULL;
3328 static void context_gbr_restore(struct isl_context *context, void *save)
3330 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3331 struct isl_gbr_tab_undo *snap = (struct isl_gbr_tab_undo *)save;
3332 if (!snap)
3333 goto error;
3334 if (isl_tab_rollback(cgbr->tab, snap->tab_snap) < 0)
3335 goto error;
3337 if (snap->shifted_snap) {
3338 if (isl_tab_rollback(cgbr->shifted, snap->shifted_snap) < 0)
3339 goto error;
3340 } else if (cgbr->shifted) {
3341 isl_tab_free(cgbr->shifted);
3342 cgbr->shifted = NULL;
3345 if (snap->cone_snap) {
3346 if (isl_tab_rollback(cgbr->cone, snap->cone_snap) < 0)
3347 goto error;
3348 } else if (cgbr->cone) {
3349 isl_tab_free(cgbr->cone);
3350 cgbr->cone = NULL;
3353 free(snap);
3355 return;
3356 error:
3357 free(snap);
3358 isl_tab_free(cgbr->tab);
3359 cgbr->tab = NULL;
3362 static void context_gbr_discard(void *save)
3364 struct isl_gbr_tab_undo *snap = (struct isl_gbr_tab_undo *)save;
3365 free(snap);
3368 static int context_gbr_is_ok(struct isl_context *context)
3370 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3371 return !!cgbr->tab;
3374 static void context_gbr_invalidate(struct isl_context *context)
3376 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3377 isl_tab_free(cgbr->tab);
3378 cgbr->tab = NULL;
3381 static __isl_null struct isl_context *context_gbr_free(
3382 struct isl_context *context)
3384 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3385 isl_tab_free(cgbr->tab);
3386 isl_tab_free(cgbr->shifted);
3387 isl_tab_free(cgbr->cone);
3388 free(cgbr);
3390 return NULL;
3393 struct isl_context_op isl_context_gbr_op = {
3394 context_gbr_detect_nonnegative_parameters,
3395 context_gbr_peek_basic_set,
3396 context_gbr_peek_tab,
3397 context_gbr_add_eq,
3398 context_gbr_add_ineq,
3399 context_gbr_ineq_sign,
3400 context_gbr_test_ineq,
3401 context_gbr_get_div,
3402 context_gbr_insert_div,
3403 context_gbr_detect_equalities,
3404 context_gbr_best_split,
3405 context_gbr_is_empty,
3406 context_gbr_is_ok,
3407 context_gbr_save,
3408 context_gbr_restore,
3409 context_gbr_discard,
3410 context_gbr_invalidate,
3411 context_gbr_free,
3414 static struct isl_context *isl_context_gbr_alloc(__isl_keep isl_basic_set *dom)
3416 struct isl_context_gbr *cgbr;
3418 if (!dom)
3419 return NULL;
3421 cgbr = isl_calloc_type(dom->ctx, struct isl_context_gbr);
3422 if (!cgbr)
3423 return NULL;
3425 cgbr->context.op = &isl_context_gbr_op;
3427 cgbr->shifted = NULL;
3428 cgbr->cone = NULL;
3429 cgbr->tab = isl_tab_from_basic_set(dom, 1);
3430 cgbr->tab = isl_tab_init_samples(cgbr->tab);
3431 if (!cgbr->tab)
3432 goto error;
3433 check_gbr_integer_feasible(cgbr);
3435 return &cgbr->context;
3436 error:
3437 cgbr->context.op->free(&cgbr->context);
3438 return NULL;
3441 /* Allocate a context corresponding to "dom".
3442 * The representation specific fields are initialized by
3443 * isl_context_lex_alloc or isl_context_gbr_alloc.
3444 * The shared "n_unknown" field is initialized to the number
3445 * of final unknown integer divisions in "dom".
3447 static struct isl_context *isl_context_alloc(__isl_keep isl_basic_set *dom)
3449 struct isl_context *context;
3450 int first;
3452 if (!dom)
3453 return NULL;
3455 if (dom->ctx->opt->context == ISL_CONTEXT_LEXMIN)
3456 context = isl_context_lex_alloc(dom);
3457 else
3458 context = isl_context_gbr_alloc(dom);
3460 if (!context)
3461 return NULL;
3463 first = isl_basic_set_first_unknown_div(dom);
3464 if (first < 0)
3465 return context->op->free(context);
3466 context->n_unknown = isl_basic_set_dim(dom, isl_dim_div) - first;
3468 return context;
3471 /* Construct an isl_sol_map structure for accumulating the solution.
3472 * If track_empty is set, then we also keep track of the parts
3473 * of the context where there is no solution.
3474 * If max is set, then we are solving a maximization, rather than
3475 * a minimization problem, which means that the variables in the
3476 * tableau have value "M - x" rather than "M + x".
3478 static struct isl_sol *sol_map_init(__isl_keep isl_basic_map *bmap,
3479 __isl_take isl_basic_set *dom, int track_empty, int max)
3481 struct isl_sol_map *sol_map = NULL;
3483 if (!bmap)
3484 goto error;
3486 sol_map = isl_calloc_type(bmap->ctx, struct isl_sol_map);
3487 if (!sol_map)
3488 goto error;
3490 sol_map->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
3491 sol_map->sol.dec_level.callback.run = &sol_dec_level_wrap;
3492 sol_map->sol.dec_level.sol = &sol_map->sol;
3493 sol_map->sol.max = max;
3494 sol_map->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
3495 sol_map->sol.add = &sol_map_add_wrap;
3496 sol_map->sol.add_empty = track_empty ? &sol_map_add_empty_wrap : NULL;
3497 sol_map->sol.free = &sol_map_free_wrap;
3498 sol_map->map = isl_map_alloc_space(isl_basic_map_get_space(bmap), 1,
3499 ISL_MAP_DISJOINT);
3500 if (!sol_map->map)
3501 goto error;
3503 sol_map->sol.context = isl_context_alloc(dom);
3504 if (!sol_map->sol.context)
3505 goto error;
3507 if (track_empty) {
3508 sol_map->empty = isl_set_alloc_space(isl_basic_set_get_space(dom),
3509 1, ISL_SET_DISJOINT);
3510 if (!sol_map->empty)
3511 goto error;
3514 isl_basic_set_free(dom);
3515 return &sol_map->sol;
3516 error:
3517 isl_basic_set_free(dom);
3518 sol_map_free(sol_map);
3519 return NULL;
3522 /* Check whether all coefficients of (non-parameter) variables
3523 * are non-positive, meaning that no pivots can be performed on the row.
3525 static int is_critical(struct isl_tab *tab, int row)
3527 int j;
3528 unsigned off = 2 + tab->M;
3530 for (j = tab->n_dead; j < tab->n_col; ++j) {
3531 if (tab->col_var[j] >= 0 &&
3532 (tab->col_var[j] < tab->n_param ||
3533 tab->col_var[j] >= tab->n_var - tab->n_div))
3534 continue;
3536 if (isl_int_is_pos(tab->mat->row[row][off + j]))
3537 return 0;
3540 return 1;
3543 /* Check whether the inequality represented by vec is strict over the integers,
3544 * i.e., there are no integer values satisfying the constraint with
3545 * equality. This happens if the gcd of the coefficients is not a divisor
3546 * of the constant term. If so, scale the constraint down by the gcd
3547 * of the coefficients.
3549 static int is_strict(struct isl_vec *vec)
3551 isl_int gcd;
3552 int strict = 0;
3554 isl_int_init(gcd);
3555 isl_seq_gcd(vec->el + 1, vec->size - 1, &gcd);
3556 if (!isl_int_is_one(gcd)) {
3557 strict = !isl_int_is_divisible_by(vec->el[0], gcd);
3558 isl_int_fdiv_q(vec->el[0], vec->el[0], gcd);
3559 isl_seq_scale_down(vec->el + 1, vec->el + 1, gcd, vec->size-1);
3561 isl_int_clear(gcd);
3563 return strict;
3566 /* Determine the sign of the given row of the main tableau.
3567 * The result is one of
3568 * isl_tab_row_pos: always non-negative; no pivot needed
3569 * isl_tab_row_neg: always non-positive; pivot
3570 * isl_tab_row_any: can be both positive and negative; split
3572 * We first handle some simple cases
3573 * - the row sign may be known already
3574 * - the row may be obviously non-negative
3575 * - the parametric constant may be equal to that of another row
3576 * for which we know the sign. This sign will be either "pos" or
3577 * "any". If it had been "neg" then we would have pivoted before.
3579 * If none of these cases hold, we check the value of the row for each
3580 * of the currently active samples. Based on the signs of these values
3581 * we make an initial determination of the sign of the row.
3583 * all zero -> unk(nown)
3584 * all non-negative -> pos
3585 * all non-positive -> neg
3586 * both negative and positive -> all
3588 * If we end up with "all", we are done.
3589 * Otherwise, we perform a check for positive and/or negative
3590 * values as follows.
3592 * samples neg unk pos
3593 * <0 ? Y N Y N
3594 * pos any pos
3595 * >0 ? Y N Y N
3596 * any neg any neg
3598 * There is no special sign for "zero", because we can usually treat zero
3599 * as either non-negative or non-positive, whatever works out best.
3600 * However, if the row is "critical", meaning that pivoting is impossible
3601 * then we don't want to limp zero with the non-positive case, because
3602 * then we we would lose the solution for those values of the parameters
3603 * where the value of the row is zero. Instead, we treat 0 as non-negative
3604 * ensuring a split if the row can attain both zero and negative values.
3605 * The same happens when the original constraint was one that could not
3606 * be satisfied with equality by any integer values of the parameters.
3607 * In this case, we normalize the constraint, but then a value of zero
3608 * for the normalized constraint is actually a positive value for the
3609 * original constraint, so again we need to treat zero as non-negative.
3610 * In both these cases, we have the following decision tree instead:
3612 * all non-negative -> pos
3613 * all negative -> neg
3614 * both negative and non-negative -> all
3616 * samples neg pos
3617 * <0 ? Y N
3618 * any pos
3619 * >=0 ? Y N
3620 * any neg
3622 static enum isl_tab_row_sign row_sign(struct isl_tab *tab,
3623 struct isl_sol *sol, int row)
3625 struct isl_vec *ineq = NULL;
3626 enum isl_tab_row_sign res = isl_tab_row_unknown;
3627 int critical;
3628 int strict;
3629 int row2;
3631 if (tab->row_sign[row] != isl_tab_row_unknown)
3632 return tab->row_sign[row];
3633 if (is_obviously_nonneg(tab, row))
3634 return isl_tab_row_pos;
3635 for (row2 = tab->n_redundant; row2 < tab->n_row; ++row2) {
3636 if (tab->row_sign[row2] == isl_tab_row_unknown)
3637 continue;
3638 if (identical_parameter_line(tab, row, row2))
3639 return tab->row_sign[row2];
3642 critical = is_critical(tab, row);
3644 ineq = get_row_parameter_ineq(tab, row);
3645 if (!ineq)
3646 goto error;
3648 strict = is_strict(ineq);
3650 res = sol->context->op->ineq_sign(sol->context, ineq->el,
3651 critical || strict);
3653 if (res == isl_tab_row_unknown || res == isl_tab_row_pos) {
3654 /* test for negative values */
3655 int feasible;
3656 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3657 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3659 feasible = sol->context->op->test_ineq(sol->context, ineq->el);
3660 if (feasible < 0)
3661 goto error;
3662 if (!feasible)
3663 res = isl_tab_row_pos;
3664 else
3665 res = (res == isl_tab_row_unknown) ? isl_tab_row_neg
3666 : isl_tab_row_any;
3667 if (res == isl_tab_row_neg) {
3668 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3669 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3673 if (res == isl_tab_row_neg) {
3674 /* test for positive values */
3675 int feasible;
3676 if (!critical && !strict)
3677 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3679 feasible = sol->context->op->test_ineq(sol->context, ineq->el);
3680 if (feasible < 0)
3681 goto error;
3682 if (feasible)
3683 res = isl_tab_row_any;
3686 isl_vec_free(ineq);
3687 return res;
3688 error:
3689 isl_vec_free(ineq);
3690 return isl_tab_row_unknown;
3693 static void find_solutions(struct isl_sol *sol, struct isl_tab *tab);
3695 /* Find solutions for values of the parameters that satisfy the given
3696 * inequality.
3698 * We currently take a snapshot of the context tableau that is reset
3699 * when we return from this function, while we make a copy of the main
3700 * tableau, leaving the original main tableau untouched.
3701 * These are fairly arbitrary choices. Making a copy also of the context
3702 * tableau would obviate the need to undo any changes made to it later,
3703 * while taking a snapshot of the main tableau could reduce memory usage.
3704 * If we were to switch to taking a snapshot of the main tableau,
3705 * we would have to keep in mind that we need to save the row signs
3706 * and that we need to do this before saving the current basis
3707 * such that the basis has been restore before we restore the row signs.
3709 static void find_in_pos(struct isl_sol *sol, struct isl_tab *tab, isl_int *ineq)
3711 void *saved;
3713 if (!sol->context)
3714 goto error;
3715 saved = sol->context->op->save(sol->context);
3717 tab = isl_tab_dup(tab);
3718 if (!tab)
3719 goto error;
3721 sol->context->op->add_ineq(sol->context, ineq, 0, 1);
3723 find_solutions(sol, tab);
3725 if (!sol->error)
3726 sol->context->op->restore(sol->context, saved);
3727 else
3728 sol->context->op->discard(saved);
3729 return;
3730 error:
3731 sol->error = 1;
3734 /* Record the absence of solutions for those values of the parameters
3735 * that do not satisfy the given inequality with equality.
3737 static void no_sol_in_strict(struct isl_sol *sol,
3738 struct isl_tab *tab, struct isl_vec *ineq)
3740 int empty;
3741 void *saved;
3743 if (!sol->context || sol->error)
3744 goto error;
3745 saved = sol->context->op->save(sol->context);
3747 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3749 sol->context->op->add_ineq(sol->context, ineq->el, 1, 0);
3750 if (!sol->context)
3751 goto error;
3753 empty = tab->empty;
3754 tab->empty = 1;
3755 sol_add(sol, tab);
3756 tab->empty = empty;
3758 isl_int_add_ui(ineq->el[0], ineq->el[0], 1);
3760 sol->context->op->restore(sol->context, saved);
3761 return;
3762 error:
3763 sol->error = 1;
3766 /* Reset all row variables that are marked to have a sign that may
3767 * be both positive and negative to have an unknown sign.
3769 static void reset_any_to_unknown(struct isl_tab *tab)
3771 int row;
3773 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3774 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
3775 continue;
3776 if (tab->row_sign[row] == isl_tab_row_any)
3777 tab->row_sign[row] = isl_tab_row_unknown;
3781 /* Compute the lexicographic minimum of the set represented by the main
3782 * tableau "tab" within the context "sol->context_tab".
3783 * On entry the sample value of the main tableau is lexicographically
3784 * less than or equal to this lexicographic minimum.
3785 * Pivots are performed until a feasible point is found, which is then
3786 * necessarily equal to the minimum, or until the tableau is found to
3787 * be infeasible. Some pivots may need to be performed for only some
3788 * feasible values of the context tableau. If so, the context tableau
3789 * is split into a part where the pivot is needed and a part where it is not.
3791 * Whenever we enter the main loop, the main tableau is such that no
3792 * "obvious" pivots need to be performed on it, where "obvious" means
3793 * that the given row can be seen to be negative without looking at
3794 * the context tableau. In particular, for non-parametric problems,
3795 * no pivots need to be performed on the main tableau.
3796 * The caller of find_solutions is responsible for making this property
3797 * hold prior to the first iteration of the loop, while restore_lexmin
3798 * is called before every other iteration.
3800 * Inside the main loop, we first examine the signs of the rows of
3801 * the main tableau within the context of the context tableau.
3802 * If we find a row that is always non-positive for all values of
3803 * the parameters satisfying the context tableau and negative for at
3804 * least one value of the parameters, we perform the appropriate pivot
3805 * and start over. An exception is the case where no pivot can be
3806 * performed on the row. In this case, we require that the sign of
3807 * the row is negative for all values of the parameters (rather than just
3808 * non-positive). This special case is handled inside row_sign, which
3809 * will say that the row can have any sign if it determines that it can
3810 * attain both negative and zero values.
3812 * If we can't find a row that always requires a pivot, but we can find
3813 * one or more rows that require a pivot for some values of the parameters
3814 * (i.e., the row can attain both positive and negative signs), then we split
3815 * the context tableau into two parts, one where we force the sign to be
3816 * non-negative and one where we force is to be negative.
3817 * The non-negative part is handled by a recursive call (through find_in_pos).
3818 * Upon returning from this call, we continue with the negative part and
3819 * perform the required pivot.
3821 * If no such rows can be found, all rows are non-negative and we have
3822 * found a (rational) feasible point. If we only wanted a rational point
3823 * then we are done.
3824 * Otherwise, we check if all values of the sample point of the tableau
3825 * are integral for the variables. If so, we have found the minimal
3826 * integral point and we are done.
3827 * If the sample point is not integral, then we need to make a distinction
3828 * based on whether the constant term is non-integral or the coefficients
3829 * of the parameters. Furthermore, in order to decide how to handle
3830 * the non-integrality, we also need to know whether the coefficients
3831 * of the other columns in the tableau are integral. This leads
3832 * to the following table. The first two rows do not correspond
3833 * to a non-integral sample point and are only mentioned for completeness.
3835 * constant parameters other
3837 * int int int |
3838 * int int rat | -> no problem
3840 * rat int int -> fail
3842 * rat int rat -> cut
3844 * int rat rat |
3845 * rat rat rat | -> parametric cut
3847 * int rat int |
3848 * rat rat int | -> split context
3850 * If the parametric constant is completely integral, then there is nothing
3851 * to be done. If the constant term is non-integral, but all the other
3852 * coefficient are integral, then there is nothing that can be done
3853 * and the tableau has no integral solution.
3854 * If, on the other hand, one or more of the other columns have rational
3855 * coefficients, but the parameter coefficients are all integral, then
3856 * we can perform a regular (non-parametric) cut.
3857 * Finally, if there is any parameter coefficient that is non-integral,
3858 * then we need to involve the context tableau. There are two cases here.
3859 * If at least one other column has a rational coefficient, then we
3860 * can perform a parametric cut in the main tableau by adding a new
3861 * integer division in the context tableau.
3862 * If all other columns have integral coefficients, then we need to
3863 * enforce that the rational combination of parameters (c + \sum a_i y_i)/m
3864 * is always integral. We do this by introducing an integer division
3865 * q = floor((c + \sum a_i y_i)/m) and stipulating that its argument should
3866 * always be integral in the context tableau, i.e., m q = c + \sum a_i y_i.
3867 * Since q is expressed in the tableau as
3868 * c + \sum a_i y_i - m q >= 0
3869 * -c - \sum a_i y_i + m q + m - 1 >= 0
3870 * it is sufficient to add the inequality
3871 * -c - \sum a_i y_i + m q >= 0
3872 * In the part of the context where this inequality does not hold, the
3873 * main tableau is marked as being empty.
3875 static void find_solutions(struct isl_sol *sol, struct isl_tab *tab)
3877 struct isl_context *context;
3878 int r;
3880 if (!tab || sol->error)
3881 goto error;
3883 context = sol->context;
3885 if (tab->empty)
3886 goto done;
3887 if (context->op->is_empty(context))
3888 goto done;
3890 for (r = 0; r >= 0 && tab && !tab->empty; r = restore_lexmin(tab)) {
3891 int flags;
3892 int row;
3893 enum isl_tab_row_sign sgn;
3894 int split = -1;
3895 int n_split = 0;
3897 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3898 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
3899 continue;
3900 sgn = row_sign(tab, sol, row);
3901 if (!sgn)
3902 goto error;
3903 tab->row_sign[row] = sgn;
3904 if (sgn == isl_tab_row_any)
3905 n_split++;
3906 if (sgn == isl_tab_row_any && split == -1)
3907 split = row;
3908 if (sgn == isl_tab_row_neg)
3909 break;
3911 if (row < tab->n_row)
3912 continue;
3913 if (split != -1) {
3914 struct isl_vec *ineq;
3915 if (n_split != 1)
3916 split = context->op->best_split(context, tab);
3917 if (split < 0)
3918 goto error;
3919 ineq = get_row_parameter_ineq(tab, split);
3920 if (!ineq)
3921 goto error;
3922 is_strict(ineq);
3923 reset_any_to_unknown(tab);
3924 tab->row_sign[split] = isl_tab_row_pos;
3925 sol_inc_level(sol);
3926 find_in_pos(sol, tab, ineq->el);
3927 tab->row_sign[split] = isl_tab_row_neg;
3928 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3929 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3930 if (!sol->error)
3931 context->op->add_ineq(context, ineq->el, 0, 1);
3932 isl_vec_free(ineq);
3933 if (sol->error)
3934 goto error;
3935 continue;
3937 if (tab->rational)
3938 break;
3939 row = first_non_integer_row(tab, &flags);
3940 if (row < 0)
3941 break;
3942 if (ISL_FL_ISSET(flags, I_PAR)) {
3943 if (ISL_FL_ISSET(flags, I_VAR)) {
3944 if (isl_tab_mark_empty(tab) < 0)
3945 goto error;
3946 break;
3948 row = add_cut(tab, row);
3949 } else if (ISL_FL_ISSET(flags, I_VAR)) {
3950 struct isl_vec *div;
3951 struct isl_vec *ineq;
3952 int d;
3953 div = get_row_split_div(tab, row);
3954 if (!div)
3955 goto error;
3956 d = context->op->get_div(context, tab, div);
3957 isl_vec_free(div);
3958 if (d < 0)
3959 goto error;
3960 ineq = ineq_for_div(context->op->peek_basic_set(context), d);
3961 if (!ineq)
3962 goto error;
3963 sol_inc_level(sol);
3964 no_sol_in_strict(sol, tab, ineq);
3965 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3966 context->op->add_ineq(context, ineq->el, 1, 1);
3967 isl_vec_free(ineq);
3968 if (sol->error || !context->op->is_ok(context))
3969 goto error;
3970 tab = set_row_cst_to_div(tab, row, d);
3971 if (context->op->is_empty(context))
3972 break;
3973 } else
3974 row = add_parametric_cut(tab, row, context);
3975 if (row < 0)
3976 goto error;
3978 if (r < 0)
3979 goto error;
3980 done:
3981 sol_add(sol, tab);
3982 isl_tab_free(tab);
3983 return;
3984 error:
3985 isl_tab_free(tab);
3986 sol->error = 1;
3989 /* Does "sol" contain a pair of partial solutions that could potentially
3990 * be merged?
3992 * We currently only check that "sol" is not in an error state
3993 * and that there are at least two partial solutions of which the final two
3994 * are defined at the same level.
3996 static int sol_has_mergeable_solutions(struct isl_sol *sol)
3998 if (sol->error)
3999 return 0;
4000 if (!sol->partial)
4001 return 0;
4002 if (!sol->partial->next)
4003 return 0;
4004 return sol->partial->level == sol->partial->next->level;
4007 /* Compute the lexicographic minimum of the set represented by the main
4008 * tableau "tab" within the context "sol->context_tab".
4010 * As a preprocessing step, we first transfer all the purely parametric
4011 * equalities from the main tableau to the context tableau, i.e.,
4012 * parameters that have been pivoted to a row.
4013 * These equalities are ignored by the main algorithm, because the
4014 * corresponding rows may not be marked as being non-negative.
4015 * In parts of the context where the added equality does not hold,
4016 * the main tableau is marked as being empty.
4018 * Before we embark on the actual computation, we save a copy
4019 * of the context. When we return, we check if there are any
4020 * partial solutions that can potentially be merged. If so,
4021 * we perform a rollback to the initial state of the context.
4022 * The merging of partial solutions happens inside calls to
4023 * sol_dec_level that are pushed onto the undo stack of the context.
4024 * If there are no partial solutions that can potentially be merged
4025 * then the rollback is skipped as it would just be wasted effort.
4027 static void find_solutions_main(struct isl_sol *sol, struct isl_tab *tab)
4029 int row;
4030 void *saved;
4032 if (!tab)
4033 goto error;
4035 sol->level = 0;
4037 for (row = tab->n_redundant; row < tab->n_row; ++row) {
4038 int p;
4039 struct isl_vec *eq;
4041 if (tab->row_var[row] < 0)
4042 continue;
4043 if (tab->row_var[row] >= tab->n_param &&
4044 tab->row_var[row] < tab->n_var - tab->n_div)
4045 continue;
4046 if (tab->row_var[row] < tab->n_param)
4047 p = tab->row_var[row];
4048 else
4049 p = tab->row_var[row]
4050 + tab->n_param - (tab->n_var - tab->n_div);
4052 eq = isl_vec_alloc(tab->mat->ctx, 1+tab->n_param+tab->n_div);
4053 if (!eq)
4054 goto error;
4055 get_row_parameter_line(tab, row, eq->el);
4056 isl_int_neg(eq->el[1 + p], tab->mat->row[row][0]);
4057 eq = isl_vec_normalize(eq);
4059 sol_inc_level(sol);
4060 no_sol_in_strict(sol, tab, eq);
4062 isl_seq_neg(eq->el, eq->el, eq->size);
4063 sol_inc_level(sol);
4064 no_sol_in_strict(sol, tab, eq);
4065 isl_seq_neg(eq->el, eq->el, eq->size);
4067 sol->context->op->add_eq(sol->context, eq->el, 1, 1);
4069 isl_vec_free(eq);
4071 if (isl_tab_mark_redundant(tab, row) < 0)
4072 goto error;
4074 if (sol->context->op->is_empty(sol->context))
4075 break;
4077 row = tab->n_redundant - 1;
4080 saved = sol->context->op->save(sol->context);
4082 find_solutions(sol, tab);
4084 if (sol_has_mergeable_solutions(sol))
4085 sol->context->op->restore(sol->context, saved);
4086 else
4087 sol->context->op->discard(saved);
4089 sol->level = 0;
4090 sol_pop(sol);
4092 return;
4093 error:
4094 isl_tab_free(tab);
4095 sol->error = 1;
4098 /* Check if integer division "div" of "dom" also occurs in "bmap".
4099 * If so, return its position within the divs.
4100 * If not, return -1.
4102 static int find_context_div(struct isl_basic_map *bmap,
4103 struct isl_basic_set *dom, unsigned div)
4105 int i;
4106 unsigned b_dim = isl_space_dim(bmap->dim, isl_dim_all);
4107 unsigned d_dim = isl_space_dim(dom->dim, isl_dim_all);
4109 if (isl_int_is_zero(dom->div[div][0]))
4110 return -1;
4111 if (isl_seq_first_non_zero(dom->div[div] + 2 + d_dim, dom->n_div) != -1)
4112 return -1;
4114 for (i = 0; i < bmap->n_div; ++i) {
4115 if (isl_int_is_zero(bmap->div[i][0]))
4116 continue;
4117 if (isl_seq_first_non_zero(bmap->div[i] + 2 + d_dim,
4118 (b_dim - d_dim) + bmap->n_div) != -1)
4119 continue;
4120 if (isl_seq_eq(bmap->div[i], dom->div[div], 2 + d_dim))
4121 return i;
4123 return -1;
4126 /* The correspondence between the variables in the main tableau,
4127 * the context tableau, and the input map and domain is as follows.
4128 * The first n_param and the last n_div variables of the main tableau
4129 * form the variables of the context tableau.
4130 * In the basic map, these n_param variables correspond to the
4131 * parameters and the input dimensions. In the domain, they correspond
4132 * to the parameters and the set dimensions.
4133 * The n_div variables correspond to the integer divisions in the domain.
4134 * To ensure that everything lines up, we may need to copy some of the
4135 * integer divisions of the domain to the map. These have to be placed
4136 * in the same order as those in the context and they have to be placed
4137 * after any other integer divisions that the map may have.
4138 * This function performs the required reordering.
4140 static struct isl_basic_map *align_context_divs(struct isl_basic_map *bmap,
4141 struct isl_basic_set *dom)
4143 int i;
4144 int common = 0;
4145 int other;
4147 for (i = 0; i < dom->n_div; ++i)
4148 if (find_context_div(bmap, dom, i) != -1)
4149 common++;
4150 other = bmap->n_div - common;
4151 if (dom->n_div - common > 0) {
4152 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
4153 dom->n_div - common, 0, 0);
4154 if (!bmap)
4155 return NULL;
4157 for (i = 0; i < dom->n_div; ++i) {
4158 int pos = find_context_div(bmap, dom, i);
4159 if (pos < 0) {
4160 pos = isl_basic_map_alloc_div(bmap);
4161 if (pos < 0)
4162 goto error;
4163 isl_int_set_si(bmap->div[pos][0], 0);
4165 if (pos != other + i)
4166 isl_basic_map_swap_div(bmap, pos, other + i);
4168 return bmap;
4169 error:
4170 isl_basic_map_free(bmap);
4171 return NULL;
4174 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4175 * some obvious symmetries.
4177 * We make sure the divs in the domain are properly ordered,
4178 * because they will be added one by one in the given order
4179 * during the construction of the solution map.
4180 * Furthermore, make sure that the known integer divisions
4181 * appear before any unknown integer division because the solution
4182 * may depend on the known integer divisions, while anything that
4183 * depends on any variable starting from the first unknown integer
4184 * division is ignored in sol_pma_add.
4186 static struct isl_sol *basic_map_partial_lexopt_base_sol(
4187 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4188 __isl_give isl_set **empty, int max,
4189 struct isl_sol *(*init)(__isl_keep isl_basic_map *bmap,
4190 __isl_take isl_basic_set *dom, int track_empty, int max))
4192 struct isl_tab *tab;
4193 struct isl_sol *sol = NULL;
4194 struct isl_context *context;
4196 if (dom->n_div) {
4197 dom = isl_basic_set_sort_divs(dom);
4198 bmap = align_context_divs(bmap, dom);
4200 sol = init(bmap, dom, !!empty, max);
4201 if (!sol)
4202 goto error;
4204 context = sol->context;
4205 if (isl_basic_set_plain_is_empty(context->op->peek_basic_set(context)))
4206 /* nothing */;
4207 else if (isl_basic_map_plain_is_empty(bmap)) {
4208 if (sol->add_empty)
4209 sol->add_empty(sol,
4210 isl_basic_set_copy(context->op->peek_basic_set(context)));
4211 } else {
4212 tab = tab_for_lexmin(bmap,
4213 context->op->peek_basic_set(context), 1, max);
4214 tab = context->op->detect_nonnegative_parameters(context, tab);
4215 find_solutions_main(sol, tab);
4217 if (sol->error)
4218 goto error;
4220 isl_basic_map_free(bmap);
4221 return sol;
4222 error:
4223 sol_free(sol);
4224 isl_basic_map_free(bmap);
4225 return NULL;
4228 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4229 * some obvious symmetries.
4231 * We call basic_map_partial_lexopt_base_sol and extract the results.
4233 static __isl_give isl_map *basic_map_partial_lexopt_base(
4234 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4235 __isl_give isl_set **empty, int max)
4237 isl_map *result = NULL;
4238 struct isl_sol *sol;
4239 struct isl_sol_map *sol_map;
4241 sol = basic_map_partial_lexopt_base_sol(bmap, dom, empty, max,
4242 &sol_map_init);
4243 if (!sol)
4244 return NULL;
4245 sol_map = (struct isl_sol_map *) sol;
4247 result = isl_map_copy(sol_map->map);
4248 if (empty)
4249 *empty = isl_set_copy(sol_map->empty);
4250 sol_free(&sol_map->sol);
4251 return result;
4254 /* Return a count of the number of occurrences of the "n" first
4255 * variables in the inequality constraints of "bmap".
4257 static __isl_give int *count_occurrences(__isl_keep isl_basic_map *bmap,
4258 int n)
4260 int i, j;
4261 isl_ctx *ctx;
4262 int *occurrences;
4264 if (!bmap)
4265 return NULL;
4266 ctx = isl_basic_map_get_ctx(bmap);
4267 occurrences = isl_calloc_array(ctx, int, n);
4268 if (!occurrences)
4269 return NULL;
4271 for (i = 0; i < bmap->n_ineq; ++i) {
4272 for (j = 0; j < n; ++j) {
4273 if (!isl_int_is_zero(bmap->ineq[i][1 + j]))
4274 occurrences[j]++;
4278 return occurrences;
4281 /* Do all of the "n" variables with non-zero coefficients in "c"
4282 * occur in exactly a single constraint.
4283 * "occurrences" is an array of length "n" containing the number
4284 * of occurrences of each of the variables in the inequality constraints.
4286 static int single_occurrence(int n, isl_int *c, int *occurrences)
4288 int i;
4290 for (i = 0; i < n; ++i) {
4291 if (isl_int_is_zero(c[i]))
4292 continue;
4293 if (occurrences[i] != 1)
4294 return 0;
4297 return 1;
4300 /* Do all of the "n" initial variables that occur in inequality constraint
4301 * "ineq" of "bmap" only occur in that constraint?
4303 static int all_single_occurrence(__isl_keep isl_basic_map *bmap, int ineq,
4304 int n)
4306 int i, j;
4308 for (i = 0; i < n; ++i) {
4309 if (isl_int_is_zero(bmap->ineq[ineq][1 + i]))
4310 continue;
4311 for (j = 0; j < bmap->n_ineq; ++j) {
4312 if (j == ineq)
4313 continue;
4314 if (!isl_int_is_zero(bmap->ineq[j][1 + i]))
4315 return 0;
4319 return 1;
4322 /* Structure used during detection of parallel constraints.
4323 * n_in: number of "input" variables: isl_dim_param + isl_dim_in
4324 * n_out: number of "output" variables: isl_dim_out + isl_dim_div
4325 * val: the coefficients of the output variables
4327 struct isl_constraint_equal_info {
4328 isl_basic_map *bmap;
4329 unsigned n_in;
4330 unsigned n_out;
4331 isl_int *val;
4334 /* Check whether the coefficients of the output variables
4335 * of the constraint in "entry" are equal to info->val.
4337 static int constraint_equal(const void *entry, const void *val)
4339 isl_int **row = (isl_int **)entry;
4340 const struct isl_constraint_equal_info *info = val;
4342 return isl_seq_eq((*row) + 1 + info->n_in, info->val, info->n_out);
4345 /* Check whether "bmap" has a pair of constraints that have
4346 * the same coefficients for the output variables.
4347 * Note that the coefficients of the existentially quantified
4348 * variables need to be zero since the existentially quantified
4349 * of the result are usually not the same as those of the input.
4350 * Furthermore, check that each of the input variables that occur
4351 * in those constraints does not occur in any other constraint.
4352 * If so, return 1 and return the row indices of the two constraints
4353 * in *first and *second.
4355 static int parallel_constraints(__isl_keep isl_basic_map *bmap,
4356 int *first, int *second)
4358 int i;
4359 isl_ctx *ctx;
4360 int *occurrences = NULL;
4361 struct isl_hash_table *table = NULL;
4362 struct isl_hash_table_entry *entry;
4363 struct isl_constraint_equal_info info;
4364 unsigned n_out;
4365 unsigned n_div;
4367 ctx = isl_basic_map_get_ctx(bmap);
4368 table = isl_hash_table_alloc(ctx, bmap->n_ineq);
4369 if (!table)
4370 goto error;
4372 info.n_in = isl_basic_map_dim(bmap, isl_dim_param) +
4373 isl_basic_map_dim(bmap, isl_dim_in);
4374 occurrences = count_occurrences(bmap, info.n_in);
4375 if (info.n_in && !occurrences)
4376 goto error;
4377 info.bmap = bmap;
4378 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4379 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4380 info.n_out = n_out + n_div;
4381 for (i = 0; i < bmap->n_ineq; ++i) {
4382 uint32_t hash;
4384 info.val = bmap->ineq[i] + 1 + info.n_in;
4385 if (isl_seq_first_non_zero(info.val, n_out) < 0)
4386 continue;
4387 if (isl_seq_first_non_zero(info.val + n_out, n_div) >= 0)
4388 continue;
4389 if (!single_occurrence(info.n_in, bmap->ineq[i] + 1,
4390 occurrences))
4391 continue;
4392 hash = isl_seq_get_hash(info.val, info.n_out);
4393 entry = isl_hash_table_find(ctx, table, hash,
4394 constraint_equal, &info, 1);
4395 if (!entry)
4396 goto error;
4397 if (entry->data)
4398 break;
4399 entry->data = &bmap->ineq[i];
4402 if (i < bmap->n_ineq) {
4403 *first = ((isl_int **)entry->data) - bmap->ineq;
4404 *second = i;
4407 isl_hash_table_free(ctx, table);
4408 free(occurrences);
4410 return i < bmap->n_ineq;
4411 error:
4412 isl_hash_table_free(ctx, table);
4413 free(occurrences);
4414 return -1;
4417 /* Given a set of upper bounds in "var", add constraints to "bset"
4418 * that make the i-th bound smallest.
4420 * In particular, if there are n bounds b_i, then add the constraints
4422 * b_i <= b_j for j > i
4423 * b_i < b_j for j < i
4425 static __isl_give isl_basic_set *select_minimum(__isl_take isl_basic_set *bset,
4426 __isl_keep isl_mat *var, int i)
4428 isl_ctx *ctx;
4429 int j, k;
4431 ctx = isl_mat_get_ctx(var);
4433 for (j = 0; j < var->n_row; ++j) {
4434 if (j == i)
4435 continue;
4436 k = isl_basic_set_alloc_inequality(bset);
4437 if (k < 0)
4438 goto error;
4439 isl_seq_combine(bset->ineq[k], ctx->one, var->row[j],
4440 ctx->negone, var->row[i], var->n_col);
4441 isl_int_set_si(bset->ineq[k][var->n_col], 0);
4442 if (j < i)
4443 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
4446 bset = isl_basic_set_finalize(bset);
4448 return bset;
4449 error:
4450 isl_basic_set_free(bset);
4451 return NULL;
4454 /* Given a set of upper bounds on the last "input" variable m,
4455 * construct a set that assigns the minimal upper bound to m, i.e.,
4456 * construct a set that divides the space into cells where one
4457 * of the upper bounds is smaller than all the others and assign
4458 * this upper bound to m.
4460 * In particular, if there are n bounds b_i, then the result
4461 * consists of n basic sets, each one of the form
4463 * m = b_i
4464 * b_i <= b_j for j > i
4465 * b_i < b_j for j < i
4467 static __isl_give isl_set *set_minimum(__isl_take isl_space *dim,
4468 __isl_take isl_mat *var)
4470 int i, k;
4471 isl_basic_set *bset = NULL;
4472 isl_set *set = NULL;
4474 if (!dim || !var)
4475 goto error;
4477 set = isl_set_alloc_space(isl_space_copy(dim),
4478 var->n_row, ISL_SET_DISJOINT);
4480 for (i = 0; i < var->n_row; ++i) {
4481 bset = isl_basic_set_alloc_space(isl_space_copy(dim), 0,
4482 1, var->n_row - 1);
4483 k = isl_basic_set_alloc_equality(bset);
4484 if (k < 0)
4485 goto error;
4486 isl_seq_cpy(bset->eq[k], var->row[i], var->n_col);
4487 isl_int_set_si(bset->eq[k][var->n_col], -1);
4488 bset = select_minimum(bset, var, i);
4489 set = isl_set_add_basic_set(set, bset);
4492 isl_space_free(dim);
4493 isl_mat_free(var);
4494 return set;
4495 error:
4496 isl_basic_set_free(bset);
4497 isl_set_free(set);
4498 isl_space_free(dim);
4499 isl_mat_free(var);
4500 return NULL;
4503 /* Given that the last input variable of "bmap" represents the minimum
4504 * of the bounds in "cst", check whether we need to split the domain
4505 * based on which bound attains the minimum.
4507 * A split is needed when the minimum appears in an integer division
4508 * or in an equality. Otherwise, it is only needed if it appears in
4509 * an upper bound that is different from the upper bounds on which it
4510 * is defined.
4512 static int need_split_basic_map(__isl_keep isl_basic_map *bmap,
4513 __isl_keep isl_mat *cst)
4515 int i, j;
4516 unsigned total;
4517 unsigned pos;
4519 pos = cst->n_col - 1;
4520 total = isl_basic_map_dim(bmap, isl_dim_all);
4522 for (i = 0; i < bmap->n_div; ++i)
4523 if (!isl_int_is_zero(bmap->div[i][2 + pos]))
4524 return 1;
4526 for (i = 0; i < bmap->n_eq; ++i)
4527 if (!isl_int_is_zero(bmap->eq[i][1 + pos]))
4528 return 1;
4530 for (i = 0; i < bmap->n_ineq; ++i) {
4531 if (isl_int_is_nonneg(bmap->ineq[i][1 + pos]))
4532 continue;
4533 if (!isl_int_is_negone(bmap->ineq[i][1 + pos]))
4534 return 1;
4535 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + pos + 1,
4536 total - pos - 1) >= 0)
4537 return 1;
4539 for (j = 0; j < cst->n_row; ++j)
4540 if (isl_seq_eq(bmap->ineq[i], cst->row[j], cst->n_col))
4541 break;
4542 if (j >= cst->n_row)
4543 return 1;
4546 return 0;
4549 /* Given that the last set variable of "bset" represents the minimum
4550 * of the bounds in "cst", check whether we need to split the domain
4551 * based on which bound attains the minimum.
4553 * We simply call need_split_basic_map here. This is safe because
4554 * the position of the minimum is computed from "cst" and not
4555 * from "bmap".
4557 static int need_split_basic_set(__isl_keep isl_basic_set *bset,
4558 __isl_keep isl_mat *cst)
4560 return need_split_basic_map(bset_to_bmap(bset), cst);
4563 /* Given that the last set variable of "set" represents the minimum
4564 * of the bounds in "cst", check whether we need to split the domain
4565 * based on which bound attains the minimum.
4567 static int need_split_set(__isl_keep isl_set *set, __isl_keep isl_mat *cst)
4569 int i;
4571 for (i = 0; i < set->n; ++i)
4572 if (need_split_basic_set(set->p[i], cst))
4573 return 1;
4575 return 0;
4578 /* Given a set of which the last set variable is the minimum
4579 * of the bounds in "cst", split each basic set in the set
4580 * in pieces where one of the bounds is (strictly) smaller than the others.
4581 * This subdivision is given in "min_expr".
4582 * The variable is subsequently projected out.
4584 * We only do the split when it is needed.
4585 * For example if the last input variable m = min(a,b) and the only
4586 * constraints in the given basic set are lower bounds on m,
4587 * i.e., l <= m = min(a,b), then we can simply project out m
4588 * to obtain l <= a and l <= b, without having to split on whether
4589 * m is equal to a or b.
4591 static __isl_give isl_set *split(__isl_take isl_set *empty,
4592 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
4594 int n_in;
4595 int i;
4596 isl_space *dim;
4597 isl_set *res;
4599 if (!empty || !min_expr || !cst)
4600 goto error;
4602 n_in = isl_set_dim(empty, isl_dim_set);
4603 dim = isl_set_get_space(empty);
4604 dim = isl_space_drop_dims(dim, isl_dim_set, n_in - 1, 1);
4605 res = isl_set_empty(dim);
4607 for (i = 0; i < empty->n; ++i) {
4608 isl_set *set;
4610 set = isl_set_from_basic_set(isl_basic_set_copy(empty->p[i]));
4611 if (need_split_basic_set(empty->p[i], cst))
4612 set = isl_set_intersect(set, isl_set_copy(min_expr));
4613 set = isl_set_remove_dims(set, isl_dim_set, n_in - 1, 1);
4615 res = isl_set_union_disjoint(res, set);
4618 isl_set_free(empty);
4619 isl_set_free(min_expr);
4620 isl_mat_free(cst);
4621 return res;
4622 error:
4623 isl_set_free(empty);
4624 isl_set_free(min_expr);
4625 isl_mat_free(cst);
4626 return NULL;
4629 /* Given a map of which the last input variable is the minimum
4630 * of the bounds in "cst", split each basic set in the set
4631 * in pieces where one of the bounds is (strictly) smaller than the others.
4632 * This subdivision is given in "min_expr".
4633 * The variable is subsequently projected out.
4635 * The implementation is essentially the same as that of "split".
4637 static __isl_give isl_map *split_domain(__isl_take isl_map *opt,
4638 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
4640 int n_in;
4641 int i;
4642 isl_space *dim;
4643 isl_map *res;
4645 if (!opt || !min_expr || !cst)
4646 goto error;
4648 n_in = isl_map_dim(opt, isl_dim_in);
4649 dim = isl_map_get_space(opt);
4650 dim = isl_space_drop_dims(dim, isl_dim_in, n_in - 1, 1);
4651 res = isl_map_empty(dim);
4653 for (i = 0; i < opt->n; ++i) {
4654 isl_map *map;
4656 map = isl_map_from_basic_map(isl_basic_map_copy(opt->p[i]));
4657 if (need_split_basic_map(opt->p[i], cst))
4658 map = isl_map_intersect_domain(map,
4659 isl_set_copy(min_expr));
4660 map = isl_map_remove_dims(map, isl_dim_in, n_in - 1, 1);
4662 res = isl_map_union_disjoint(res, map);
4665 isl_map_free(opt);
4666 isl_set_free(min_expr);
4667 isl_mat_free(cst);
4668 return res;
4669 error:
4670 isl_map_free(opt);
4671 isl_set_free(min_expr);
4672 isl_mat_free(cst);
4673 return NULL;
4676 static __isl_give isl_map *basic_map_partial_lexopt(
4677 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4678 __isl_give isl_set **empty, int max);
4680 /* This function is called from basic_map_partial_lexopt_symm.
4681 * The last variable of "bmap" and "dom" corresponds to the minimum
4682 * of the bounds in "cst". "map_space" is the space of the original
4683 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
4684 * is the space of the original domain.
4686 * We recursively call basic_map_partial_lexopt and then plug in
4687 * the definition of the minimum in the result.
4689 static __isl_give isl_map *basic_map_partial_lexopt_symm_core(
4690 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4691 __isl_give isl_set **empty, int max, __isl_take isl_mat *cst,
4692 __isl_take isl_space *map_space, __isl_take isl_space *set_space)
4694 isl_map *opt;
4695 isl_set *min_expr;
4697 min_expr = set_minimum(isl_basic_set_get_space(dom), isl_mat_copy(cst));
4699 opt = basic_map_partial_lexopt(bmap, dom, empty, max);
4701 if (empty) {
4702 *empty = split(*empty,
4703 isl_set_copy(min_expr), isl_mat_copy(cst));
4704 *empty = isl_set_reset_space(*empty, set_space);
4707 opt = split_domain(opt, min_expr, cst);
4708 opt = isl_map_reset_space(opt, map_space);
4710 return opt;
4713 /* Extract a domain from "bmap" for the purpose of computing
4714 * a lexicographic optimum.
4716 * This function is only called when the caller wants to compute a full
4717 * lexicographic optimum, i.e., without specifying a domain. In this case,
4718 * the caller is not interested in the part of the domain space where
4719 * there is no solution and the domain can be initialized to those constraints
4720 * of "bmap" that only involve the parameters and the input dimensions.
4721 * This relieves the parametric programming engine from detecting those
4722 * inequalities and transferring them to the context. More importantly,
4723 * it ensures that those inequalities are transferred first and not
4724 * intermixed with inequalities that actually split the domain.
4726 * If the caller does not require the absence of existentially quantified
4727 * variables in the result (i.e., if ISL_OPT_QE is not set in "flags"),
4728 * then the actual domain of "bmap" can be used. This ensures that
4729 * the domain does not need to be split at all just to separate out
4730 * pieces of the domain that do not have a solution from piece that do.
4731 * This domain cannot be used in general because it may involve
4732 * (unknown) existentially quantified variables which will then also
4733 * appear in the solution.
4735 static __isl_give isl_basic_set *extract_domain(__isl_keep isl_basic_map *bmap,
4736 unsigned flags)
4738 int n_div;
4739 int n_out;
4741 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4742 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4743 bmap = isl_basic_map_copy(bmap);
4744 if (ISL_FL_ISSET(flags, ISL_OPT_QE)) {
4745 bmap = isl_basic_map_drop_constraints_involving_dims(bmap,
4746 isl_dim_div, 0, n_div);
4747 bmap = isl_basic_map_drop_constraints_involving_dims(bmap,
4748 isl_dim_out, 0, n_out);
4750 return isl_basic_map_domain(bmap);
4753 #undef TYPE
4754 #define TYPE isl_map
4755 #undef SUFFIX
4756 #define SUFFIX
4757 #include "isl_tab_lexopt_templ.c"
4759 struct isl_sol_for {
4760 struct isl_sol sol;
4761 int (*fn)(__isl_take isl_basic_set *dom,
4762 __isl_take isl_aff_list *list, void *user);
4763 void *user;
4766 static void sol_for_free(struct isl_sol_for *sol_for)
4768 if (!sol_for)
4769 return;
4770 if (sol_for->sol.context)
4771 sol_for->sol.context->op->free(sol_for->sol.context);
4772 free(sol_for);
4775 static void sol_for_free_wrap(struct isl_sol *sol)
4777 sol_for_free((struct isl_sol_for *)sol);
4780 /* Add the solution identified by the tableau and the context tableau.
4782 * See documentation of sol_add for more details.
4784 * Instead of constructing a basic map, this function calls a user
4785 * defined function with the current context as a basic set and
4786 * a list of affine expressions representing the relation between
4787 * the input and output. The space over which the affine expressions
4788 * are defined is the same as that of the domain. The number of
4789 * affine expressions in the list is equal to the number of output variables.
4791 static void sol_for_add(struct isl_sol_for *sol,
4792 __isl_take isl_basic_set *dom, __isl_take isl_mat *M)
4794 int i;
4795 isl_ctx *ctx;
4796 isl_local_space *ls;
4797 isl_aff *aff;
4798 isl_aff_list *list;
4800 if (sol->sol.error || !dom || !M)
4801 goto error;
4803 ctx = isl_basic_set_get_ctx(dom);
4804 ls = isl_basic_set_get_local_space(dom);
4805 list = isl_aff_list_alloc(ctx, M->n_row - 1);
4806 for (i = 1; i < M->n_row; ++i) {
4807 aff = isl_aff_alloc(isl_local_space_copy(ls));
4808 if (aff) {
4809 isl_int_set(aff->v->el[0], M->row[0][0]);
4810 isl_seq_cpy(aff->v->el + 1, M->row[i], M->n_col);
4812 aff = isl_aff_normalize(aff);
4813 list = isl_aff_list_add(list, aff);
4815 isl_local_space_free(ls);
4817 dom = isl_basic_set_finalize(dom);
4819 if (sol->fn(isl_basic_set_copy(dom), list, sol->user) < 0)
4820 goto error;
4822 isl_basic_set_free(dom);
4823 isl_mat_free(M);
4824 return;
4825 error:
4826 isl_basic_set_free(dom);
4827 isl_mat_free(M);
4828 sol->sol.error = 1;
4831 static void sol_for_add_wrap(struct isl_sol *sol,
4832 __isl_take isl_basic_set *dom, __isl_take isl_mat *M)
4834 sol_for_add((struct isl_sol_for *)sol, dom, M);
4837 static struct isl_sol_for *sol_for_init(__isl_keep isl_basic_map *bmap, int max,
4838 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4839 void *user),
4840 void *user)
4842 struct isl_sol_for *sol_for = NULL;
4843 isl_space *dom_dim;
4844 struct isl_basic_set *dom = NULL;
4846 sol_for = isl_calloc_type(bmap->ctx, struct isl_sol_for);
4847 if (!sol_for)
4848 goto error;
4850 dom_dim = isl_space_domain(isl_space_copy(bmap->dim));
4851 dom = isl_basic_set_universe(dom_dim);
4853 sol_for->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
4854 sol_for->sol.dec_level.callback.run = &sol_dec_level_wrap;
4855 sol_for->sol.dec_level.sol = &sol_for->sol;
4856 sol_for->fn = fn;
4857 sol_for->user = user;
4858 sol_for->sol.max = max;
4859 sol_for->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
4860 sol_for->sol.add = &sol_for_add_wrap;
4861 sol_for->sol.add_empty = NULL;
4862 sol_for->sol.free = &sol_for_free_wrap;
4864 sol_for->sol.context = isl_context_alloc(dom);
4865 if (!sol_for->sol.context)
4866 goto error;
4868 isl_basic_set_free(dom);
4869 return sol_for;
4870 error:
4871 isl_basic_set_free(dom);
4872 sol_for_free(sol_for);
4873 return NULL;
4876 static void sol_for_find_solutions(struct isl_sol_for *sol_for,
4877 struct isl_tab *tab)
4879 find_solutions_main(&sol_for->sol, tab);
4882 int isl_basic_map_foreach_lexopt(__isl_keep isl_basic_map *bmap, int max,
4883 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4884 void *user),
4885 void *user)
4887 struct isl_sol_for *sol_for = NULL;
4889 bmap = isl_basic_map_copy(bmap);
4890 bmap = isl_basic_map_detect_equalities(bmap);
4891 if (!bmap)
4892 return -1;
4894 sol_for = sol_for_init(bmap, max, fn, user);
4895 if (!sol_for)
4896 goto error;
4898 if (isl_basic_map_plain_is_empty(bmap))
4899 /* nothing */;
4900 else {
4901 struct isl_tab *tab;
4902 struct isl_context *context = sol_for->sol.context;
4903 tab = tab_for_lexmin(bmap,
4904 context->op->peek_basic_set(context), 1, max);
4905 tab = context->op->detect_nonnegative_parameters(context, tab);
4906 sol_for_find_solutions(sol_for, tab);
4907 if (sol_for->sol.error)
4908 goto error;
4911 sol_free(&sol_for->sol);
4912 isl_basic_map_free(bmap);
4913 return 0;
4914 error:
4915 sol_free(&sol_for->sol);
4916 isl_basic_map_free(bmap);
4917 return -1;
4920 int isl_basic_set_foreach_lexopt(__isl_keep isl_basic_set *bset, int max,
4921 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4922 void *user),
4923 void *user)
4925 return isl_basic_map_foreach_lexopt(bset, max, fn, user);
4928 /* Check if the given sequence of len variables starting at pos
4929 * represents a trivial (i.e., zero) solution.
4930 * The variables are assumed to be non-negative and to come in pairs,
4931 * with each pair representing a variable of unrestricted sign.
4932 * The solution is trivial if each such pair in the sequence consists
4933 * of two identical values, meaning that the variable being represented
4934 * has value zero.
4936 static int region_is_trivial(struct isl_tab *tab, int pos, int len)
4938 int i;
4940 if (len == 0)
4941 return 0;
4943 for (i = 0; i < len; i += 2) {
4944 int neg_row;
4945 int pos_row;
4947 neg_row = tab->var[pos + i].is_row ?
4948 tab->var[pos + i].index : -1;
4949 pos_row = tab->var[pos + i + 1].is_row ?
4950 tab->var[pos + i + 1].index : -1;
4952 if ((neg_row < 0 ||
4953 isl_int_is_zero(tab->mat->row[neg_row][1])) &&
4954 (pos_row < 0 ||
4955 isl_int_is_zero(tab->mat->row[pos_row][1])))
4956 continue;
4958 if (neg_row < 0 || pos_row < 0)
4959 return 0;
4960 if (isl_int_ne(tab->mat->row[neg_row][1],
4961 tab->mat->row[pos_row][1]))
4962 return 0;
4965 return 1;
4968 /* Return the index of the first trivial region or -1 if all regions
4969 * are non-trivial.
4971 static int first_trivial_region(struct isl_tab *tab,
4972 int n_region, struct isl_region *region)
4974 int i;
4976 for (i = 0; i < n_region; ++i) {
4977 if (region_is_trivial(tab, region[i].pos, region[i].len))
4978 return i;
4981 return -1;
4984 /* Check if the solution is optimal, i.e., whether the first
4985 * n_op entries are zero.
4987 static int is_optimal(__isl_keep isl_vec *sol, int n_op)
4989 int i;
4991 for (i = 0; i < n_op; ++i)
4992 if (!isl_int_is_zero(sol->el[1 + i]))
4993 return 0;
4994 return 1;
4997 /* Add constraints to "tab" that ensure that any solution is significantly
4998 * better than that represented by "sol". That is, find the first
4999 * relevant (within first n_op) non-zero coefficient and force it (along
5000 * with all previous coefficients) to be zero.
5001 * If the solution is already optimal (all relevant coefficients are zero),
5002 * then just mark the table as empty.
5004 * This function assumes that at least 2 * n_op more rows and at least
5005 * 2 * n_op more elements in the constraint array are available in the tableau.
5007 static int force_better_solution(struct isl_tab *tab,
5008 __isl_keep isl_vec *sol, int n_op)
5010 int i;
5011 isl_ctx *ctx;
5012 isl_vec *v = NULL;
5014 if (!sol)
5015 return -1;
5017 for (i = 0; i < n_op; ++i)
5018 if (!isl_int_is_zero(sol->el[1 + i]))
5019 break;
5021 if (i == n_op) {
5022 if (isl_tab_mark_empty(tab) < 0)
5023 return -1;
5024 return 0;
5027 ctx = isl_vec_get_ctx(sol);
5028 v = isl_vec_alloc(ctx, 1 + tab->n_var);
5029 if (!v)
5030 return -1;
5032 for (; i >= 0; --i) {
5033 v = isl_vec_clr(v);
5034 isl_int_set_si(v->el[1 + i], -1);
5035 if (add_lexmin_eq(tab, v->el) < 0)
5036 goto error;
5039 isl_vec_free(v);
5040 return 0;
5041 error:
5042 isl_vec_free(v);
5043 return -1;
5046 struct isl_trivial {
5047 int update;
5048 int region;
5049 int side;
5050 struct isl_tab_undo *snap;
5053 /* Return the lexicographically smallest non-trivial solution of the
5054 * given ILP problem.
5056 * All variables are assumed to be non-negative.
5058 * n_op is the number of initial coordinates to optimize.
5059 * That is, once a solution has been found, we will only continue looking
5060 * for solution that result in significantly better values for those
5061 * initial coordinates. That is, we only continue looking for solutions
5062 * that increase the number of initial zeros in this sequence.
5064 * A solution is non-trivial, if it is non-trivial on each of the
5065 * specified regions. Each region represents a sequence of pairs
5066 * of variables. A solution is non-trivial on such a region if
5067 * at least one of these pairs consists of different values, i.e.,
5068 * such that the non-negative variable represented by the pair is non-zero.
5070 * Whenever a conflict is encountered, all constraints involved are
5071 * reported to the caller through a call to "conflict".
5073 * We perform a simple branch-and-bound backtracking search.
5074 * Each level in the search represents initially trivial region that is forced
5075 * to be non-trivial.
5076 * At each level we consider n cases, where n is the length of the region.
5077 * In terms of the n/2 variables of unrestricted signs being encoded by
5078 * the region, we consider the cases
5079 * x_0 >= 1
5080 * x_0 <= -1
5081 * x_0 = 0 and x_1 >= 1
5082 * x_0 = 0 and x_1 <= -1
5083 * x_0 = 0 and x_1 = 0 and x_2 >= 1
5084 * x_0 = 0 and x_1 = 0 and x_2 <= -1
5085 * ...
5086 * The cases are considered in this order, assuming that each pair
5087 * x_i_a x_i_b represents the value x_i_b - x_i_a.
5088 * That is, x_0 >= 1 is enforced by adding the constraint
5089 * x_0_b - x_0_a >= 1
5091 __isl_give isl_vec *isl_tab_basic_set_non_trivial_lexmin(
5092 __isl_take isl_basic_set *bset, int n_op, int n_region,
5093 struct isl_region *region,
5094 int (*conflict)(int con, void *user), void *user)
5096 int i, j;
5097 int r;
5098 isl_ctx *ctx;
5099 isl_vec *v = NULL;
5100 isl_vec *sol = NULL;
5101 struct isl_tab *tab;
5102 struct isl_trivial *triv = NULL;
5103 int level, init;
5105 if (!bset)
5106 return NULL;
5108 ctx = isl_basic_set_get_ctx(bset);
5109 sol = isl_vec_alloc(ctx, 0);
5111 tab = tab_for_lexmin(bset, NULL, 0, 0);
5112 if (!tab)
5113 goto error;
5114 tab->conflict = conflict;
5115 tab->conflict_user = user;
5117 v = isl_vec_alloc(ctx, 1 + tab->n_var);
5118 triv = isl_calloc_array(ctx, struct isl_trivial, n_region);
5119 if (!v || (n_region && !triv))
5120 goto error;
5122 level = 0;
5123 init = 1;
5125 while (level >= 0) {
5126 int side, base;
5128 if (init) {
5129 tab = cut_to_integer_lexmin(tab, CUT_ONE);
5130 if (!tab)
5131 goto error;
5132 if (tab->empty)
5133 goto backtrack;
5134 r = first_trivial_region(tab, n_region, region);
5135 if (r < 0) {
5136 for (i = 0; i < level; ++i)
5137 triv[i].update = 1;
5138 isl_vec_free(sol);
5139 sol = isl_tab_get_sample_value(tab);
5140 if (!sol)
5141 goto error;
5142 if (is_optimal(sol, n_op))
5143 break;
5144 goto backtrack;
5146 if (level >= n_region)
5147 isl_die(ctx, isl_error_internal,
5148 "nesting level too deep", goto error);
5149 if (isl_tab_extend_cons(tab,
5150 2 * region[r].len + 2 * n_op) < 0)
5151 goto error;
5152 triv[level].region = r;
5153 triv[level].side = 0;
5156 r = triv[level].region;
5157 side = triv[level].side;
5158 base = 2 * (side/2);
5160 if (side >= region[r].len) {
5161 backtrack:
5162 level--;
5163 init = 0;
5164 if (level >= 0)
5165 if (isl_tab_rollback(tab, triv[level].snap) < 0)
5166 goto error;
5167 continue;
5170 if (triv[level].update) {
5171 if (force_better_solution(tab, sol, n_op) < 0)
5172 goto error;
5173 triv[level].update = 0;
5176 if (side == base && base >= 2) {
5177 for (j = base - 2; j < base; ++j) {
5178 v = isl_vec_clr(v);
5179 isl_int_set_si(v->el[1 + region[r].pos + j], 1);
5180 if (add_lexmin_eq(tab, v->el) < 0)
5181 goto error;
5185 triv[level].snap = isl_tab_snap(tab);
5186 if (isl_tab_push_basis(tab) < 0)
5187 goto error;
5189 v = isl_vec_clr(v);
5190 isl_int_set_si(v->el[0], -1);
5191 isl_int_set_si(v->el[1 + region[r].pos + side], -1);
5192 isl_int_set_si(v->el[1 + region[r].pos + (side ^ 1)], 1);
5193 tab = add_lexmin_ineq(tab, v->el);
5195 triv[level].side++;
5196 level++;
5197 init = 1;
5200 free(triv);
5201 isl_vec_free(v);
5202 isl_tab_free(tab);
5203 isl_basic_set_free(bset);
5205 return sol;
5206 error:
5207 free(triv);
5208 isl_vec_free(v);
5209 isl_tab_free(tab);
5210 isl_basic_set_free(bset);
5211 isl_vec_free(sol);
5212 return NULL;
5215 /* Wrapper for a tableau that is used for computing
5216 * the lexicographically smallest rational point of a non-negative set.
5217 * This point is represented by the sample value of "tab",
5218 * unless "tab" is empty.
5220 struct isl_tab_lexmin {
5221 isl_ctx *ctx;
5222 struct isl_tab *tab;
5225 /* Free "tl" and return NULL.
5227 __isl_null isl_tab_lexmin *isl_tab_lexmin_free(__isl_take isl_tab_lexmin *tl)
5229 if (!tl)
5230 return NULL;
5231 isl_ctx_deref(tl->ctx);
5232 isl_tab_free(tl->tab);
5233 free(tl);
5235 return NULL;
5238 /* Construct an isl_tab_lexmin for computing
5239 * the lexicographically smallest rational point in "bset",
5240 * assuming that all variables are non-negative.
5242 __isl_give isl_tab_lexmin *isl_tab_lexmin_from_basic_set(
5243 __isl_take isl_basic_set *bset)
5245 isl_ctx *ctx;
5246 isl_tab_lexmin *tl;
5248 if (!bset)
5249 return NULL;
5251 ctx = isl_basic_set_get_ctx(bset);
5252 tl = isl_calloc_type(ctx, struct isl_tab_lexmin);
5253 if (!tl)
5254 goto error;
5255 tl->ctx = ctx;
5256 isl_ctx_ref(ctx);
5257 tl->tab = tab_for_lexmin(bset, NULL, 0, 0);
5258 isl_basic_set_free(bset);
5259 if (!tl->tab)
5260 return isl_tab_lexmin_free(tl);
5261 return tl;
5262 error:
5263 isl_basic_set_free(bset);
5264 isl_tab_lexmin_free(tl);
5265 return NULL;
5268 /* Return the dimension of the set represented by "tl".
5270 int isl_tab_lexmin_dim(__isl_keep isl_tab_lexmin *tl)
5272 return tl ? tl->tab->n_var : -1;
5275 /* Add the equality with coefficients "eq" to "tl", updating the optimal
5276 * solution if needed.
5277 * The equality is added as two opposite inequality constraints.
5279 __isl_give isl_tab_lexmin *isl_tab_lexmin_add_eq(__isl_take isl_tab_lexmin *tl,
5280 isl_int *eq)
5282 unsigned n_var;
5284 if (!tl || !eq)
5285 return isl_tab_lexmin_free(tl);
5287 if (isl_tab_extend_cons(tl->tab, 2) < 0)
5288 return isl_tab_lexmin_free(tl);
5289 n_var = tl->tab->n_var;
5290 isl_seq_neg(eq, eq, 1 + n_var);
5291 tl->tab = add_lexmin_ineq(tl->tab, eq);
5292 isl_seq_neg(eq, eq, 1 + n_var);
5293 tl->tab = add_lexmin_ineq(tl->tab, eq);
5295 if (!tl->tab)
5296 return isl_tab_lexmin_free(tl);
5298 return tl;
5301 /* Return the lexicographically smallest rational point in the basic set
5302 * from which "tl" was constructed.
5303 * If the original input was empty, then return a zero-length vector.
5305 __isl_give isl_vec *isl_tab_lexmin_get_solution(__isl_keep isl_tab_lexmin *tl)
5307 if (!tl)
5308 return NULL;
5309 if (tl->tab->empty)
5310 return isl_vec_alloc(tl->ctx, 0);
5311 else
5312 return isl_tab_get_sample_value(tl->tab);
5315 /* Return the lexicographically smallest rational point in "bset",
5316 * assuming that all variables are non-negative.
5317 * If "bset" is empty, then return a zero-length vector.
5319 __isl_give isl_vec *isl_tab_basic_set_non_neg_lexmin(
5320 __isl_take isl_basic_set *bset)
5322 isl_tab_lexmin *tl;
5323 isl_vec *sol;
5325 tl = isl_tab_lexmin_from_basic_set(bset);
5326 sol = isl_tab_lexmin_get_solution(tl);
5327 isl_tab_lexmin_free(tl);
5328 return sol;
5331 struct isl_sol_pma {
5332 struct isl_sol sol;
5333 isl_pw_multi_aff *pma;
5334 isl_set *empty;
5337 static void sol_pma_free(struct isl_sol_pma *sol_pma)
5339 if (!sol_pma)
5340 return;
5341 if (sol_pma->sol.context)
5342 sol_pma->sol.context->op->free(sol_pma->sol.context);
5343 isl_pw_multi_aff_free(sol_pma->pma);
5344 isl_set_free(sol_pma->empty);
5345 free(sol_pma);
5348 /* This function is called for parts of the context where there is
5349 * no solution, with "bset" corresponding to the context tableau.
5350 * Simply add the basic set to the set "empty".
5352 static void sol_pma_add_empty(struct isl_sol_pma *sol,
5353 __isl_take isl_basic_set *bset)
5355 if (!bset || !sol->empty)
5356 goto error;
5358 sol->empty = isl_set_grow(sol->empty, 1);
5359 bset = isl_basic_set_simplify(bset);
5360 bset = isl_basic_set_finalize(bset);
5361 sol->empty = isl_set_add_basic_set(sol->empty, bset);
5362 if (!sol->empty)
5363 sol->sol.error = 1;
5364 return;
5365 error:
5366 isl_basic_set_free(bset);
5367 sol->sol.error = 1;
5370 /* Check that the final columns of "M", starting at "first", are zero.
5372 static isl_stat check_final_columns_are_zero(__isl_keep isl_mat *M,
5373 unsigned first)
5375 int i;
5376 unsigned rows, cols, n;
5378 if (!M)
5379 return isl_stat_error;
5380 rows = isl_mat_rows(M);
5381 cols = isl_mat_cols(M);
5382 n = cols - first;
5383 for (i = 0; i < rows; ++i)
5384 if (isl_seq_first_non_zero(M->row[i] + first, n) != -1)
5385 isl_die(isl_mat_get_ctx(M), isl_error_internal,
5386 "final columns should be zero",
5387 return isl_stat_error);
5388 return isl_stat_ok;
5391 /* Set the affine expressions in "ma" according to the rows in "M", which
5392 * are defined over the local space "ls".
5393 * The matrix "M" may have extra (zero) columns beyond the number
5394 * of variables in "ls".
5396 static __isl_give isl_multi_aff *set_from_affine_matrix(
5397 __isl_take isl_multi_aff *ma, __isl_take isl_local_space *ls,
5398 __isl_take isl_mat *M)
5400 int i, dim;
5401 isl_aff *aff;
5403 if (!ma || !ls || !M)
5404 goto error;
5406 dim = isl_local_space_dim(ls, isl_dim_all);
5407 if (check_final_columns_are_zero(M, 1 + dim) < 0)
5408 goto error;
5409 for (i = 1; i < M->n_row; ++i) {
5410 aff = isl_aff_alloc(isl_local_space_copy(ls));
5411 if (aff) {
5412 isl_int_set(aff->v->el[0], M->row[0][0]);
5413 isl_seq_cpy(aff->v->el + 1, M->row[i], 1 + dim);
5415 aff = isl_aff_normalize(aff);
5416 ma = isl_multi_aff_set_aff(ma, i - 1, aff);
5418 isl_local_space_free(ls);
5419 isl_mat_free(M);
5421 return ma;
5422 error:
5423 isl_local_space_free(ls);
5424 isl_mat_free(M);
5425 isl_multi_aff_free(ma);
5426 return NULL;
5429 /* Given a basic set "dom" that represents the context and an affine
5430 * matrix "M" that maps the dimensions of the context to the
5431 * output variables, construct an isl_pw_multi_aff with a single
5432 * cell corresponding to "dom" and affine expressions copied from "M".
5434 * Note that the description of the initial context may have involved
5435 * existentially quantified variables, in which case they also appear
5436 * in "dom". These need to be removed before creating the affine
5437 * expression because an affine expression cannot be defined in terms
5438 * of existentially quantified variables without a known representation.
5439 * Since newly added integer divisions are inserted before these
5440 * existentially quantified variables, they are still in the final
5441 * positions and the corresponding final columns of "M" are zero
5442 * because align_context_divs adds the existentially quantified
5443 * variables of the context to the main tableau without any constraints and
5444 * any equality constraints that are added later on can only serve
5445 * to eliminate these existentially quantified variables.
5447 static void sol_pma_add(struct isl_sol_pma *sol,
5448 __isl_take isl_basic_set *dom, __isl_take isl_mat *M)
5450 isl_local_space *ls;
5451 isl_multi_aff *maff;
5452 isl_pw_multi_aff *pma;
5453 int n_div, n_known;
5455 n_div = isl_basic_set_dim(dom, isl_dim_div);
5456 n_known = n_div - sol->sol.context->n_unknown;
5458 maff = isl_multi_aff_alloc(isl_pw_multi_aff_get_space(sol->pma));
5459 ls = isl_basic_set_get_local_space(dom);
5460 ls = isl_local_space_drop_dims(ls, isl_dim_div,
5461 n_known, n_div - n_known);
5462 maff = set_from_affine_matrix(maff, ls, M);
5463 dom = isl_basic_set_simplify(dom);
5464 dom = isl_basic_set_finalize(dom);
5465 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(dom), maff);
5466 sol->pma = isl_pw_multi_aff_add_disjoint(sol->pma, pma);
5467 if (!sol->pma)
5468 sol->sol.error = 1;
5471 static void sol_pma_free_wrap(struct isl_sol *sol)
5473 sol_pma_free((struct isl_sol_pma *)sol);
5476 static void sol_pma_add_empty_wrap(struct isl_sol *sol,
5477 __isl_take isl_basic_set *bset)
5479 sol_pma_add_empty((struct isl_sol_pma *)sol, bset);
5482 static void sol_pma_add_wrap(struct isl_sol *sol,
5483 __isl_take isl_basic_set *dom, __isl_take isl_mat *M)
5485 sol_pma_add((struct isl_sol_pma *)sol, dom, M);
5488 /* Construct an isl_sol_pma structure for accumulating the solution.
5489 * If track_empty is set, then we also keep track of the parts
5490 * of the context where there is no solution.
5491 * If max is set, then we are solving a maximization, rather than
5492 * a minimization problem, which means that the variables in the
5493 * tableau have value "M - x" rather than "M + x".
5495 static struct isl_sol *sol_pma_init(__isl_keep isl_basic_map *bmap,
5496 __isl_take isl_basic_set *dom, int track_empty, int max)
5498 struct isl_sol_pma *sol_pma = NULL;
5500 if (!bmap)
5501 goto error;
5503 sol_pma = isl_calloc_type(bmap->ctx, struct isl_sol_pma);
5504 if (!sol_pma)
5505 goto error;
5507 sol_pma->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
5508 sol_pma->sol.dec_level.callback.run = &sol_dec_level_wrap;
5509 sol_pma->sol.dec_level.sol = &sol_pma->sol;
5510 sol_pma->sol.max = max;
5511 sol_pma->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
5512 sol_pma->sol.add = &sol_pma_add_wrap;
5513 sol_pma->sol.add_empty = track_empty ? &sol_pma_add_empty_wrap : NULL;
5514 sol_pma->sol.free = &sol_pma_free_wrap;
5515 sol_pma->pma = isl_pw_multi_aff_empty(isl_basic_map_get_space(bmap));
5516 if (!sol_pma->pma)
5517 goto error;
5519 sol_pma->sol.context = isl_context_alloc(dom);
5520 if (!sol_pma->sol.context)
5521 goto error;
5523 if (track_empty) {
5524 sol_pma->empty = isl_set_alloc_space(isl_basic_set_get_space(dom),
5525 1, ISL_SET_DISJOINT);
5526 if (!sol_pma->empty)
5527 goto error;
5530 isl_basic_set_free(dom);
5531 return &sol_pma->sol;
5532 error:
5533 isl_basic_set_free(dom);
5534 sol_pma_free(sol_pma);
5535 return NULL;
5538 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
5539 * some obvious symmetries.
5541 * We call basic_map_partial_lexopt_base_sol and extract the results.
5543 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_base_pw_multi_aff(
5544 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5545 __isl_give isl_set **empty, int max)
5547 isl_pw_multi_aff *result = NULL;
5548 struct isl_sol *sol;
5549 struct isl_sol_pma *sol_pma;
5551 sol = basic_map_partial_lexopt_base_sol(bmap, dom, empty, max,
5552 &sol_pma_init);
5553 if (!sol)
5554 return NULL;
5555 sol_pma = (struct isl_sol_pma *) sol;
5557 result = isl_pw_multi_aff_copy(sol_pma->pma);
5558 if (empty)
5559 *empty = isl_set_copy(sol_pma->empty);
5560 sol_free(&sol_pma->sol);
5561 return result;
5564 /* Given that the last input variable of "maff" represents the minimum
5565 * of some bounds, check whether we need to plug in the expression
5566 * of the minimum.
5568 * In particular, check if the last input variable appears in any
5569 * of the expressions in "maff".
5571 static int need_substitution(__isl_keep isl_multi_aff *maff)
5573 int i;
5574 unsigned pos;
5576 pos = isl_multi_aff_dim(maff, isl_dim_in) - 1;
5578 for (i = 0; i < maff->n; ++i)
5579 if (isl_aff_involves_dims(maff->p[i], isl_dim_in, pos, 1))
5580 return 1;
5582 return 0;
5585 /* Given a set of upper bounds on the last "input" variable m,
5586 * construct a piecewise affine expression that selects
5587 * the minimal upper bound to m, i.e.,
5588 * divide the space into cells where one
5589 * of the upper bounds is smaller than all the others and select
5590 * this upper bound on that cell.
5592 * In particular, if there are n bounds b_i, then the result
5593 * consists of n cell, each one of the form
5595 * b_i <= b_j for j > i
5596 * b_i < b_j for j < i
5598 * The affine expression on this cell is
5600 * b_i
5602 static __isl_give isl_pw_aff *set_minimum_pa(__isl_take isl_space *space,
5603 __isl_take isl_mat *var)
5605 int i;
5606 isl_aff *aff = NULL;
5607 isl_basic_set *bset = NULL;
5608 isl_pw_aff *paff = NULL;
5609 isl_space *pw_space;
5610 isl_local_space *ls = NULL;
5612 if (!space || !var)
5613 goto error;
5615 ls = isl_local_space_from_space(isl_space_copy(space));
5616 pw_space = isl_space_copy(space);
5617 pw_space = isl_space_from_domain(pw_space);
5618 pw_space = isl_space_add_dims(pw_space, isl_dim_out, 1);
5619 paff = isl_pw_aff_alloc_size(pw_space, var->n_row);
5621 for (i = 0; i < var->n_row; ++i) {
5622 isl_pw_aff *paff_i;
5624 aff = isl_aff_alloc(isl_local_space_copy(ls));
5625 bset = isl_basic_set_alloc_space(isl_space_copy(space), 0,
5626 0, var->n_row - 1);
5627 if (!aff || !bset)
5628 goto error;
5629 isl_int_set_si(aff->v->el[0], 1);
5630 isl_seq_cpy(aff->v->el + 1, var->row[i], var->n_col);
5631 isl_int_set_si(aff->v->el[1 + var->n_col], 0);
5632 bset = select_minimum(bset, var, i);
5633 paff_i = isl_pw_aff_alloc(isl_set_from_basic_set(bset), aff);
5634 paff = isl_pw_aff_add_disjoint(paff, paff_i);
5637 isl_local_space_free(ls);
5638 isl_space_free(space);
5639 isl_mat_free(var);
5640 return paff;
5641 error:
5642 isl_aff_free(aff);
5643 isl_basic_set_free(bset);
5644 isl_pw_aff_free(paff);
5645 isl_local_space_free(ls);
5646 isl_space_free(space);
5647 isl_mat_free(var);
5648 return NULL;
5651 /* Given a piecewise multi-affine expression of which the last input variable
5652 * is the minimum of the bounds in "cst", plug in the value of the minimum.
5653 * This minimum expression is given in "min_expr_pa".
5654 * The set "min_expr" contains the same information, but in the form of a set.
5655 * The variable is subsequently projected out.
5657 * The implementation is similar to those of "split" and "split_domain".
5658 * If the variable appears in a given expression, then minimum expression
5659 * is plugged in. Otherwise, if the variable appears in the constraints
5660 * and a split is required, then the domain is split. Otherwise, no split
5661 * is performed.
5663 static __isl_give isl_pw_multi_aff *split_domain_pma(
5664 __isl_take isl_pw_multi_aff *opt, __isl_take isl_pw_aff *min_expr_pa,
5665 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
5667 int n_in;
5668 int i;
5669 isl_space *space;
5670 isl_pw_multi_aff *res;
5672 if (!opt || !min_expr || !cst)
5673 goto error;
5675 n_in = isl_pw_multi_aff_dim(opt, isl_dim_in);
5676 space = isl_pw_multi_aff_get_space(opt);
5677 space = isl_space_drop_dims(space, isl_dim_in, n_in - 1, 1);
5678 res = isl_pw_multi_aff_empty(space);
5680 for (i = 0; i < opt->n; ++i) {
5681 isl_pw_multi_aff *pma;
5683 pma = isl_pw_multi_aff_alloc(isl_set_copy(opt->p[i].set),
5684 isl_multi_aff_copy(opt->p[i].maff));
5685 if (need_substitution(opt->p[i].maff))
5686 pma = isl_pw_multi_aff_substitute(pma,
5687 isl_dim_in, n_in - 1, min_expr_pa);
5688 else if (need_split_set(opt->p[i].set, cst))
5689 pma = isl_pw_multi_aff_intersect_domain(pma,
5690 isl_set_copy(min_expr));
5691 pma = isl_pw_multi_aff_project_out(pma,
5692 isl_dim_in, n_in - 1, 1);
5694 res = isl_pw_multi_aff_add_disjoint(res, pma);
5697 isl_pw_multi_aff_free(opt);
5698 isl_pw_aff_free(min_expr_pa);
5699 isl_set_free(min_expr);
5700 isl_mat_free(cst);
5701 return res;
5702 error:
5703 isl_pw_multi_aff_free(opt);
5704 isl_pw_aff_free(min_expr_pa);
5705 isl_set_free(min_expr);
5706 isl_mat_free(cst);
5707 return NULL;
5710 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_pw_multi_aff(
5711 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5712 __isl_give isl_set **empty, int max);
5714 /* This function is called from basic_map_partial_lexopt_symm.
5715 * The last variable of "bmap" and "dom" corresponds to the minimum
5716 * of the bounds in "cst". "map_space" is the space of the original
5717 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
5718 * is the space of the original domain.
5720 * We recursively call basic_map_partial_lexopt and then plug in
5721 * the definition of the minimum in the result.
5723 static __isl_give isl_pw_multi_aff *
5724 basic_map_partial_lexopt_symm_core_pw_multi_aff(
5725 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5726 __isl_give isl_set **empty, int max, __isl_take isl_mat *cst,
5727 __isl_take isl_space *map_space, __isl_take isl_space *set_space)
5729 isl_pw_multi_aff *opt;
5730 isl_pw_aff *min_expr_pa;
5731 isl_set *min_expr;
5733 min_expr = set_minimum(isl_basic_set_get_space(dom), isl_mat_copy(cst));
5734 min_expr_pa = set_minimum_pa(isl_basic_set_get_space(dom),
5735 isl_mat_copy(cst));
5737 opt = basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, max);
5739 if (empty) {
5740 *empty = split(*empty,
5741 isl_set_copy(min_expr), isl_mat_copy(cst));
5742 *empty = isl_set_reset_space(*empty, set_space);
5745 opt = split_domain_pma(opt, min_expr_pa, min_expr, cst);
5746 opt = isl_pw_multi_aff_reset_space(opt, map_space);
5748 return opt;
5751 #undef TYPE
5752 #define TYPE isl_pw_multi_aff
5753 #undef SUFFIX
5754 #define SUFFIX _pw_multi_aff
5755 #include "isl_tab_lexopt_templ.c"