isl_schedule_constraints.c: extract out shared isl_schedule_constraints_set
[isl.git] / isl_tab_pip.c
blobd9515bc8df0e754c3fe9587aa821dac1dd9cd59c
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2016 Sven Verdoolaege
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
14 #include <isl_ctx_private.h>
15 #include "isl_map_private.h"
16 #include <isl_seq.h>
17 #include "isl_tab.h"
18 #include "isl_sample.h"
19 #include <isl_mat_private.h>
20 #include <isl_vec_private.h>
21 #include <isl_aff_private.h>
22 #include <isl_constraint_private.h>
23 #include <isl_options_private.h>
24 #include <isl_config.h>
27 * The implementation of parametric integer linear programming in this file
28 * was inspired by the paper "Parametric Integer Programming" and the
29 * report "Solving systems of affine (in)equalities" by Paul Feautrier
30 * (and others).
32 * The strategy used for obtaining a feasible solution is different
33 * from the one used in isl_tab.c. In particular, in isl_tab.c,
34 * upon finding a constraint that is not yet satisfied, we pivot
35 * in a row that increases the constant term of the row holding the
36 * constraint, making sure the sample solution remains feasible
37 * for all the constraints it already satisfied.
38 * Here, we always pivot in the row holding the constraint,
39 * choosing a column that induces the lexicographically smallest
40 * increment to the sample solution.
42 * By starting out from a sample value that is lexicographically
43 * smaller than any integer point in the problem space, the first
44 * feasible integer sample point we find will also be the lexicographically
45 * smallest. If all variables can be assumed to be non-negative,
46 * then the initial sample value may be chosen equal to zero.
47 * However, we will not make this assumption. Instead, we apply
48 * the "big parameter" trick. Any variable x is then not directly
49 * used in the tableau, but instead it is represented by another
50 * variable x' = M + x, where M is an arbitrarily large (positive)
51 * value. x' is therefore always non-negative, whatever the value of x.
52 * Taking as initial sample value x' = 0 corresponds to x = -M,
53 * which is always smaller than any possible value of x.
55 * The big parameter trick is used in the main tableau and
56 * also in the context tableau if isl_context_lex is used.
57 * In this case, each tableaus has its own big parameter.
58 * Before doing any real work, we check if all the parameters
59 * happen to be non-negative. If so, we drop the column corresponding
60 * to M from the initial context tableau.
61 * If isl_context_gbr is used, then the big parameter trick is only
62 * used in the main tableau.
65 struct isl_context;
66 struct isl_context_op {
67 /* detect nonnegative parameters in context and mark them in tab */
68 struct isl_tab *(*detect_nonnegative_parameters)(
69 struct isl_context *context, struct isl_tab *tab);
70 /* return temporary reference to basic set representation of context */
71 struct isl_basic_set *(*peek_basic_set)(struct isl_context *context);
72 /* return temporary reference to tableau representation of context */
73 struct isl_tab *(*peek_tab)(struct isl_context *context);
74 /* add equality; check is 1 if eq may not be valid;
75 * update is 1 if we may want to call ineq_sign on context later.
77 void (*add_eq)(struct isl_context *context, isl_int *eq,
78 int check, int update);
79 /* add inequality; check is 1 if ineq may not be valid;
80 * update is 1 if we may want to call ineq_sign on context later.
82 void (*add_ineq)(struct isl_context *context, isl_int *ineq,
83 int check, int update);
84 /* check sign of ineq based on previous information.
85 * strict is 1 if saturation should be treated as a positive sign.
87 enum isl_tab_row_sign (*ineq_sign)(struct isl_context *context,
88 isl_int *ineq, int strict);
89 /* check if inequality maintains feasibility */
90 int (*test_ineq)(struct isl_context *context, isl_int *ineq);
91 /* return index of a div that corresponds to "div" */
92 int (*get_div)(struct isl_context *context, struct isl_tab *tab,
93 struct isl_vec *div);
94 /* insert div "div" to context at "pos" and return non-negativity */
95 isl_bool (*insert_div)(struct isl_context *context, int pos,
96 __isl_keep isl_vec *div);
97 int (*detect_equalities)(struct isl_context *context,
98 struct isl_tab *tab);
99 /* return row index of "best" split */
100 int (*best_split)(struct isl_context *context, struct isl_tab *tab);
101 /* check if context has already been determined to be empty */
102 int (*is_empty)(struct isl_context *context);
103 /* check if context is still usable */
104 int (*is_ok)(struct isl_context *context);
105 /* save a copy/snapshot of context */
106 void *(*save)(struct isl_context *context);
107 /* restore saved context */
108 void (*restore)(struct isl_context *context, void *);
109 /* discard saved context */
110 void (*discard)(void *);
111 /* invalidate context */
112 void (*invalidate)(struct isl_context *context);
113 /* free context */
114 __isl_null struct isl_context *(*free)(struct isl_context *context);
117 /* Shared parts of context representation.
119 * "n_unknown" is the number of final unknown integer divisions
120 * in the input domain.
122 struct isl_context {
123 struct isl_context_op *op;
124 int n_unknown;
127 struct isl_context_lex {
128 struct isl_context context;
129 struct isl_tab *tab;
132 /* A stack (linked list) of solutions of subtrees of the search space.
134 * "M" describes the solution in terms of the dimensions of "dom".
135 * The number of columns of "M" is one more than the total number
136 * of dimensions of "dom".
138 * If "M" is NULL, then there is no solution on "dom".
140 struct isl_partial_sol {
141 int level;
142 struct isl_basic_set *dom;
143 struct isl_mat *M;
145 struct isl_partial_sol *next;
148 struct isl_sol;
149 struct isl_sol_callback {
150 struct isl_tab_callback callback;
151 struct isl_sol *sol;
154 /* isl_sol is an interface for constructing a solution to
155 * a parametric integer linear programming problem.
156 * Every time the algorithm reaches a state where a solution
157 * can be read off from the tableau (including cases where the tableau
158 * is empty), the function "add" is called on the isl_sol passed
159 * to find_solutions_main.
161 * The context tableau is owned by isl_sol and is updated incrementally.
163 * There are currently two implementations of this interface,
164 * isl_sol_map, which simply collects the solutions in an isl_map
165 * and (optionally) the parts of the context where there is no solution
166 * in an isl_set, and
167 * isl_sol_for, which calls a user-defined function for each part of
168 * the solution.
170 struct isl_sol {
171 int error;
172 int rational;
173 int level;
174 int max;
175 int n_out;
176 struct isl_context *context;
177 struct isl_partial_sol *partial;
178 void (*add)(struct isl_sol *sol,
179 struct isl_basic_set *dom, struct isl_mat *M);
180 void (*add_empty)(struct isl_sol *sol, struct isl_basic_set *bset);
181 void (*free)(struct isl_sol *sol);
182 struct isl_sol_callback dec_level;
185 static void sol_free(struct isl_sol *sol)
187 struct isl_partial_sol *partial, *next;
188 if (!sol)
189 return;
190 for (partial = sol->partial; partial; partial = next) {
191 next = partial->next;
192 isl_basic_set_free(partial->dom);
193 isl_mat_free(partial->M);
194 free(partial);
196 sol->free(sol);
199 /* Push a partial solution represented by a domain and mapping M
200 * onto the stack of partial solutions.
202 static void sol_push_sol(struct isl_sol *sol,
203 struct isl_basic_set *dom, struct isl_mat *M)
205 struct isl_partial_sol *partial;
207 if (sol->error || !dom)
208 goto error;
210 partial = isl_alloc_type(dom->ctx, struct isl_partial_sol);
211 if (!partial)
212 goto error;
214 partial->level = sol->level;
215 partial->dom = dom;
216 partial->M = M;
217 partial->next = sol->partial;
219 sol->partial = partial;
221 return;
222 error:
223 isl_basic_set_free(dom);
224 isl_mat_free(M);
225 sol->error = 1;
228 /* Pop one partial solution from the partial solution stack and
229 * pass it on to sol->add or sol->add_empty.
231 static void sol_pop_one(struct isl_sol *sol)
233 struct isl_partial_sol *partial;
235 partial = sol->partial;
236 sol->partial = partial->next;
238 if (partial->M)
239 sol->add(sol, partial->dom, partial->M);
240 else
241 sol->add_empty(sol, partial->dom);
242 free(partial);
245 /* Return a fresh copy of the domain represented by the context tableau.
247 static struct isl_basic_set *sol_domain(struct isl_sol *sol)
249 struct isl_basic_set *bset;
251 if (sol->error)
252 return NULL;
254 bset = isl_basic_set_dup(sol->context->op->peek_basic_set(sol->context));
255 bset = isl_basic_set_update_from_tab(bset,
256 sol->context->op->peek_tab(sol->context));
258 return bset;
261 /* Check whether two partial solutions have the same mapping, where n_div
262 * is the number of divs that the two partial solutions have in common.
264 static int same_solution(struct isl_partial_sol *s1, struct isl_partial_sol *s2,
265 unsigned n_div)
267 int i;
268 unsigned dim;
270 if (!s1->M != !s2->M)
271 return 0;
272 if (!s1->M)
273 return 1;
275 dim = isl_basic_set_total_dim(s1->dom) - s1->dom->n_div;
277 for (i = 0; i < s1->M->n_row; ++i) {
278 if (isl_seq_first_non_zero(s1->M->row[i]+1+dim+n_div,
279 s1->M->n_col-1-dim-n_div) != -1)
280 return 0;
281 if (isl_seq_first_non_zero(s2->M->row[i]+1+dim+n_div,
282 s2->M->n_col-1-dim-n_div) != -1)
283 return 0;
284 if (!isl_seq_eq(s1->M->row[i], s2->M->row[i], 1+dim+n_div))
285 return 0;
287 return 1;
290 /* Pop all solutions from the partial solution stack that were pushed onto
291 * the stack at levels that are deeper than the current level.
292 * If the two topmost elements on the stack have the same level
293 * and represent the same solution, then their domains are combined.
294 * This combined domain is the same as the current context domain
295 * as sol_pop is called each time we move back to a higher level.
296 * If the outer level (0) has been reached, then all partial solutions
297 * at the current level are also popped off.
299 static void sol_pop(struct isl_sol *sol)
301 struct isl_partial_sol *partial;
302 unsigned n_div;
304 if (sol->error)
305 return;
307 partial = sol->partial;
308 if (!partial)
309 return;
311 if (partial->level == 0 && sol->level == 0) {
312 for (partial = sol->partial; partial; partial = sol->partial)
313 sol_pop_one(sol);
314 return;
317 if (partial->level <= sol->level)
318 return;
320 if (partial->next && partial->next->level == partial->level) {
321 n_div = isl_basic_set_dim(
322 sol->context->op->peek_basic_set(sol->context),
323 isl_dim_div);
325 if (!same_solution(partial, partial->next, n_div)) {
326 sol_pop_one(sol);
327 sol_pop_one(sol);
328 } else {
329 struct isl_basic_set *bset;
330 isl_mat *M;
331 unsigned n;
333 n = isl_basic_set_dim(partial->next->dom, isl_dim_div);
334 n -= n_div;
335 bset = sol_domain(sol);
336 isl_basic_set_free(partial->next->dom);
337 partial->next->dom = bset;
338 M = partial->next->M;
339 if (M) {
340 M = isl_mat_drop_cols(M, M->n_col - n, n);
341 partial->next->M = M;
342 if (!M)
343 goto error;
345 partial->next->level = sol->level;
347 if (!bset)
348 goto error;
350 sol->partial = partial->next;
351 isl_basic_set_free(partial->dom);
352 isl_mat_free(partial->M);
353 free(partial);
355 } else
356 sol_pop_one(sol);
358 if (sol->level == 0) {
359 for (partial = sol->partial; partial; partial = sol->partial)
360 sol_pop_one(sol);
361 return;
364 if (0)
365 error: sol->error = 1;
368 static void sol_dec_level(struct isl_sol *sol)
370 if (sol->error)
371 return;
373 sol->level--;
375 sol_pop(sol);
378 static int sol_dec_level_wrap(struct isl_tab_callback *cb)
380 struct isl_sol_callback *callback = (struct isl_sol_callback *)cb;
382 sol_dec_level(callback->sol);
384 return callback->sol->error ? -1 : 0;
387 /* Move down to next level and push callback onto context tableau
388 * to decrease the level again when it gets rolled back across
389 * the current state. That is, dec_level will be called with
390 * the context tableau in the same state as it is when inc_level
391 * is called.
393 static void sol_inc_level(struct isl_sol *sol)
395 struct isl_tab *tab;
397 if (sol->error)
398 return;
400 sol->level++;
401 tab = sol->context->op->peek_tab(sol->context);
402 if (isl_tab_push_callback(tab, &sol->dec_level.callback) < 0)
403 sol->error = 1;
406 static void scale_rows(struct isl_mat *mat, isl_int m, int n_row)
408 int i;
410 if (isl_int_is_one(m))
411 return;
413 for (i = 0; i < n_row; ++i)
414 isl_seq_scale(mat->row[i], mat->row[i], m, mat->n_col);
417 /* Add the solution identified by the tableau and the context tableau.
419 * The layout of the variables is as follows.
420 * tab->n_var is equal to the total number of variables in the input
421 * map (including divs that were copied from the context)
422 * + the number of extra divs constructed
423 * Of these, the first tab->n_param and the last tab->n_div variables
424 * correspond to the variables in the context, i.e.,
425 * tab->n_param + tab->n_div = context_tab->n_var
426 * tab->n_param is equal to the number of parameters and input
427 * dimensions in the input map
428 * tab->n_div is equal to the number of divs in the context
430 * If there is no solution, then call add_empty with a basic set
431 * that corresponds to the context tableau. (If add_empty is NULL,
432 * then do nothing).
434 * If there is a solution, then first construct a matrix that maps
435 * all dimensions of the context to the output variables, i.e.,
436 * the output dimensions in the input map.
437 * The divs in the input map (if any) that do not correspond to any
438 * div in the context do not appear in the solution.
439 * The algorithm will make sure that they have an integer value,
440 * but these values themselves are of no interest.
441 * We have to be careful not to drop or rearrange any divs in the
442 * context because that would change the meaning of the matrix.
444 * To extract the value of the output variables, it should be noted
445 * that we always use a big parameter M in the main tableau and so
446 * the variable stored in this tableau is not an output variable x itself, but
447 * x' = M + x (in case of minimization)
448 * or
449 * x' = M - x (in case of maximization)
450 * If x' appears in a column, then its optimal value is zero,
451 * which means that the optimal value of x is an unbounded number
452 * (-M for minimization and M for maximization).
453 * We currently assume that the output dimensions in the original map
454 * are bounded, so this cannot occur.
455 * Similarly, when x' appears in a row, then the coefficient of M in that
456 * row is necessarily 1.
457 * If the row in the tableau represents
458 * d x' = c + d M + e(y)
459 * then, in case of minimization, the corresponding row in the matrix
460 * will be
461 * a c + a e(y)
462 * with a d = m, the (updated) common denominator of the matrix.
463 * In case of maximization, the row will be
464 * -a c - a e(y)
466 static void sol_add(struct isl_sol *sol, struct isl_tab *tab)
468 struct isl_basic_set *bset = NULL;
469 struct isl_mat *mat = NULL;
470 unsigned off;
471 int row;
472 isl_int m;
474 if (sol->error || !tab)
475 goto error;
477 if (tab->empty && !sol->add_empty)
478 return;
479 if (sol->context->op->is_empty(sol->context))
480 return;
482 bset = sol_domain(sol);
484 if (tab->empty) {
485 sol_push_sol(sol, bset, NULL);
486 return;
489 off = 2 + tab->M;
491 mat = isl_mat_alloc(tab->mat->ctx, 1 + sol->n_out,
492 1 + tab->n_param + tab->n_div);
493 if (!mat)
494 goto error;
496 isl_int_init(m);
498 isl_seq_clr(mat->row[0] + 1, mat->n_col - 1);
499 isl_int_set_si(mat->row[0][0], 1);
500 for (row = 0; row < sol->n_out; ++row) {
501 int i = tab->n_param + row;
502 int r, j;
504 isl_seq_clr(mat->row[1 + row], mat->n_col);
505 if (!tab->var[i].is_row) {
506 if (tab->M)
507 isl_die(mat->ctx, isl_error_invalid,
508 "unbounded optimum", goto error2);
509 continue;
512 r = tab->var[i].index;
513 if (tab->M &&
514 isl_int_ne(tab->mat->row[r][2], tab->mat->row[r][0]))
515 isl_die(mat->ctx, isl_error_invalid,
516 "unbounded optimum", goto error2);
517 isl_int_gcd(m, mat->row[0][0], tab->mat->row[r][0]);
518 isl_int_divexact(m, tab->mat->row[r][0], m);
519 scale_rows(mat, m, 1 + row);
520 isl_int_divexact(m, mat->row[0][0], tab->mat->row[r][0]);
521 isl_int_mul(mat->row[1 + row][0], m, tab->mat->row[r][1]);
522 for (j = 0; j < tab->n_param; ++j) {
523 int col;
524 if (tab->var[j].is_row)
525 continue;
526 col = tab->var[j].index;
527 isl_int_mul(mat->row[1 + row][1 + j], m,
528 tab->mat->row[r][off + col]);
530 for (j = 0; j < tab->n_div; ++j) {
531 int col;
532 if (tab->var[tab->n_var - tab->n_div+j].is_row)
533 continue;
534 col = tab->var[tab->n_var - tab->n_div+j].index;
535 isl_int_mul(mat->row[1 + row][1 + tab->n_param + j], m,
536 tab->mat->row[r][off + col]);
538 if (sol->max)
539 isl_seq_neg(mat->row[1 + row], mat->row[1 + row],
540 mat->n_col);
543 isl_int_clear(m);
545 sol_push_sol(sol, bset, mat);
546 return;
547 error2:
548 isl_int_clear(m);
549 error:
550 isl_basic_set_free(bset);
551 isl_mat_free(mat);
552 sol->error = 1;
555 struct isl_sol_map {
556 struct isl_sol sol;
557 struct isl_map *map;
558 struct isl_set *empty;
561 static void sol_map_free(struct isl_sol_map *sol_map)
563 if (!sol_map)
564 return;
565 if (sol_map->sol.context)
566 sol_map->sol.context->op->free(sol_map->sol.context);
567 isl_map_free(sol_map->map);
568 isl_set_free(sol_map->empty);
569 free(sol_map);
572 static void sol_map_free_wrap(struct isl_sol *sol)
574 sol_map_free((struct isl_sol_map *)sol);
577 /* This function is called for parts of the context where there is
578 * no solution, with "bset" corresponding to the context tableau.
579 * Simply add the basic set to the set "empty".
581 static void sol_map_add_empty(struct isl_sol_map *sol,
582 struct isl_basic_set *bset)
584 if (!bset || !sol->empty)
585 goto error;
587 sol->empty = isl_set_grow(sol->empty, 1);
588 bset = isl_basic_set_simplify(bset);
589 bset = isl_basic_set_finalize(bset);
590 sol->empty = isl_set_add_basic_set(sol->empty, isl_basic_set_copy(bset));
591 if (!sol->empty)
592 goto error;
593 isl_basic_set_free(bset);
594 return;
595 error:
596 isl_basic_set_free(bset);
597 sol->sol.error = 1;
600 static void sol_map_add_empty_wrap(struct isl_sol *sol,
601 struct isl_basic_set *bset)
603 sol_map_add_empty((struct isl_sol_map *)sol, bset);
606 /* Given a basic map "dom" that represents the context and an affine
607 * matrix "M" that maps the dimensions of the context to the
608 * output variables, construct a basic map with the same parameters
609 * and divs as the context, the dimensions of the context as input
610 * dimensions and a number of output dimensions that is equal to
611 * the number of output dimensions in the input map.
613 * The constraints and divs of the context are simply copied
614 * from "dom". For each row
615 * x = c + e(y)
616 * an equality
617 * c + e(y) - d x = 0
618 * is added, with d the common denominator of M.
620 static void sol_map_add(struct isl_sol_map *sol,
621 struct isl_basic_set *dom, struct isl_mat *M)
623 int i;
624 struct isl_basic_map *bmap = NULL;
625 unsigned n_eq;
626 unsigned n_ineq;
627 unsigned nparam;
628 unsigned total;
629 unsigned n_div;
630 unsigned n_out;
632 if (sol->sol.error || !dom || !M)
633 goto error;
635 n_out = sol->sol.n_out;
636 n_eq = dom->n_eq + n_out;
637 n_ineq = dom->n_ineq;
638 n_div = dom->n_div;
639 nparam = isl_basic_set_total_dim(dom) - n_div;
640 total = isl_map_dim(sol->map, isl_dim_all);
641 bmap = isl_basic_map_alloc_space(isl_map_get_space(sol->map),
642 n_div, n_eq, 2 * n_div + n_ineq);
643 if (!bmap)
644 goto error;
645 if (sol->sol.rational)
646 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
647 for (i = 0; i < dom->n_div; ++i) {
648 int k = isl_basic_map_alloc_div(bmap);
649 if (k < 0)
650 goto error;
651 isl_seq_cpy(bmap->div[k], dom->div[i], 1 + 1 + nparam);
652 isl_seq_clr(bmap->div[k] + 1 + 1 + nparam, total - nparam);
653 isl_seq_cpy(bmap->div[k] + 1 + 1 + total,
654 dom->div[i] + 1 + 1 + nparam, i);
656 for (i = 0; i < dom->n_eq; ++i) {
657 int k = isl_basic_map_alloc_equality(bmap);
658 if (k < 0)
659 goto error;
660 isl_seq_cpy(bmap->eq[k], dom->eq[i], 1 + nparam);
661 isl_seq_clr(bmap->eq[k] + 1 + nparam, total - nparam);
662 isl_seq_cpy(bmap->eq[k] + 1 + total,
663 dom->eq[i] + 1 + nparam, n_div);
665 for (i = 0; i < dom->n_ineq; ++i) {
666 int k = isl_basic_map_alloc_inequality(bmap);
667 if (k < 0)
668 goto error;
669 isl_seq_cpy(bmap->ineq[k], dom->ineq[i], 1 + nparam);
670 isl_seq_clr(bmap->ineq[k] + 1 + nparam, total - nparam);
671 isl_seq_cpy(bmap->ineq[k] + 1 + total,
672 dom->ineq[i] + 1 + nparam, n_div);
674 for (i = 0; i < M->n_row - 1; ++i) {
675 int k = isl_basic_map_alloc_equality(bmap);
676 if (k < 0)
677 goto error;
678 isl_seq_cpy(bmap->eq[k], M->row[1 + i], 1 + nparam);
679 isl_seq_clr(bmap->eq[k] + 1 + nparam, n_out);
680 isl_int_neg(bmap->eq[k][1 + nparam + i], M->row[0][0]);
681 isl_seq_cpy(bmap->eq[k] + 1 + nparam + n_out,
682 M->row[1 + i] + 1 + nparam, n_div);
684 bmap = isl_basic_map_simplify(bmap);
685 bmap = isl_basic_map_finalize(bmap);
686 sol->map = isl_map_grow(sol->map, 1);
687 sol->map = isl_map_add_basic_map(sol->map, bmap);
688 isl_basic_set_free(dom);
689 isl_mat_free(M);
690 if (!sol->map)
691 sol->sol.error = 1;
692 return;
693 error:
694 isl_basic_set_free(dom);
695 isl_mat_free(M);
696 isl_basic_map_free(bmap);
697 sol->sol.error = 1;
700 static void sol_map_add_wrap(struct isl_sol *sol,
701 struct isl_basic_set *dom, struct isl_mat *M)
703 sol_map_add((struct isl_sol_map *)sol, dom, M);
707 /* Store the "parametric constant" of row "row" of tableau "tab" in "line",
708 * i.e., the constant term and the coefficients of all variables that
709 * appear in the context tableau.
710 * Note that the coefficient of the big parameter M is NOT copied.
711 * The context tableau may not have a big parameter and even when it
712 * does, it is a different big parameter.
714 static void get_row_parameter_line(struct isl_tab *tab, int row, isl_int *line)
716 int i;
717 unsigned off = 2 + tab->M;
719 isl_int_set(line[0], tab->mat->row[row][1]);
720 for (i = 0; i < tab->n_param; ++i) {
721 if (tab->var[i].is_row)
722 isl_int_set_si(line[1 + i], 0);
723 else {
724 int col = tab->var[i].index;
725 isl_int_set(line[1 + i], tab->mat->row[row][off + col]);
728 for (i = 0; i < tab->n_div; ++i) {
729 if (tab->var[tab->n_var - tab->n_div + i].is_row)
730 isl_int_set_si(line[1 + tab->n_param + i], 0);
731 else {
732 int col = tab->var[tab->n_var - tab->n_div + i].index;
733 isl_int_set(line[1 + tab->n_param + i],
734 tab->mat->row[row][off + col]);
739 /* Check if rows "row1" and "row2" have identical "parametric constants",
740 * as explained above.
741 * In this case, we also insist that the coefficients of the big parameter
742 * be the same as the values of the constants will only be the same
743 * if these coefficients are also the same.
745 static int identical_parameter_line(struct isl_tab *tab, int row1, int row2)
747 int i;
748 unsigned off = 2 + tab->M;
750 if (isl_int_ne(tab->mat->row[row1][1], tab->mat->row[row2][1]))
751 return 0;
753 if (tab->M && isl_int_ne(tab->mat->row[row1][2],
754 tab->mat->row[row2][2]))
755 return 0;
757 for (i = 0; i < tab->n_param + tab->n_div; ++i) {
758 int pos = i < tab->n_param ? i :
759 tab->n_var - tab->n_div + i - tab->n_param;
760 int col;
762 if (tab->var[pos].is_row)
763 continue;
764 col = tab->var[pos].index;
765 if (isl_int_ne(tab->mat->row[row1][off + col],
766 tab->mat->row[row2][off + col]))
767 return 0;
769 return 1;
772 /* Return an inequality that expresses that the "parametric constant"
773 * should be non-negative.
774 * This function is only called when the coefficient of the big parameter
775 * is equal to zero.
777 static struct isl_vec *get_row_parameter_ineq(struct isl_tab *tab, int row)
779 struct isl_vec *ineq;
781 ineq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_param + tab->n_div);
782 if (!ineq)
783 return NULL;
785 get_row_parameter_line(tab, row, ineq->el);
786 if (ineq)
787 ineq = isl_vec_normalize(ineq);
789 return ineq;
792 /* Normalize a div expression of the form
794 * [(g*f(x) + c)/(g * m)]
796 * with c the constant term and f(x) the remaining coefficients, to
798 * [(f(x) + [c/g])/m]
800 static void normalize_div(__isl_keep isl_vec *div)
802 isl_ctx *ctx = isl_vec_get_ctx(div);
803 int len = div->size - 2;
805 isl_seq_gcd(div->el + 2, len, &ctx->normalize_gcd);
806 isl_int_gcd(ctx->normalize_gcd, ctx->normalize_gcd, div->el[0]);
808 if (isl_int_is_one(ctx->normalize_gcd))
809 return;
811 isl_int_divexact(div->el[0], div->el[0], ctx->normalize_gcd);
812 isl_int_fdiv_q(div->el[1], div->el[1], ctx->normalize_gcd);
813 isl_seq_scale_down(div->el + 2, div->el + 2, ctx->normalize_gcd, len);
816 /* Return an integer division for use in a parametric cut based
817 * on the given row.
818 * In particular, let the parametric constant of the row be
820 * \sum_i a_i y_i
822 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
823 * The div returned is equal to
825 * floor(\sum_i {-a_i} y_i) = floor((\sum_i (-a_i mod d) y_i)/d)
827 static struct isl_vec *get_row_parameter_div(struct isl_tab *tab, int row)
829 struct isl_vec *div;
831 div = isl_vec_alloc(tab->mat->ctx, 1 + 1 + tab->n_param + tab->n_div);
832 if (!div)
833 return NULL;
835 isl_int_set(div->el[0], tab->mat->row[row][0]);
836 get_row_parameter_line(tab, row, div->el + 1);
837 isl_seq_neg(div->el + 1, div->el + 1, div->size - 1);
838 normalize_div(div);
839 isl_seq_fdiv_r(div->el + 1, div->el + 1, div->el[0], div->size - 1);
841 return div;
844 /* Return an integer division for use in transferring an integrality constraint
845 * to the context.
846 * In particular, let the parametric constant of the row be
848 * \sum_i a_i y_i
850 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
851 * The the returned div is equal to
853 * floor(\sum_i {a_i} y_i) = floor((\sum_i (a_i mod d) y_i)/d)
855 static struct isl_vec *get_row_split_div(struct isl_tab *tab, int row)
857 struct isl_vec *div;
859 div = isl_vec_alloc(tab->mat->ctx, 1 + 1 + tab->n_param + tab->n_div);
860 if (!div)
861 return NULL;
863 isl_int_set(div->el[0], tab->mat->row[row][0]);
864 get_row_parameter_line(tab, row, div->el + 1);
865 normalize_div(div);
866 isl_seq_fdiv_r(div->el + 1, div->el + 1, div->el[0], div->size - 1);
868 return div;
871 /* Construct and return an inequality that expresses an upper bound
872 * on the given div.
873 * In particular, if the div is given by
875 * d = floor(e/m)
877 * then the inequality expresses
879 * m d <= e
881 static struct isl_vec *ineq_for_div(struct isl_basic_set *bset, unsigned div)
883 unsigned total;
884 unsigned div_pos;
885 struct isl_vec *ineq;
887 if (!bset)
888 return NULL;
890 total = isl_basic_set_total_dim(bset);
891 div_pos = 1 + total - bset->n_div + div;
893 ineq = isl_vec_alloc(bset->ctx, 1 + total);
894 if (!ineq)
895 return NULL;
897 isl_seq_cpy(ineq->el, bset->div[div] + 1, 1 + total);
898 isl_int_neg(ineq->el[div_pos], bset->div[div][0]);
899 return ineq;
902 /* Given a row in the tableau and a div that was created
903 * using get_row_split_div and that has been constrained to equality, i.e.,
905 * d = floor(\sum_i {a_i} y_i) = \sum_i {a_i} y_i
907 * replace the expression "\sum_i {a_i} y_i" in the row by d,
908 * i.e., we subtract "\sum_i {a_i} y_i" and add 1 d.
909 * The coefficients of the non-parameters in the tableau have been
910 * verified to be integral. We can therefore simply replace coefficient b
911 * by floor(b). For the coefficients of the parameters we have
912 * floor(a_i) = a_i - {a_i}, while for the other coefficients, we have
913 * floor(b) = b.
915 static struct isl_tab *set_row_cst_to_div(struct isl_tab *tab, int row, int div)
917 isl_seq_fdiv_q(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
918 tab->mat->row[row][0], 1 + tab->M + tab->n_col);
920 isl_int_set_si(tab->mat->row[row][0], 1);
922 if (tab->var[tab->n_var - tab->n_div + div].is_row) {
923 int drow = tab->var[tab->n_var - tab->n_div + div].index;
925 isl_assert(tab->mat->ctx,
926 isl_int_is_one(tab->mat->row[drow][0]), goto error);
927 isl_seq_combine(tab->mat->row[row] + 1,
928 tab->mat->ctx->one, tab->mat->row[row] + 1,
929 tab->mat->ctx->one, tab->mat->row[drow] + 1,
930 1 + tab->M + tab->n_col);
931 } else {
932 int dcol = tab->var[tab->n_var - tab->n_div + div].index;
934 isl_int_add_ui(tab->mat->row[row][2 + tab->M + dcol],
935 tab->mat->row[row][2 + tab->M + dcol], 1);
938 return tab;
939 error:
940 isl_tab_free(tab);
941 return NULL;
944 /* Check if the (parametric) constant of the given row is obviously
945 * negative, meaning that we don't need to consult the context tableau.
946 * If there is a big parameter and its coefficient is non-zero,
947 * then this coefficient determines the outcome.
948 * Otherwise, we check whether the constant is negative and
949 * all non-zero coefficients of parameters are negative and
950 * belong to non-negative parameters.
952 static int is_obviously_neg(struct isl_tab *tab, int row)
954 int i;
955 int col;
956 unsigned off = 2 + tab->M;
958 if (tab->M) {
959 if (isl_int_is_pos(tab->mat->row[row][2]))
960 return 0;
961 if (isl_int_is_neg(tab->mat->row[row][2]))
962 return 1;
965 if (isl_int_is_nonneg(tab->mat->row[row][1]))
966 return 0;
967 for (i = 0; i < tab->n_param; ++i) {
968 /* Eliminated parameter */
969 if (tab->var[i].is_row)
970 continue;
971 col = tab->var[i].index;
972 if (isl_int_is_zero(tab->mat->row[row][off + col]))
973 continue;
974 if (!tab->var[i].is_nonneg)
975 return 0;
976 if (isl_int_is_pos(tab->mat->row[row][off + col]))
977 return 0;
979 for (i = 0; i < tab->n_div; ++i) {
980 if (tab->var[tab->n_var - tab->n_div + i].is_row)
981 continue;
982 col = tab->var[tab->n_var - tab->n_div + i].index;
983 if (isl_int_is_zero(tab->mat->row[row][off + col]))
984 continue;
985 if (!tab->var[tab->n_var - tab->n_div + i].is_nonneg)
986 return 0;
987 if (isl_int_is_pos(tab->mat->row[row][off + col]))
988 return 0;
990 return 1;
993 /* Check if the (parametric) constant of the given row is obviously
994 * non-negative, meaning that we don't need to consult the context tableau.
995 * If there is a big parameter and its coefficient is non-zero,
996 * then this coefficient determines the outcome.
997 * Otherwise, we check whether the constant is non-negative and
998 * all non-zero coefficients of parameters are positive and
999 * belong to non-negative parameters.
1001 static int is_obviously_nonneg(struct isl_tab *tab, int row)
1003 int i;
1004 int col;
1005 unsigned off = 2 + tab->M;
1007 if (tab->M) {
1008 if (isl_int_is_pos(tab->mat->row[row][2]))
1009 return 1;
1010 if (isl_int_is_neg(tab->mat->row[row][2]))
1011 return 0;
1014 if (isl_int_is_neg(tab->mat->row[row][1]))
1015 return 0;
1016 for (i = 0; i < tab->n_param; ++i) {
1017 /* Eliminated parameter */
1018 if (tab->var[i].is_row)
1019 continue;
1020 col = tab->var[i].index;
1021 if (isl_int_is_zero(tab->mat->row[row][off + col]))
1022 continue;
1023 if (!tab->var[i].is_nonneg)
1024 return 0;
1025 if (isl_int_is_neg(tab->mat->row[row][off + col]))
1026 return 0;
1028 for (i = 0; i < tab->n_div; ++i) {
1029 if (tab->var[tab->n_var - tab->n_div + i].is_row)
1030 continue;
1031 col = tab->var[tab->n_var - tab->n_div + i].index;
1032 if (isl_int_is_zero(tab->mat->row[row][off + col]))
1033 continue;
1034 if (!tab->var[tab->n_var - tab->n_div + i].is_nonneg)
1035 return 0;
1036 if (isl_int_is_neg(tab->mat->row[row][off + col]))
1037 return 0;
1039 return 1;
1042 /* Given a row r and two columns, return the column that would
1043 * lead to the lexicographically smallest increment in the sample
1044 * solution when leaving the basis in favor of the row.
1045 * Pivoting with column c will increment the sample value by a non-negative
1046 * constant times a_{V,c}/a_{r,c}, with a_{V,c} the elements of column c
1047 * corresponding to the non-parametric variables.
1048 * If variable v appears in a column c_v, the a_{v,c} = 1 iff c = c_v,
1049 * with all other entries in this virtual row equal to zero.
1050 * If variable v appears in a row, then a_{v,c} is the element in column c
1051 * of that row.
1053 * Let v be the first variable with a_{v,c1}/a_{r,c1} != a_{v,c2}/a_{r,c2}.
1054 * Then if a_{v,c1}/a_{r,c1} < a_{v,c2}/a_{r,c2}, i.e.,
1055 * a_{v,c2} a_{r,c1} - a_{v,c1} a_{r,c2} > 0, c1 results in the minimal
1056 * increment. Otherwise, it's c2.
1058 static int lexmin_col_pair(struct isl_tab *tab,
1059 int row, int col1, int col2, isl_int tmp)
1061 int i;
1062 isl_int *tr;
1064 tr = tab->mat->row[row] + 2 + tab->M;
1066 for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
1067 int s1, s2;
1068 isl_int *r;
1070 if (!tab->var[i].is_row) {
1071 if (tab->var[i].index == col1)
1072 return col2;
1073 if (tab->var[i].index == col2)
1074 return col1;
1075 continue;
1078 if (tab->var[i].index == row)
1079 continue;
1081 r = tab->mat->row[tab->var[i].index] + 2 + tab->M;
1082 s1 = isl_int_sgn(r[col1]);
1083 s2 = isl_int_sgn(r[col2]);
1084 if (s1 == 0 && s2 == 0)
1085 continue;
1086 if (s1 < s2)
1087 return col1;
1088 if (s2 < s1)
1089 return col2;
1091 isl_int_mul(tmp, r[col2], tr[col1]);
1092 isl_int_submul(tmp, r[col1], tr[col2]);
1093 if (isl_int_is_pos(tmp))
1094 return col1;
1095 if (isl_int_is_neg(tmp))
1096 return col2;
1098 return -1;
1101 /* Given a row in the tableau, find and return the column that would
1102 * result in the lexicographically smallest, but positive, increment
1103 * in the sample point.
1104 * If there is no such column, then return tab->n_col.
1105 * If anything goes wrong, return -1.
1107 static int lexmin_pivot_col(struct isl_tab *tab, int row)
1109 int j;
1110 int col = tab->n_col;
1111 isl_int *tr;
1112 isl_int tmp;
1114 tr = tab->mat->row[row] + 2 + tab->M;
1116 isl_int_init(tmp);
1118 for (j = tab->n_dead; j < tab->n_col; ++j) {
1119 if (tab->col_var[j] >= 0 &&
1120 (tab->col_var[j] < tab->n_param ||
1121 tab->col_var[j] >= tab->n_var - tab->n_div))
1122 continue;
1124 if (!isl_int_is_pos(tr[j]))
1125 continue;
1127 if (col == tab->n_col)
1128 col = j;
1129 else
1130 col = lexmin_col_pair(tab, row, col, j, tmp);
1131 isl_assert(tab->mat->ctx, col >= 0, goto error);
1134 isl_int_clear(tmp);
1135 return col;
1136 error:
1137 isl_int_clear(tmp);
1138 return -1;
1141 /* Return the first known violated constraint, i.e., a non-negative
1142 * constraint that currently has an either obviously negative value
1143 * or a previously determined to be negative value.
1145 * If any constraint has a negative coefficient for the big parameter,
1146 * if any, then we return one of these first.
1148 static int first_neg(struct isl_tab *tab)
1150 int row;
1152 if (tab->M)
1153 for (row = tab->n_redundant; row < tab->n_row; ++row) {
1154 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
1155 continue;
1156 if (!isl_int_is_neg(tab->mat->row[row][2]))
1157 continue;
1158 if (tab->row_sign)
1159 tab->row_sign[row] = isl_tab_row_neg;
1160 return row;
1162 for (row = tab->n_redundant; row < tab->n_row; ++row) {
1163 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
1164 continue;
1165 if (tab->row_sign) {
1166 if (tab->row_sign[row] == 0 &&
1167 is_obviously_neg(tab, row))
1168 tab->row_sign[row] = isl_tab_row_neg;
1169 if (tab->row_sign[row] != isl_tab_row_neg)
1170 continue;
1171 } else if (!is_obviously_neg(tab, row))
1172 continue;
1173 return row;
1175 return -1;
1178 /* Check whether the invariant that all columns are lexico-positive
1179 * is satisfied. This function is not called from the current code
1180 * but is useful during debugging.
1182 static void check_lexpos(struct isl_tab *tab) __attribute__ ((unused));
1183 static void check_lexpos(struct isl_tab *tab)
1185 unsigned off = 2 + tab->M;
1186 int col;
1187 int var;
1188 int row;
1190 for (col = tab->n_dead; col < tab->n_col; ++col) {
1191 if (tab->col_var[col] >= 0 &&
1192 (tab->col_var[col] < tab->n_param ||
1193 tab->col_var[col] >= tab->n_var - tab->n_div))
1194 continue;
1195 for (var = tab->n_param; var < tab->n_var - tab->n_div; ++var) {
1196 if (!tab->var[var].is_row) {
1197 if (tab->var[var].index == col)
1198 break;
1199 else
1200 continue;
1202 row = tab->var[var].index;
1203 if (isl_int_is_zero(tab->mat->row[row][off + col]))
1204 continue;
1205 if (isl_int_is_pos(tab->mat->row[row][off + col]))
1206 break;
1207 fprintf(stderr, "lexneg column %d (row %d)\n",
1208 col, row);
1210 if (var >= tab->n_var - tab->n_div)
1211 fprintf(stderr, "zero column %d\n", col);
1215 /* Report to the caller that the given constraint is part of an encountered
1216 * conflict.
1218 static int report_conflicting_constraint(struct isl_tab *tab, int con)
1220 return tab->conflict(con, tab->conflict_user);
1223 /* Given a conflicting row in the tableau, report all constraints
1224 * involved in the row to the caller. That is, the row itself
1225 * (if it represents a constraint) and all constraint columns with
1226 * non-zero (and therefore negative) coefficients.
1228 static int report_conflict(struct isl_tab *tab, int row)
1230 int j;
1231 isl_int *tr;
1233 if (!tab->conflict)
1234 return 0;
1236 if (tab->row_var[row] < 0 &&
1237 report_conflicting_constraint(tab, ~tab->row_var[row]) < 0)
1238 return -1;
1240 tr = tab->mat->row[row] + 2 + tab->M;
1242 for (j = tab->n_dead; j < tab->n_col; ++j) {
1243 if (tab->col_var[j] >= 0 &&
1244 (tab->col_var[j] < tab->n_param ||
1245 tab->col_var[j] >= tab->n_var - tab->n_div))
1246 continue;
1248 if (!isl_int_is_neg(tr[j]))
1249 continue;
1251 if (tab->col_var[j] < 0 &&
1252 report_conflicting_constraint(tab, ~tab->col_var[j]) < 0)
1253 return -1;
1256 return 0;
1259 /* Resolve all known or obviously violated constraints through pivoting.
1260 * In particular, as long as we can find any violated constraint, we
1261 * look for a pivoting column that would result in the lexicographically
1262 * smallest increment in the sample point. If there is no such column
1263 * then the tableau is infeasible.
1265 static int restore_lexmin(struct isl_tab *tab) WARN_UNUSED;
1266 static int restore_lexmin(struct isl_tab *tab)
1268 int row, col;
1270 if (!tab)
1271 return -1;
1272 if (tab->empty)
1273 return 0;
1274 while ((row = first_neg(tab)) != -1) {
1275 col = lexmin_pivot_col(tab, row);
1276 if (col >= tab->n_col) {
1277 if (report_conflict(tab, row) < 0)
1278 return -1;
1279 if (isl_tab_mark_empty(tab) < 0)
1280 return -1;
1281 return 0;
1283 if (col < 0)
1284 return -1;
1285 if (isl_tab_pivot(tab, row, col) < 0)
1286 return -1;
1288 return 0;
1291 /* Given a row that represents an equality, look for an appropriate
1292 * pivoting column.
1293 * In particular, if there are any non-zero coefficients among
1294 * the non-parameter variables, then we take the last of these
1295 * variables. Eliminating this variable in terms of the other
1296 * variables and/or parameters does not influence the property
1297 * that all column in the initial tableau are lexicographically
1298 * positive. The row corresponding to the eliminated variable
1299 * will only have non-zero entries below the diagonal of the
1300 * initial tableau. That is, we transform
1302 * I I
1303 * 1 into a
1304 * I I
1306 * If there is no such non-parameter variable, then we are dealing with
1307 * pure parameter equality and we pick any parameter with coefficient 1 or -1
1308 * for elimination. This will ensure that the eliminated parameter
1309 * always has an integer value whenever all the other parameters are integral.
1310 * If there is no such parameter then we return -1.
1312 static int last_var_col_or_int_par_col(struct isl_tab *tab, int row)
1314 unsigned off = 2 + tab->M;
1315 int i;
1317 for (i = tab->n_var - tab->n_div - 1; i >= 0 && i >= tab->n_param; --i) {
1318 int col;
1319 if (tab->var[i].is_row)
1320 continue;
1321 col = tab->var[i].index;
1322 if (col <= tab->n_dead)
1323 continue;
1324 if (!isl_int_is_zero(tab->mat->row[row][off + col]))
1325 return col;
1327 for (i = tab->n_dead; i < tab->n_col; ++i) {
1328 if (isl_int_is_one(tab->mat->row[row][off + i]))
1329 return i;
1330 if (isl_int_is_negone(tab->mat->row[row][off + i]))
1331 return i;
1333 return -1;
1336 /* Add an equality that is known to be valid to the tableau.
1337 * We first check if we can eliminate a variable or a parameter.
1338 * If not, we add the equality as two inequalities.
1339 * In this case, the equality was a pure parameter equality and there
1340 * is no need to resolve any constraint violations.
1342 * This function assumes that at least two more rows and at least
1343 * two more elements in the constraint array are available in the tableau.
1345 static struct isl_tab *add_lexmin_valid_eq(struct isl_tab *tab, isl_int *eq)
1347 int i;
1348 int r;
1350 if (!tab)
1351 return NULL;
1352 r = isl_tab_add_row(tab, eq);
1353 if (r < 0)
1354 goto error;
1356 r = tab->con[r].index;
1357 i = last_var_col_or_int_par_col(tab, r);
1358 if (i < 0) {
1359 tab->con[r].is_nonneg = 1;
1360 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1361 goto error;
1362 isl_seq_neg(eq, eq, 1 + tab->n_var);
1363 r = isl_tab_add_row(tab, eq);
1364 if (r < 0)
1365 goto error;
1366 tab->con[r].is_nonneg = 1;
1367 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1368 goto error;
1369 } else {
1370 if (isl_tab_pivot(tab, r, i) < 0)
1371 goto error;
1372 if (isl_tab_kill_col(tab, i) < 0)
1373 goto error;
1374 tab->n_eq++;
1377 return tab;
1378 error:
1379 isl_tab_free(tab);
1380 return NULL;
1383 /* Check if the given row is a pure constant.
1385 static int is_constant(struct isl_tab *tab, int row)
1387 unsigned off = 2 + tab->M;
1389 return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1390 tab->n_col - tab->n_dead) == -1;
1393 /* Add an equality that may or may not be valid to the tableau.
1394 * If the resulting row is a pure constant, then it must be zero.
1395 * Otherwise, the resulting tableau is empty.
1397 * If the row is not a pure constant, then we add two inequalities,
1398 * each time checking that they can be satisfied.
1399 * In the end we try to use one of the two constraints to eliminate
1400 * a column.
1402 * This function assumes that at least two more rows and at least
1403 * two more elements in the constraint array are available in the tableau.
1405 static int add_lexmin_eq(struct isl_tab *tab, isl_int *eq) WARN_UNUSED;
1406 static int add_lexmin_eq(struct isl_tab *tab, isl_int *eq)
1408 int r1, r2;
1409 int row;
1410 struct isl_tab_undo *snap;
1412 if (!tab)
1413 return -1;
1414 snap = isl_tab_snap(tab);
1415 r1 = isl_tab_add_row(tab, eq);
1416 if (r1 < 0)
1417 return -1;
1418 tab->con[r1].is_nonneg = 1;
1419 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r1]) < 0)
1420 return -1;
1422 row = tab->con[r1].index;
1423 if (is_constant(tab, row)) {
1424 if (!isl_int_is_zero(tab->mat->row[row][1]) ||
1425 (tab->M && !isl_int_is_zero(tab->mat->row[row][2]))) {
1426 if (isl_tab_mark_empty(tab) < 0)
1427 return -1;
1428 return 0;
1430 if (isl_tab_rollback(tab, snap) < 0)
1431 return -1;
1432 return 0;
1435 if (restore_lexmin(tab) < 0)
1436 return -1;
1437 if (tab->empty)
1438 return 0;
1440 isl_seq_neg(eq, eq, 1 + tab->n_var);
1442 r2 = isl_tab_add_row(tab, eq);
1443 if (r2 < 0)
1444 return -1;
1445 tab->con[r2].is_nonneg = 1;
1446 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r2]) < 0)
1447 return -1;
1449 if (restore_lexmin(tab) < 0)
1450 return -1;
1451 if (tab->empty)
1452 return 0;
1454 if (!tab->con[r1].is_row) {
1455 if (isl_tab_kill_col(tab, tab->con[r1].index) < 0)
1456 return -1;
1457 } else if (!tab->con[r2].is_row) {
1458 if (isl_tab_kill_col(tab, tab->con[r2].index) < 0)
1459 return -1;
1462 if (tab->bmap) {
1463 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1464 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1465 return -1;
1466 isl_seq_neg(eq, eq, 1 + tab->n_var);
1467 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1468 isl_seq_neg(eq, eq, 1 + tab->n_var);
1469 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1470 return -1;
1471 if (!tab->bmap)
1472 return -1;
1475 return 0;
1478 /* Add an inequality to the tableau, resolving violations using
1479 * restore_lexmin.
1481 * This function assumes that at least one more row and at least
1482 * one more element in the constraint array are available in the tableau.
1484 static struct isl_tab *add_lexmin_ineq(struct isl_tab *tab, isl_int *ineq)
1486 int r;
1488 if (!tab)
1489 return NULL;
1490 if (tab->bmap) {
1491 tab->bmap = isl_basic_map_add_ineq(tab->bmap, ineq);
1492 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1493 goto error;
1494 if (!tab->bmap)
1495 goto error;
1497 r = isl_tab_add_row(tab, ineq);
1498 if (r < 0)
1499 goto error;
1500 tab->con[r].is_nonneg = 1;
1501 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1502 goto error;
1503 if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1504 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1505 goto error;
1506 return tab;
1509 if (restore_lexmin(tab) < 0)
1510 goto error;
1511 if (!tab->empty && tab->con[r].is_row &&
1512 isl_tab_row_is_redundant(tab, tab->con[r].index))
1513 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1514 goto error;
1515 return tab;
1516 error:
1517 isl_tab_free(tab);
1518 return NULL;
1521 /* Check if the coefficients of the parameters are all integral.
1523 static int integer_parameter(struct isl_tab *tab, int row)
1525 int i;
1526 int col;
1527 unsigned off = 2 + tab->M;
1529 for (i = 0; i < tab->n_param; ++i) {
1530 /* Eliminated parameter */
1531 if (tab->var[i].is_row)
1532 continue;
1533 col = tab->var[i].index;
1534 if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
1535 tab->mat->row[row][0]))
1536 return 0;
1538 for (i = 0; i < tab->n_div; ++i) {
1539 if (tab->var[tab->n_var - tab->n_div + i].is_row)
1540 continue;
1541 col = tab->var[tab->n_var - tab->n_div + i].index;
1542 if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
1543 tab->mat->row[row][0]))
1544 return 0;
1546 return 1;
1549 /* Check if the coefficients of the non-parameter variables are all integral.
1551 static int integer_variable(struct isl_tab *tab, int row)
1553 int i;
1554 unsigned off = 2 + tab->M;
1556 for (i = tab->n_dead; i < tab->n_col; ++i) {
1557 if (tab->col_var[i] >= 0 &&
1558 (tab->col_var[i] < tab->n_param ||
1559 tab->col_var[i] >= tab->n_var - tab->n_div))
1560 continue;
1561 if (!isl_int_is_divisible_by(tab->mat->row[row][off + i],
1562 tab->mat->row[row][0]))
1563 return 0;
1565 return 1;
1568 /* Check if the constant term is integral.
1570 static int integer_constant(struct isl_tab *tab, int row)
1572 return isl_int_is_divisible_by(tab->mat->row[row][1],
1573 tab->mat->row[row][0]);
1576 #define I_CST 1 << 0
1577 #define I_PAR 1 << 1
1578 #define I_VAR 1 << 2
1580 /* Check for next (non-parameter) variable after "var" (first if var == -1)
1581 * that is non-integer and therefore requires a cut and return
1582 * the index of the variable.
1583 * For parametric tableaus, there are three parts in a row,
1584 * the constant, the coefficients of the parameters and the rest.
1585 * For each part, we check whether the coefficients in that part
1586 * are all integral and if so, set the corresponding flag in *f.
1587 * If the constant and the parameter part are integral, then the
1588 * current sample value is integral and no cut is required
1589 * (irrespective of whether the variable part is integral).
1591 static int next_non_integer_var(struct isl_tab *tab, int var, int *f)
1593 var = var < 0 ? tab->n_param : var + 1;
1595 for (; var < tab->n_var - tab->n_div; ++var) {
1596 int flags = 0;
1597 int row;
1598 if (!tab->var[var].is_row)
1599 continue;
1600 row = tab->var[var].index;
1601 if (integer_constant(tab, row))
1602 ISL_FL_SET(flags, I_CST);
1603 if (integer_parameter(tab, row))
1604 ISL_FL_SET(flags, I_PAR);
1605 if (ISL_FL_ISSET(flags, I_CST) && ISL_FL_ISSET(flags, I_PAR))
1606 continue;
1607 if (integer_variable(tab, row))
1608 ISL_FL_SET(flags, I_VAR);
1609 *f = flags;
1610 return var;
1612 return -1;
1615 /* Check for first (non-parameter) variable that is non-integer and
1616 * therefore requires a cut and return the corresponding row.
1617 * For parametric tableaus, there are three parts in a row,
1618 * the constant, the coefficients of the parameters and the rest.
1619 * For each part, we check whether the coefficients in that part
1620 * are all integral and if so, set the corresponding flag in *f.
1621 * If the constant and the parameter part are integral, then the
1622 * current sample value is integral and no cut is required
1623 * (irrespective of whether the variable part is integral).
1625 static int first_non_integer_row(struct isl_tab *tab, int *f)
1627 int var = next_non_integer_var(tab, -1, f);
1629 return var < 0 ? -1 : tab->var[var].index;
1632 /* Add a (non-parametric) cut to cut away the non-integral sample
1633 * value of the given row.
1635 * If the row is given by
1637 * m r = f + \sum_i a_i y_i
1639 * then the cut is
1641 * c = - {-f/m} + \sum_i {a_i/m} y_i >= 0
1643 * The big parameter, if any, is ignored, since it is assumed to be big
1644 * enough to be divisible by any integer.
1645 * If the tableau is actually a parametric tableau, then this function
1646 * is only called when all coefficients of the parameters are integral.
1647 * The cut therefore has zero coefficients for the parameters.
1649 * The current value is known to be negative, so row_sign, if it
1650 * exists, is set accordingly.
1652 * Return the row of the cut or -1.
1654 static int add_cut(struct isl_tab *tab, int row)
1656 int i;
1657 int r;
1658 isl_int *r_row;
1659 unsigned off = 2 + tab->M;
1661 if (isl_tab_extend_cons(tab, 1) < 0)
1662 return -1;
1663 r = isl_tab_allocate_con(tab);
1664 if (r < 0)
1665 return -1;
1667 r_row = tab->mat->row[tab->con[r].index];
1668 isl_int_set(r_row[0], tab->mat->row[row][0]);
1669 isl_int_neg(r_row[1], tab->mat->row[row][1]);
1670 isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
1671 isl_int_neg(r_row[1], r_row[1]);
1672 if (tab->M)
1673 isl_int_set_si(r_row[2], 0);
1674 for (i = 0; i < tab->n_col; ++i)
1675 isl_int_fdiv_r(r_row[off + i],
1676 tab->mat->row[row][off + i], tab->mat->row[row][0]);
1678 tab->con[r].is_nonneg = 1;
1679 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1680 return -1;
1681 if (tab->row_sign)
1682 tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
1684 return tab->con[r].index;
1687 #define CUT_ALL 1
1688 #define CUT_ONE 0
1690 /* Given a non-parametric tableau, add cuts until an integer
1691 * sample point is obtained or until the tableau is determined
1692 * to be integer infeasible.
1693 * As long as there is any non-integer value in the sample point,
1694 * we add appropriate cuts, if possible, for each of these
1695 * non-integer values and then resolve the violated
1696 * cut constraints using restore_lexmin.
1697 * If one of the corresponding rows is equal to an integral
1698 * combination of variables/constraints plus a non-integral constant,
1699 * then there is no way to obtain an integer point and we return
1700 * a tableau that is marked empty.
1701 * The parameter cutting_strategy controls the strategy used when adding cuts
1702 * to remove non-integer points. CUT_ALL adds all possible cuts
1703 * before continuing the search. CUT_ONE adds only one cut at a time.
1705 static struct isl_tab *cut_to_integer_lexmin(struct isl_tab *tab,
1706 int cutting_strategy)
1708 int var;
1709 int row;
1710 int flags;
1712 if (!tab)
1713 return NULL;
1714 if (tab->empty)
1715 return tab;
1717 while ((var = next_non_integer_var(tab, -1, &flags)) != -1) {
1718 do {
1719 if (ISL_FL_ISSET(flags, I_VAR)) {
1720 if (isl_tab_mark_empty(tab) < 0)
1721 goto error;
1722 return tab;
1724 row = tab->var[var].index;
1725 row = add_cut(tab, row);
1726 if (row < 0)
1727 goto error;
1728 if (cutting_strategy == CUT_ONE)
1729 break;
1730 } while ((var = next_non_integer_var(tab, var, &flags)) != -1);
1731 if (restore_lexmin(tab) < 0)
1732 goto error;
1733 if (tab->empty)
1734 break;
1736 return tab;
1737 error:
1738 isl_tab_free(tab);
1739 return NULL;
1742 /* Check whether all the currently active samples also satisfy the inequality
1743 * "ineq" (treated as an equality if eq is set).
1744 * Remove those samples that do not.
1746 static struct isl_tab *check_samples(struct isl_tab *tab, isl_int *ineq, int eq)
1748 int i;
1749 isl_int v;
1751 if (!tab)
1752 return NULL;
1754 isl_assert(tab->mat->ctx, tab->bmap, goto error);
1755 isl_assert(tab->mat->ctx, tab->samples, goto error);
1756 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, goto error);
1758 isl_int_init(v);
1759 for (i = tab->n_outside; i < tab->n_sample; ++i) {
1760 int sgn;
1761 isl_seq_inner_product(ineq, tab->samples->row[i],
1762 1 + tab->n_var, &v);
1763 sgn = isl_int_sgn(v);
1764 if (eq ? (sgn == 0) : (sgn >= 0))
1765 continue;
1766 tab = isl_tab_drop_sample(tab, i);
1767 if (!tab)
1768 break;
1770 isl_int_clear(v);
1772 return tab;
1773 error:
1774 isl_tab_free(tab);
1775 return NULL;
1778 /* Check whether the sample value of the tableau is finite,
1779 * i.e., either the tableau does not use a big parameter, or
1780 * all values of the variables are equal to the big parameter plus
1781 * some constant. This constant is the actual sample value.
1783 static int sample_is_finite(struct isl_tab *tab)
1785 int i;
1787 if (!tab->M)
1788 return 1;
1790 for (i = 0; i < tab->n_var; ++i) {
1791 int row;
1792 if (!tab->var[i].is_row)
1793 return 0;
1794 row = tab->var[i].index;
1795 if (isl_int_ne(tab->mat->row[row][0], tab->mat->row[row][2]))
1796 return 0;
1798 return 1;
1801 /* Check if the context tableau of sol has any integer points.
1802 * Leave tab in empty state if no integer point can be found.
1803 * If an integer point can be found and if moreover it is finite,
1804 * then it is added to the list of sample values.
1806 * This function is only called when none of the currently active sample
1807 * values satisfies the most recently added constraint.
1809 static struct isl_tab *check_integer_feasible(struct isl_tab *tab)
1811 struct isl_tab_undo *snap;
1813 if (!tab)
1814 return NULL;
1816 snap = isl_tab_snap(tab);
1817 if (isl_tab_push_basis(tab) < 0)
1818 goto error;
1820 tab = cut_to_integer_lexmin(tab, CUT_ALL);
1821 if (!tab)
1822 goto error;
1824 if (!tab->empty && sample_is_finite(tab)) {
1825 struct isl_vec *sample;
1827 sample = isl_tab_get_sample_value(tab);
1829 if (isl_tab_add_sample(tab, sample) < 0)
1830 goto error;
1833 if (!tab->empty && isl_tab_rollback(tab, snap) < 0)
1834 goto error;
1836 return tab;
1837 error:
1838 isl_tab_free(tab);
1839 return NULL;
1842 /* Check if any of the currently active sample values satisfies
1843 * the inequality "ineq" (an equality if eq is set).
1845 static int tab_has_valid_sample(struct isl_tab *tab, isl_int *ineq, int eq)
1847 int i;
1848 isl_int v;
1850 if (!tab)
1851 return -1;
1853 isl_assert(tab->mat->ctx, tab->bmap, return -1);
1854 isl_assert(tab->mat->ctx, tab->samples, return -1);
1855 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, return -1);
1857 isl_int_init(v);
1858 for (i = tab->n_outside; i < tab->n_sample; ++i) {
1859 int sgn;
1860 isl_seq_inner_product(ineq, tab->samples->row[i],
1861 1 + tab->n_var, &v);
1862 sgn = isl_int_sgn(v);
1863 if (eq ? (sgn == 0) : (sgn >= 0))
1864 break;
1866 isl_int_clear(v);
1868 return i < tab->n_sample;
1871 /* Insert a div specified by "div" to the tableau "tab" at position "pos" and
1872 * return isl_bool_true if the div is obviously non-negative.
1874 static isl_bool context_tab_insert_div(struct isl_tab *tab, int pos,
1875 __isl_keep isl_vec *div,
1876 int (*add_ineq)(void *user, isl_int *), void *user)
1878 int i;
1879 int r;
1880 struct isl_mat *samples;
1881 int nonneg;
1883 r = isl_tab_insert_div(tab, pos, div, add_ineq, user);
1884 if (r < 0)
1885 return isl_bool_error;
1886 nonneg = tab->var[r].is_nonneg;
1887 tab->var[r].frozen = 1;
1889 samples = isl_mat_extend(tab->samples,
1890 tab->n_sample, 1 + tab->n_var);
1891 tab->samples = samples;
1892 if (!samples)
1893 return isl_bool_error;
1894 for (i = tab->n_outside; i < samples->n_row; ++i) {
1895 isl_seq_inner_product(div->el + 1, samples->row[i],
1896 div->size - 1, &samples->row[i][samples->n_col - 1]);
1897 isl_int_fdiv_q(samples->row[i][samples->n_col - 1],
1898 samples->row[i][samples->n_col - 1], div->el[0]);
1900 tab->samples = isl_mat_move_cols(tab->samples, 1 + pos,
1901 1 + tab->n_var - 1, 1);
1902 if (!tab->samples)
1903 return isl_bool_error;
1905 return nonneg;
1908 /* Add a div specified by "div" to both the main tableau and
1909 * the context tableau. In case of the main tableau, we only
1910 * need to add an extra div. In the context tableau, we also
1911 * need to express the meaning of the div.
1912 * Return the index of the div or -1 if anything went wrong.
1914 * The new integer division is added before any unknown integer
1915 * divisions in the context to ensure that it does not get
1916 * equated to some linear combination involving unknown integer
1917 * divisions.
1919 static int add_div(struct isl_tab *tab, struct isl_context *context,
1920 __isl_keep isl_vec *div)
1922 int r;
1923 int pos;
1924 isl_bool nonneg;
1925 struct isl_tab *context_tab = context->op->peek_tab(context);
1927 if (!tab || !context_tab)
1928 goto error;
1930 pos = context_tab->n_var - context->n_unknown;
1931 if ((nonneg = context->op->insert_div(context, pos, div)) < 0)
1932 goto error;
1934 if (!context->op->is_ok(context))
1935 goto error;
1937 pos = tab->n_var - context->n_unknown;
1938 if (isl_tab_extend_vars(tab, 1) < 0)
1939 goto error;
1940 r = isl_tab_insert_var(tab, pos);
1941 if (r < 0)
1942 goto error;
1943 if (nonneg)
1944 tab->var[r].is_nonneg = 1;
1945 tab->var[r].frozen = 1;
1946 tab->n_div++;
1948 return tab->n_div - 1 - context->n_unknown;
1949 error:
1950 context->op->invalidate(context);
1951 return -1;
1954 static int find_div(struct isl_tab *tab, isl_int *div, isl_int denom)
1956 int i;
1957 unsigned total = isl_basic_map_total_dim(tab->bmap);
1959 for (i = 0; i < tab->bmap->n_div; ++i) {
1960 if (isl_int_ne(tab->bmap->div[i][0], denom))
1961 continue;
1962 if (!isl_seq_eq(tab->bmap->div[i] + 1, div, 1 + total))
1963 continue;
1964 return i;
1966 return -1;
1969 /* Return the index of a div that corresponds to "div".
1970 * We first check if we already have such a div and if not, we create one.
1972 static int get_div(struct isl_tab *tab, struct isl_context *context,
1973 struct isl_vec *div)
1975 int d;
1976 struct isl_tab *context_tab = context->op->peek_tab(context);
1978 if (!context_tab)
1979 return -1;
1981 d = find_div(context_tab, div->el + 1, div->el[0]);
1982 if (d != -1)
1983 return d;
1985 return add_div(tab, context, div);
1988 /* Add a parametric cut to cut away the non-integral sample value
1989 * of the give row.
1990 * Let a_i be the coefficients of the constant term and the parameters
1991 * and let b_i be the coefficients of the variables or constraints
1992 * in basis of the tableau.
1993 * Let q be the div q = floor(\sum_i {-a_i} y_i).
1995 * The cut is expressed as
1997 * c = \sum_i -{-a_i} y_i + \sum_i {b_i} x_i + q >= 0
1999 * If q did not already exist in the context tableau, then it is added first.
2000 * If q is in a column of the main tableau then the "+ q" can be accomplished
2001 * by setting the corresponding entry to the denominator of the constraint.
2002 * If q happens to be in a row of the main tableau, then the corresponding
2003 * row needs to be added instead (taking care of the denominators).
2004 * Note that this is very unlikely, but perhaps not entirely impossible.
2006 * The current value of the cut is known to be negative (or at least
2007 * non-positive), so row_sign is set accordingly.
2009 * Return the row of the cut or -1.
2011 static int add_parametric_cut(struct isl_tab *tab, int row,
2012 struct isl_context *context)
2014 struct isl_vec *div;
2015 int d;
2016 int i;
2017 int r;
2018 isl_int *r_row;
2019 int col;
2020 int n;
2021 unsigned off = 2 + tab->M;
2023 if (!context)
2024 return -1;
2026 div = get_row_parameter_div(tab, row);
2027 if (!div)
2028 return -1;
2030 n = tab->n_div - context->n_unknown;
2031 d = context->op->get_div(context, tab, div);
2032 isl_vec_free(div);
2033 if (d < 0)
2034 return -1;
2036 if (isl_tab_extend_cons(tab, 1) < 0)
2037 return -1;
2038 r = isl_tab_allocate_con(tab);
2039 if (r < 0)
2040 return -1;
2042 r_row = tab->mat->row[tab->con[r].index];
2043 isl_int_set(r_row[0], tab->mat->row[row][0]);
2044 isl_int_neg(r_row[1], tab->mat->row[row][1]);
2045 isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
2046 isl_int_neg(r_row[1], r_row[1]);
2047 if (tab->M)
2048 isl_int_set_si(r_row[2], 0);
2049 for (i = 0; i < tab->n_param; ++i) {
2050 if (tab->var[i].is_row)
2051 continue;
2052 col = tab->var[i].index;
2053 isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
2054 isl_int_fdiv_r(r_row[off + col], r_row[off + col],
2055 tab->mat->row[row][0]);
2056 isl_int_neg(r_row[off + col], r_row[off + col]);
2058 for (i = 0; i < tab->n_div; ++i) {
2059 if (tab->var[tab->n_var - tab->n_div + i].is_row)
2060 continue;
2061 col = tab->var[tab->n_var - tab->n_div + i].index;
2062 isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
2063 isl_int_fdiv_r(r_row[off + col], r_row[off + col],
2064 tab->mat->row[row][0]);
2065 isl_int_neg(r_row[off + col], r_row[off + col]);
2067 for (i = 0; i < tab->n_col; ++i) {
2068 if (tab->col_var[i] >= 0 &&
2069 (tab->col_var[i] < tab->n_param ||
2070 tab->col_var[i] >= tab->n_var - tab->n_div))
2071 continue;
2072 isl_int_fdiv_r(r_row[off + i],
2073 tab->mat->row[row][off + i], tab->mat->row[row][0]);
2075 if (tab->var[tab->n_var - tab->n_div + d].is_row) {
2076 isl_int gcd;
2077 int d_row = tab->var[tab->n_var - tab->n_div + d].index;
2078 isl_int_init(gcd);
2079 isl_int_gcd(gcd, tab->mat->row[d_row][0], r_row[0]);
2080 isl_int_divexact(r_row[0], r_row[0], gcd);
2081 isl_int_divexact(gcd, tab->mat->row[d_row][0], gcd);
2082 isl_seq_combine(r_row + 1, gcd, r_row + 1,
2083 r_row[0], tab->mat->row[d_row] + 1,
2084 off - 1 + tab->n_col);
2085 isl_int_mul(r_row[0], r_row[0], tab->mat->row[d_row][0]);
2086 isl_int_clear(gcd);
2087 } else {
2088 col = tab->var[tab->n_var - tab->n_div + d].index;
2089 isl_int_set(r_row[off + col], tab->mat->row[row][0]);
2092 tab->con[r].is_nonneg = 1;
2093 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
2094 return -1;
2095 if (tab->row_sign)
2096 tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
2098 row = tab->con[r].index;
2100 if (d >= n && context->op->detect_equalities(context, tab) < 0)
2101 return -1;
2103 return row;
2106 /* Construct a tableau for bmap that can be used for computing
2107 * the lexicographic minimum (or maximum) of bmap.
2108 * If not NULL, then dom is the domain where the minimum
2109 * should be computed. In this case, we set up a parametric
2110 * tableau with row signs (initialized to "unknown").
2111 * If M is set, then the tableau will use a big parameter.
2112 * If max is set, then a maximum should be computed instead of a minimum.
2113 * This means that for each variable x, the tableau will contain the variable
2114 * x' = M - x, rather than x' = M + x. This in turn means that the coefficient
2115 * of the variables in all constraints are negated prior to adding them
2116 * to the tableau.
2118 static struct isl_tab *tab_for_lexmin(struct isl_basic_map *bmap,
2119 struct isl_basic_set *dom, unsigned M, int max)
2121 int i;
2122 struct isl_tab *tab;
2123 unsigned n_var;
2124 unsigned o_var;
2126 tab = isl_tab_alloc(bmap->ctx, 2 * bmap->n_eq + bmap->n_ineq + 1,
2127 isl_basic_map_total_dim(bmap), M);
2128 if (!tab)
2129 return NULL;
2131 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
2132 if (dom) {
2133 tab->n_param = isl_basic_set_total_dim(dom) - dom->n_div;
2134 tab->n_div = dom->n_div;
2135 tab->row_sign = isl_calloc_array(bmap->ctx,
2136 enum isl_tab_row_sign, tab->mat->n_row);
2137 if (tab->mat->n_row && !tab->row_sign)
2138 goto error;
2140 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
2141 if (isl_tab_mark_empty(tab) < 0)
2142 goto error;
2143 return tab;
2146 for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
2147 tab->var[i].is_nonneg = 1;
2148 tab->var[i].frozen = 1;
2150 o_var = 1 + tab->n_param;
2151 n_var = tab->n_var - tab->n_param - tab->n_div;
2152 for (i = 0; i < bmap->n_eq; ++i) {
2153 if (max)
2154 isl_seq_neg(bmap->eq[i] + o_var,
2155 bmap->eq[i] + o_var, n_var);
2156 tab = add_lexmin_valid_eq(tab, bmap->eq[i]);
2157 if (max)
2158 isl_seq_neg(bmap->eq[i] + o_var,
2159 bmap->eq[i] + o_var, n_var);
2160 if (!tab || tab->empty)
2161 return tab;
2163 if (bmap->n_eq && restore_lexmin(tab) < 0)
2164 goto error;
2165 for (i = 0; i < bmap->n_ineq; ++i) {
2166 if (max)
2167 isl_seq_neg(bmap->ineq[i] + o_var,
2168 bmap->ineq[i] + o_var, n_var);
2169 tab = add_lexmin_ineq(tab, bmap->ineq[i]);
2170 if (max)
2171 isl_seq_neg(bmap->ineq[i] + o_var,
2172 bmap->ineq[i] + o_var, n_var);
2173 if (!tab || tab->empty)
2174 return tab;
2176 return tab;
2177 error:
2178 isl_tab_free(tab);
2179 return NULL;
2182 /* Given a main tableau where more than one row requires a split,
2183 * determine and return the "best" row to split on.
2185 * Given two rows in the main tableau, if the inequality corresponding
2186 * to the first row is redundant with respect to that of the second row
2187 * in the current tableau, then it is better to split on the second row,
2188 * since in the positive part, both rows will be positive.
2189 * (In the negative part a pivot will have to be performed and just about
2190 * anything can happen to the sign of the other row.)
2192 * As a simple heuristic, we therefore select the row that makes the most
2193 * of the other rows redundant.
2195 * Perhaps it would also be useful to look at the number of constraints
2196 * that conflict with any given constraint.
2198 * best is the best row so far (-1 when we have not found any row yet).
2199 * best_r is the number of other rows made redundant by row best.
2200 * When best is still -1, bset_r is meaningless, but it is initialized
2201 * to some arbitrary value (0) anyway. Without this redundant initialization
2202 * valgrind may warn about uninitialized memory accesses when isl
2203 * is compiled with some versions of gcc.
2205 static int best_split(struct isl_tab *tab, struct isl_tab *context_tab)
2207 struct isl_tab_undo *snap;
2208 int split;
2209 int row;
2210 int best = -1;
2211 int best_r = 0;
2213 if (isl_tab_extend_cons(context_tab, 2) < 0)
2214 return -1;
2216 snap = isl_tab_snap(context_tab);
2218 for (split = tab->n_redundant; split < tab->n_row; ++split) {
2219 struct isl_tab_undo *snap2;
2220 struct isl_vec *ineq = NULL;
2221 int r = 0;
2222 int ok;
2224 if (!isl_tab_var_from_row(tab, split)->is_nonneg)
2225 continue;
2226 if (tab->row_sign[split] != isl_tab_row_any)
2227 continue;
2229 ineq = get_row_parameter_ineq(tab, split);
2230 if (!ineq)
2231 return -1;
2232 ok = isl_tab_add_ineq(context_tab, ineq->el) >= 0;
2233 isl_vec_free(ineq);
2234 if (!ok)
2235 return -1;
2237 snap2 = isl_tab_snap(context_tab);
2239 for (row = tab->n_redundant; row < tab->n_row; ++row) {
2240 struct isl_tab_var *var;
2242 if (row == split)
2243 continue;
2244 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
2245 continue;
2246 if (tab->row_sign[row] != isl_tab_row_any)
2247 continue;
2249 ineq = get_row_parameter_ineq(tab, row);
2250 if (!ineq)
2251 return -1;
2252 ok = isl_tab_add_ineq(context_tab, ineq->el) >= 0;
2253 isl_vec_free(ineq);
2254 if (!ok)
2255 return -1;
2256 var = &context_tab->con[context_tab->n_con - 1];
2257 if (!context_tab->empty &&
2258 !isl_tab_min_at_most_neg_one(context_tab, var))
2259 r++;
2260 if (isl_tab_rollback(context_tab, snap2) < 0)
2261 return -1;
2263 if (best == -1 || r > best_r) {
2264 best = split;
2265 best_r = r;
2267 if (isl_tab_rollback(context_tab, snap) < 0)
2268 return -1;
2271 return best;
2274 static struct isl_basic_set *context_lex_peek_basic_set(
2275 struct isl_context *context)
2277 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2278 if (!clex->tab)
2279 return NULL;
2280 return isl_tab_peek_bset(clex->tab);
2283 static struct isl_tab *context_lex_peek_tab(struct isl_context *context)
2285 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2286 return clex->tab;
2289 static void context_lex_add_eq(struct isl_context *context, isl_int *eq,
2290 int check, int update)
2292 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2293 if (isl_tab_extend_cons(clex->tab, 2) < 0)
2294 goto error;
2295 if (add_lexmin_eq(clex->tab, eq) < 0)
2296 goto error;
2297 if (check) {
2298 int v = tab_has_valid_sample(clex->tab, eq, 1);
2299 if (v < 0)
2300 goto error;
2301 if (!v)
2302 clex->tab = check_integer_feasible(clex->tab);
2304 if (update)
2305 clex->tab = check_samples(clex->tab, eq, 1);
2306 return;
2307 error:
2308 isl_tab_free(clex->tab);
2309 clex->tab = NULL;
2312 static void context_lex_add_ineq(struct isl_context *context, isl_int *ineq,
2313 int check, int update)
2315 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2316 if (isl_tab_extend_cons(clex->tab, 1) < 0)
2317 goto error;
2318 clex->tab = add_lexmin_ineq(clex->tab, ineq);
2319 if (check) {
2320 int v = tab_has_valid_sample(clex->tab, ineq, 0);
2321 if (v < 0)
2322 goto error;
2323 if (!v)
2324 clex->tab = check_integer_feasible(clex->tab);
2326 if (update)
2327 clex->tab = check_samples(clex->tab, ineq, 0);
2328 return;
2329 error:
2330 isl_tab_free(clex->tab);
2331 clex->tab = NULL;
2334 static int context_lex_add_ineq_wrap(void *user, isl_int *ineq)
2336 struct isl_context *context = (struct isl_context *)user;
2337 context_lex_add_ineq(context, ineq, 0, 0);
2338 return context->op->is_ok(context) ? 0 : -1;
2341 /* Check which signs can be obtained by "ineq" on all the currently
2342 * active sample values. See row_sign for more information.
2344 static enum isl_tab_row_sign tab_ineq_sign(struct isl_tab *tab, isl_int *ineq,
2345 int strict)
2347 int i;
2348 int sgn;
2349 isl_int tmp;
2350 enum isl_tab_row_sign res = isl_tab_row_unknown;
2352 isl_assert(tab->mat->ctx, tab->samples, return isl_tab_row_unknown);
2353 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var,
2354 return isl_tab_row_unknown);
2356 isl_int_init(tmp);
2357 for (i = tab->n_outside; i < tab->n_sample; ++i) {
2358 isl_seq_inner_product(tab->samples->row[i], ineq,
2359 1 + tab->n_var, &tmp);
2360 sgn = isl_int_sgn(tmp);
2361 if (sgn > 0 || (sgn == 0 && strict)) {
2362 if (res == isl_tab_row_unknown)
2363 res = isl_tab_row_pos;
2364 if (res == isl_tab_row_neg)
2365 res = isl_tab_row_any;
2367 if (sgn < 0) {
2368 if (res == isl_tab_row_unknown)
2369 res = isl_tab_row_neg;
2370 if (res == isl_tab_row_pos)
2371 res = isl_tab_row_any;
2373 if (res == isl_tab_row_any)
2374 break;
2376 isl_int_clear(tmp);
2378 return res;
2381 static enum isl_tab_row_sign context_lex_ineq_sign(struct isl_context *context,
2382 isl_int *ineq, int strict)
2384 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2385 return tab_ineq_sign(clex->tab, ineq, strict);
2388 /* Check whether "ineq" can be added to the tableau without rendering
2389 * it infeasible.
2391 static int context_lex_test_ineq(struct isl_context *context, isl_int *ineq)
2393 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2394 struct isl_tab_undo *snap;
2395 int feasible;
2397 if (!clex->tab)
2398 return -1;
2400 if (isl_tab_extend_cons(clex->tab, 1) < 0)
2401 return -1;
2403 snap = isl_tab_snap(clex->tab);
2404 if (isl_tab_push_basis(clex->tab) < 0)
2405 return -1;
2406 clex->tab = add_lexmin_ineq(clex->tab, ineq);
2407 clex->tab = check_integer_feasible(clex->tab);
2408 if (!clex->tab)
2409 return -1;
2410 feasible = !clex->tab->empty;
2411 if (isl_tab_rollback(clex->tab, snap) < 0)
2412 return -1;
2414 return feasible;
2417 static int context_lex_get_div(struct isl_context *context, struct isl_tab *tab,
2418 struct isl_vec *div)
2420 return get_div(tab, context, div);
2423 /* Insert a div specified by "div" to the context tableau at position "pos" and
2424 * return isl_bool_true if the div is obviously non-negative.
2425 * context_tab_add_div will always return isl_bool_true, because all variables
2426 * in a isl_context_lex tableau are non-negative.
2427 * However, if we are using a big parameter in the context, then this only
2428 * reflects the non-negativity of the variable used to _encode_ the
2429 * div, i.e., div' = M + div, so we can't draw any conclusions.
2431 static isl_bool context_lex_insert_div(struct isl_context *context, int pos,
2432 __isl_keep isl_vec *div)
2434 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2435 isl_bool nonneg;
2436 nonneg = context_tab_insert_div(clex->tab, pos, div,
2437 context_lex_add_ineq_wrap, context);
2438 if (nonneg < 0)
2439 return isl_bool_error;
2440 if (clex->tab->M)
2441 return isl_bool_false;
2442 return nonneg;
2445 static int context_lex_detect_equalities(struct isl_context *context,
2446 struct isl_tab *tab)
2448 return 0;
2451 static int context_lex_best_split(struct isl_context *context,
2452 struct isl_tab *tab)
2454 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2455 struct isl_tab_undo *snap;
2456 int r;
2458 snap = isl_tab_snap(clex->tab);
2459 if (isl_tab_push_basis(clex->tab) < 0)
2460 return -1;
2461 r = best_split(tab, clex->tab);
2463 if (r >= 0 && isl_tab_rollback(clex->tab, snap) < 0)
2464 return -1;
2466 return r;
2469 static int context_lex_is_empty(struct isl_context *context)
2471 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2472 if (!clex->tab)
2473 return -1;
2474 return clex->tab->empty;
2477 static void *context_lex_save(struct isl_context *context)
2479 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2480 struct isl_tab_undo *snap;
2482 snap = isl_tab_snap(clex->tab);
2483 if (isl_tab_push_basis(clex->tab) < 0)
2484 return NULL;
2485 if (isl_tab_save_samples(clex->tab) < 0)
2486 return NULL;
2488 return snap;
2491 static void context_lex_restore(struct isl_context *context, void *save)
2493 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2494 if (isl_tab_rollback(clex->tab, (struct isl_tab_undo *)save) < 0) {
2495 isl_tab_free(clex->tab);
2496 clex->tab = NULL;
2500 static void context_lex_discard(void *save)
2504 static int context_lex_is_ok(struct isl_context *context)
2506 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2507 return !!clex->tab;
2510 /* For each variable in the context tableau, check if the variable can
2511 * only attain non-negative values. If so, mark the parameter as non-negative
2512 * in the main tableau. This allows for a more direct identification of some
2513 * cases of violated constraints.
2515 static struct isl_tab *tab_detect_nonnegative_parameters(struct isl_tab *tab,
2516 struct isl_tab *context_tab)
2518 int i;
2519 struct isl_tab_undo *snap;
2520 struct isl_vec *ineq = NULL;
2521 struct isl_tab_var *var;
2522 int n;
2524 if (context_tab->n_var == 0)
2525 return tab;
2527 ineq = isl_vec_alloc(tab->mat->ctx, 1 + context_tab->n_var);
2528 if (!ineq)
2529 goto error;
2531 if (isl_tab_extend_cons(context_tab, 1) < 0)
2532 goto error;
2534 snap = isl_tab_snap(context_tab);
2536 n = 0;
2537 isl_seq_clr(ineq->el, ineq->size);
2538 for (i = 0; i < context_tab->n_var; ++i) {
2539 isl_int_set_si(ineq->el[1 + i], 1);
2540 if (isl_tab_add_ineq(context_tab, ineq->el) < 0)
2541 goto error;
2542 var = &context_tab->con[context_tab->n_con - 1];
2543 if (!context_tab->empty &&
2544 !isl_tab_min_at_most_neg_one(context_tab, var)) {
2545 int j = i;
2546 if (i >= tab->n_param)
2547 j = i - tab->n_param + tab->n_var - tab->n_div;
2548 tab->var[j].is_nonneg = 1;
2549 n++;
2551 isl_int_set_si(ineq->el[1 + i], 0);
2552 if (isl_tab_rollback(context_tab, snap) < 0)
2553 goto error;
2556 if (context_tab->M && n == context_tab->n_var) {
2557 context_tab->mat = isl_mat_drop_cols(context_tab->mat, 2, 1);
2558 context_tab->M = 0;
2561 isl_vec_free(ineq);
2562 return tab;
2563 error:
2564 isl_vec_free(ineq);
2565 isl_tab_free(tab);
2566 return NULL;
2569 static struct isl_tab *context_lex_detect_nonnegative_parameters(
2570 struct isl_context *context, struct isl_tab *tab)
2572 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2573 struct isl_tab_undo *snap;
2575 if (!tab)
2576 return NULL;
2578 snap = isl_tab_snap(clex->tab);
2579 if (isl_tab_push_basis(clex->tab) < 0)
2580 goto error;
2582 tab = tab_detect_nonnegative_parameters(tab, clex->tab);
2584 if (isl_tab_rollback(clex->tab, snap) < 0)
2585 goto error;
2587 return tab;
2588 error:
2589 isl_tab_free(tab);
2590 return NULL;
2593 static void context_lex_invalidate(struct isl_context *context)
2595 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2596 isl_tab_free(clex->tab);
2597 clex->tab = NULL;
2600 static __isl_null struct isl_context *context_lex_free(
2601 struct isl_context *context)
2603 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2604 isl_tab_free(clex->tab);
2605 free(clex);
2607 return NULL;
2610 struct isl_context_op isl_context_lex_op = {
2611 context_lex_detect_nonnegative_parameters,
2612 context_lex_peek_basic_set,
2613 context_lex_peek_tab,
2614 context_lex_add_eq,
2615 context_lex_add_ineq,
2616 context_lex_ineq_sign,
2617 context_lex_test_ineq,
2618 context_lex_get_div,
2619 context_lex_insert_div,
2620 context_lex_detect_equalities,
2621 context_lex_best_split,
2622 context_lex_is_empty,
2623 context_lex_is_ok,
2624 context_lex_save,
2625 context_lex_restore,
2626 context_lex_discard,
2627 context_lex_invalidate,
2628 context_lex_free,
2631 static struct isl_tab *context_tab_for_lexmin(struct isl_basic_set *bset)
2633 struct isl_tab *tab;
2635 if (!bset)
2636 return NULL;
2637 tab = tab_for_lexmin((struct isl_basic_map *)bset, NULL, 1, 0);
2638 if (!tab)
2639 goto error;
2640 if (isl_tab_track_bset(tab, bset) < 0)
2641 goto error;
2642 tab = isl_tab_init_samples(tab);
2643 return tab;
2644 error:
2645 isl_basic_set_free(bset);
2646 return NULL;
2649 static struct isl_context *isl_context_lex_alloc(struct isl_basic_set *dom)
2651 struct isl_context_lex *clex;
2653 if (!dom)
2654 return NULL;
2656 clex = isl_alloc_type(dom->ctx, struct isl_context_lex);
2657 if (!clex)
2658 return NULL;
2660 clex->context.op = &isl_context_lex_op;
2662 clex->tab = context_tab_for_lexmin(isl_basic_set_copy(dom));
2663 if (restore_lexmin(clex->tab) < 0)
2664 goto error;
2665 clex->tab = check_integer_feasible(clex->tab);
2666 if (!clex->tab)
2667 goto error;
2669 return &clex->context;
2670 error:
2671 clex->context.op->free(&clex->context);
2672 return NULL;
2675 /* Representation of the context when using generalized basis reduction.
2677 * "shifted" contains the offsets of the unit hypercubes that lie inside the
2678 * context. Any rational point in "shifted" can therefore be rounded
2679 * up to an integer point in the context.
2680 * If the context is constrained by any equality, then "shifted" is not used
2681 * as it would be empty.
2683 struct isl_context_gbr {
2684 struct isl_context context;
2685 struct isl_tab *tab;
2686 struct isl_tab *shifted;
2687 struct isl_tab *cone;
2690 static struct isl_tab *context_gbr_detect_nonnegative_parameters(
2691 struct isl_context *context, struct isl_tab *tab)
2693 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2694 if (!tab)
2695 return NULL;
2696 return tab_detect_nonnegative_parameters(tab, cgbr->tab);
2699 static struct isl_basic_set *context_gbr_peek_basic_set(
2700 struct isl_context *context)
2702 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2703 if (!cgbr->tab)
2704 return NULL;
2705 return isl_tab_peek_bset(cgbr->tab);
2708 static struct isl_tab *context_gbr_peek_tab(struct isl_context *context)
2710 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2711 return cgbr->tab;
2714 /* Initialize the "shifted" tableau of the context, which
2715 * contains the constraints of the original tableau shifted
2716 * by the sum of all negative coefficients. This ensures
2717 * that any rational point in the shifted tableau can
2718 * be rounded up to yield an integer point in the original tableau.
2720 static void gbr_init_shifted(struct isl_context_gbr *cgbr)
2722 int i, j;
2723 struct isl_vec *cst;
2724 struct isl_basic_set *bset = isl_tab_peek_bset(cgbr->tab);
2725 unsigned dim = isl_basic_set_total_dim(bset);
2727 cst = isl_vec_alloc(cgbr->tab->mat->ctx, bset->n_ineq);
2728 if (!cst)
2729 return;
2731 for (i = 0; i < bset->n_ineq; ++i) {
2732 isl_int_set(cst->el[i], bset->ineq[i][0]);
2733 for (j = 0; j < dim; ++j) {
2734 if (!isl_int_is_neg(bset->ineq[i][1 + j]))
2735 continue;
2736 isl_int_add(bset->ineq[i][0], bset->ineq[i][0],
2737 bset->ineq[i][1 + j]);
2741 cgbr->shifted = isl_tab_from_basic_set(bset, 0);
2743 for (i = 0; i < bset->n_ineq; ++i)
2744 isl_int_set(bset->ineq[i][0], cst->el[i]);
2746 isl_vec_free(cst);
2749 /* Check if the shifted tableau is non-empty, and if so
2750 * use the sample point to construct an integer point
2751 * of the context tableau.
2753 static struct isl_vec *gbr_get_shifted_sample(struct isl_context_gbr *cgbr)
2755 struct isl_vec *sample;
2757 if (!cgbr->shifted)
2758 gbr_init_shifted(cgbr);
2759 if (!cgbr->shifted)
2760 return NULL;
2761 if (cgbr->shifted->empty)
2762 return isl_vec_alloc(cgbr->tab->mat->ctx, 0);
2764 sample = isl_tab_get_sample_value(cgbr->shifted);
2765 sample = isl_vec_ceil(sample);
2767 return sample;
2770 static struct isl_basic_set *drop_constant_terms(struct isl_basic_set *bset)
2772 int i;
2774 if (!bset)
2775 return NULL;
2777 for (i = 0; i < bset->n_eq; ++i)
2778 isl_int_set_si(bset->eq[i][0], 0);
2780 for (i = 0; i < bset->n_ineq; ++i)
2781 isl_int_set_si(bset->ineq[i][0], 0);
2783 return bset;
2786 static int use_shifted(struct isl_context_gbr *cgbr)
2788 if (!cgbr->tab)
2789 return 0;
2790 return cgbr->tab->bmap->n_eq == 0 && cgbr->tab->bmap->n_div == 0;
2793 static struct isl_vec *gbr_get_sample(struct isl_context_gbr *cgbr)
2795 struct isl_basic_set *bset;
2796 struct isl_basic_set *cone;
2798 if (isl_tab_sample_is_integer(cgbr->tab))
2799 return isl_tab_get_sample_value(cgbr->tab);
2801 if (use_shifted(cgbr)) {
2802 struct isl_vec *sample;
2804 sample = gbr_get_shifted_sample(cgbr);
2805 if (!sample || sample->size > 0)
2806 return sample;
2808 isl_vec_free(sample);
2811 if (!cgbr->cone) {
2812 bset = isl_tab_peek_bset(cgbr->tab);
2813 cgbr->cone = isl_tab_from_recession_cone(bset, 0);
2814 if (!cgbr->cone)
2815 return NULL;
2816 if (isl_tab_track_bset(cgbr->cone,
2817 isl_basic_set_copy(bset)) < 0)
2818 return NULL;
2820 if (isl_tab_detect_implicit_equalities(cgbr->cone) < 0)
2821 return NULL;
2823 if (cgbr->cone->n_dead == cgbr->cone->n_col) {
2824 struct isl_vec *sample;
2825 struct isl_tab_undo *snap;
2827 if (cgbr->tab->basis) {
2828 if (cgbr->tab->basis->n_col != 1 + cgbr->tab->n_var) {
2829 isl_mat_free(cgbr->tab->basis);
2830 cgbr->tab->basis = NULL;
2832 cgbr->tab->n_zero = 0;
2833 cgbr->tab->n_unbounded = 0;
2836 snap = isl_tab_snap(cgbr->tab);
2838 sample = isl_tab_sample(cgbr->tab);
2840 if (!sample || isl_tab_rollback(cgbr->tab, snap) < 0) {
2841 isl_vec_free(sample);
2842 return NULL;
2845 return sample;
2848 cone = isl_basic_set_dup(isl_tab_peek_bset(cgbr->cone));
2849 cone = drop_constant_terms(cone);
2850 cone = isl_basic_set_update_from_tab(cone, cgbr->cone);
2851 cone = isl_basic_set_underlying_set(cone);
2852 cone = isl_basic_set_gauss(cone, NULL);
2854 bset = isl_basic_set_dup(isl_tab_peek_bset(cgbr->tab));
2855 bset = isl_basic_set_update_from_tab(bset, cgbr->tab);
2856 bset = isl_basic_set_underlying_set(bset);
2857 bset = isl_basic_set_gauss(bset, NULL);
2859 return isl_basic_set_sample_with_cone(bset, cone);
2862 static void check_gbr_integer_feasible(struct isl_context_gbr *cgbr)
2864 struct isl_vec *sample;
2866 if (!cgbr->tab)
2867 return;
2869 if (cgbr->tab->empty)
2870 return;
2872 sample = gbr_get_sample(cgbr);
2873 if (!sample)
2874 goto error;
2876 if (sample->size == 0) {
2877 isl_vec_free(sample);
2878 if (isl_tab_mark_empty(cgbr->tab) < 0)
2879 goto error;
2880 return;
2883 if (isl_tab_add_sample(cgbr->tab, sample) < 0)
2884 goto error;
2886 return;
2887 error:
2888 isl_tab_free(cgbr->tab);
2889 cgbr->tab = NULL;
2892 static struct isl_tab *add_gbr_eq(struct isl_tab *tab, isl_int *eq)
2894 if (!tab)
2895 return NULL;
2897 if (isl_tab_extend_cons(tab, 2) < 0)
2898 goto error;
2900 if (isl_tab_add_eq(tab, eq) < 0)
2901 goto error;
2903 return tab;
2904 error:
2905 isl_tab_free(tab);
2906 return NULL;
2909 /* Add the equality described by "eq" to the context.
2910 * If "check" is set, then we check if the context is empty after
2911 * adding the equality.
2912 * If "update" is set, then we check if the samples are still valid.
2914 * We do not explicitly add shifted copies of the equality to
2915 * cgbr->shifted since they would conflict with each other.
2916 * Instead, we directly mark cgbr->shifted empty.
2918 static void context_gbr_add_eq(struct isl_context *context, isl_int *eq,
2919 int check, int update)
2921 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2923 cgbr->tab = add_gbr_eq(cgbr->tab, eq);
2925 if (cgbr->shifted && !cgbr->shifted->empty && use_shifted(cgbr)) {
2926 if (isl_tab_mark_empty(cgbr->shifted) < 0)
2927 goto error;
2930 if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
2931 if (isl_tab_extend_cons(cgbr->cone, 2) < 0)
2932 goto error;
2933 if (isl_tab_add_eq(cgbr->cone, eq) < 0)
2934 goto error;
2937 if (check) {
2938 int v = tab_has_valid_sample(cgbr->tab, eq, 1);
2939 if (v < 0)
2940 goto error;
2941 if (!v)
2942 check_gbr_integer_feasible(cgbr);
2944 if (update)
2945 cgbr->tab = check_samples(cgbr->tab, eq, 1);
2946 return;
2947 error:
2948 isl_tab_free(cgbr->tab);
2949 cgbr->tab = NULL;
2952 static void add_gbr_ineq(struct isl_context_gbr *cgbr, isl_int *ineq)
2954 if (!cgbr->tab)
2955 return;
2957 if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
2958 goto error;
2960 if (isl_tab_add_ineq(cgbr->tab, ineq) < 0)
2961 goto error;
2963 if (cgbr->shifted && !cgbr->shifted->empty && use_shifted(cgbr)) {
2964 int i;
2965 unsigned dim;
2966 dim = isl_basic_map_total_dim(cgbr->tab->bmap);
2968 if (isl_tab_extend_cons(cgbr->shifted, 1) < 0)
2969 goto error;
2971 for (i = 0; i < dim; ++i) {
2972 if (!isl_int_is_neg(ineq[1 + i]))
2973 continue;
2974 isl_int_add(ineq[0], ineq[0], ineq[1 + i]);
2977 if (isl_tab_add_ineq(cgbr->shifted, ineq) < 0)
2978 goto error;
2980 for (i = 0; i < dim; ++i) {
2981 if (!isl_int_is_neg(ineq[1 + i]))
2982 continue;
2983 isl_int_sub(ineq[0], ineq[0], ineq[1 + i]);
2987 if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
2988 if (isl_tab_extend_cons(cgbr->cone, 1) < 0)
2989 goto error;
2990 if (isl_tab_add_ineq(cgbr->cone, ineq) < 0)
2991 goto error;
2994 return;
2995 error:
2996 isl_tab_free(cgbr->tab);
2997 cgbr->tab = NULL;
3000 static void context_gbr_add_ineq(struct isl_context *context, isl_int *ineq,
3001 int check, int update)
3003 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3005 add_gbr_ineq(cgbr, ineq);
3006 if (!cgbr->tab)
3007 return;
3009 if (check) {
3010 int v = tab_has_valid_sample(cgbr->tab, ineq, 0);
3011 if (v < 0)
3012 goto error;
3013 if (!v)
3014 check_gbr_integer_feasible(cgbr);
3016 if (update)
3017 cgbr->tab = check_samples(cgbr->tab, ineq, 0);
3018 return;
3019 error:
3020 isl_tab_free(cgbr->tab);
3021 cgbr->tab = NULL;
3024 static int context_gbr_add_ineq_wrap(void *user, isl_int *ineq)
3026 struct isl_context *context = (struct isl_context *)user;
3027 context_gbr_add_ineq(context, ineq, 0, 0);
3028 return context->op->is_ok(context) ? 0 : -1;
3031 static enum isl_tab_row_sign context_gbr_ineq_sign(struct isl_context *context,
3032 isl_int *ineq, int strict)
3034 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3035 return tab_ineq_sign(cgbr->tab, ineq, strict);
3038 /* Check whether "ineq" can be added to the tableau without rendering
3039 * it infeasible.
3041 static int context_gbr_test_ineq(struct isl_context *context, isl_int *ineq)
3043 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3044 struct isl_tab_undo *snap;
3045 struct isl_tab_undo *shifted_snap = NULL;
3046 struct isl_tab_undo *cone_snap = NULL;
3047 int feasible;
3049 if (!cgbr->tab)
3050 return -1;
3052 if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
3053 return -1;
3055 snap = isl_tab_snap(cgbr->tab);
3056 if (cgbr->shifted)
3057 shifted_snap = isl_tab_snap(cgbr->shifted);
3058 if (cgbr->cone)
3059 cone_snap = isl_tab_snap(cgbr->cone);
3060 add_gbr_ineq(cgbr, ineq);
3061 check_gbr_integer_feasible(cgbr);
3062 if (!cgbr->tab)
3063 return -1;
3064 feasible = !cgbr->tab->empty;
3065 if (isl_tab_rollback(cgbr->tab, snap) < 0)
3066 return -1;
3067 if (shifted_snap) {
3068 if (isl_tab_rollback(cgbr->shifted, shifted_snap))
3069 return -1;
3070 } else if (cgbr->shifted) {
3071 isl_tab_free(cgbr->shifted);
3072 cgbr->shifted = NULL;
3074 if (cone_snap) {
3075 if (isl_tab_rollback(cgbr->cone, cone_snap))
3076 return -1;
3077 } else if (cgbr->cone) {
3078 isl_tab_free(cgbr->cone);
3079 cgbr->cone = NULL;
3082 return feasible;
3085 /* Return the column of the last of the variables associated to
3086 * a column that has a non-zero coefficient.
3087 * This function is called in a context where only coefficients
3088 * of parameters or divs can be non-zero.
3090 static int last_non_zero_var_col(struct isl_tab *tab, isl_int *p)
3092 int i;
3093 int col;
3095 if (tab->n_var == 0)
3096 return -1;
3098 for (i = tab->n_var - 1; i >= 0; --i) {
3099 if (i >= tab->n_param && i < tab->n_var - tab->n_div)
3100 continue;
3101 if (tab->var[i].is_row)
3102 continue;
3103 col = tab->var[i].index;
3104 if (!isl_int_is_zero(p[col]))
3105 return col;
3108 return -1;
3111 /* Look through all the recently added equalities in the context
3112 * to see if we can propagate any of them to the main tableau.
3114 * The newly added equalities in the context are encoded as pairs
3115 * of inequalities starting at inequality "first".
3117 * We tentatively add each of these equalities to the main tableau
3118 * and if this happens to result in a row with a final coefficient
3119 * that is one or negative one, we use it to kill a column
3120 * in the main tableau. Otherwise, we discard the tentatively
3121 * added row.
3122 * This tentative addition of equality constraints turns
3123 * on the undo facility of the tableau. Turn it off again
3124 * at the end, assuming it was turned off to begin with.
3126 * Return 0 on success and -1 on failure.
3128 static int propagate_equalities(struct isl_context_gbr *cgbr,
3129 struct isl_tab *tab, unsigned first)
3131 int i;
3132 struct isl_vec *eq = NULL;
3133 isl_bool needs_undo;
3135 needs_undo = isl_tab_need_undo(tab);
3136 if (needs_undo < 0)
3137 goto error;
3138 eq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
3139 if (!eq)
3140 goto error;
3142 if (isl_tab_extend_cons(tab, (cgbr->tab->bmap->n_ineq - first)/2) < 0)
3143 goto error;
3145 isl_seq_clr(eq->el + 1 + tab->n_param,
3146 tab->n_var - tab->n_param - tab->n_div);
3147 for (i = first; i < cgbr->tab->bmap->n_ineq; i += 2) {
3148 int j;
3149 int r;
3150 struct isl_tab_undo *snap;
3151 snap = isl_tab_snap(tab);
3153 isl_seq_cpy(eq->el, cgbr->tab->bmap->ineq[i], 1 + tab->n_param);
3154 isl_seq_cpy(eq->el + 1 + tab->n_var - tab->n_div,
3155 cgbr->tab->bmap->ineq[i] + 1 + tab->n_param,
3156 tab->n_div);
3158 r = isl_tab_add_row(tab, eq->el);
3159 if (r < 0)
3160 goto error;
3161 r = tab->con[r].index;
3162 j = last_non_zero_var_col(tab, tab->mat->row[r] + 2 + tab->M);
3163 if (j < 0 || j < tab->n_dead ||
3164 !isl_int_is_one(tab->mat->row[r][0]) ||
3165 (!isl_int_is_one(tab->mat->row[r][2 + tab->M + j]) &&
3166 !isl_int_is_negone(tab->mat->row[r][2 + tab->M + j]))) {
3167 if (isl_tab_rollback(tab, snap) < 0)
3168 goto error;
3169 continue;
3171 if (isl_tab_pivot(tab, r, j) < 0)
3172 goto error;
3173 if (isl_tab_kill_col(tab, j) < 0)
3174 goto error;
3176 if (restore_lexmin(tab) < 0)
3177 goto error;
3180 if (!needs_undo)
3181 isl_tab_clear_undo(tab);
3182 isl_vec_free(eq);
3184 return 0;
3185 error:
3186 isl_vec_free(eq);
3187 isl_tab_free(cgbr->tab);
3188 cgbr->tab = NULL;
3189 return -1;
3192 static int context_gbr_detect_equalities(struct isl_context *context,
3193 struct isl_tab *tab)
3195 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3196 unsigned n_ineq;
3198 if (!cgbr->cone) {
3199 struct isl_basic_set *bset = isl_tab_peek_bset(cgbr->tab);
3200 cgbr->cone = isl_tab_from_recession_cone(bset, 0);
3201 if (!cgbr->cone)
3202 goto error;
3203 if (isl_tab_track_bset(cgbr->cone,
3204 isl_basic_set_copy(bset)) < 0)
3205 goto error;
3207 if (isl_tab_detect_implicit_equalities(cgbr->cone) < 0)
3208 goto error;
3210 n_ineq = cgbr->tab->bmap->n_ineq;
3211 cgbr->tab = isl_tab_detect_equalities(cgbr->tab, cgbr->cone);
3212 if (!cgbr->tab)
3213 return -1;
3214 if (cgbr->tab->bmap->n_ineq > n_ineq &&
3215 propagate_equalities(cgbr, tab, n_ineq) < 0)
3216 return -1;
3218 return 0;
3219 error:
3220 isl_tab_free(cgbr->tab);
3221 cgbr->tab = NULL;
3222 return -1;
3225 static int context_gbr_get_div(struct isl_context *context, struct isl_tab *tab,
3226 struct isl_vec *div)
3228 return get_div(tab, context, div);
3231 static isl_bool context_gbr_insert_div(struct isl_context *context, int pos,
3232 __isl_keep isl_vec *div)
3234 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3235 if (cgbr->cone) {
3236 int r, n_div, o_div;
3238 n_div = isl_basic_map_dim(cgbr->cone->bmap, isl_dim_div);
3239 o_div = cgbr->cone->n_var - n_div;
3241 if (isl_tab_extend_cons(cgbr->cone, 3) < 0)
3242 return isl_bool_error;
3243 if (isl_tab_extend_vars(cgbr->cone, 1) < 0)
3244 return isl_bool_error;
3245 if ((r = isl_tab_insert_var(cgbr->cone, pos)) <0)
3246 return isl_bool_error;
3248 cgbr->cone->bmap = isl_basic_map_insert_div(cgbr->cone->bmap,
3249 r - o_div, div);
3250 if (!cgbr->cone->bmap)
3251 return isl_bool_error;
3252 if (isl_tab_push_var(cgbr->cone, isl_tab_undo_bmap_div,
3253 &cgbr->cone->var[r]) < 0)
3254 return isl_bool_error;
3256 return context_tab_insert_div(cgbr->tab, pos, div,
3257 context_gbr_add_ineq_wrap, context);
3260 static int context_gbr_best_split(struct isl_context *context,
3261 struct isl_tab *tab)
3263 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3264 struct isl_tab_undo *snap;
3265 int r;
3267 snap = isl_tab_snap(cgbr->tab);
3268 r = best_split(tab, cgbr->tab);
3270 if (r >= 0 && isl_tab_rollback(cgbr->tab, snap) < 0)
3271 return -1;
3273 return r;
3276 static int context_gbr_is_empty(struct isl_context *context)
3278 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3279 if (!cgbr->tab)
3280 return -1;
3281 return cgbr->tab->empty;
3284 struct isl_gbr_tab_undo {
3285 struct isl_tab_undo *tab_snap;
3286 struct isl_tab_undo *shifted_snap;
3287 struct isl_tab_undo *cone_snap;
3290 static void *context_gbr_save(struct isl_context *context)
3292 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3293 struct isl_gbr_tab_undo *snap;
3295 if (!cgbr->tab)
3296 return NULL;
3298 snap = isl_alloc_type(cgbr->tab->mat->ctx, struct isl_gbr_tab_undo);
3299 if (!snap)
3300 return NULL;
3302 snap->tab_snap = isl_tab_snap(cgbr->tab);
3303 if (isl_tab_save_samples(cgbr->tab) < 0)
3304 goto error;
3306 if (cgbr->shifted)
3307 snap->shifted_snap = isl_tab_snap(cgbr->shifted);
3308 else
3309 snap->shifted_snap = NULL;
3311 if (cgbr->cone)
3312 snap->cone_snap = isl_tab_snap(cgbr->cone);
3313 else
3314 snap->cone_snap = NULL;
3316 return snap;
3317 error:
3318 free(snap);
3319 return NULL;
3322 static void context_gbr_restore(struct isl_context *context, void *save)
3324 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3325 struct isl_gbr_tab_undo *snap = (struct isl_gbr_tab_undo *)save;
3326 if (!snap)
3327 goto error;
3328 if (isl_tab_rollback(cgbr->tab, snap->tab_snap) < 0)
3329 goto error;
3331 if (snap->shifted_snap) {
3332 if (isl_tab_rollback(cgbr->shifted, snap->shifted_snap) < 0)
3333 goto error;
3334 } else if (cgbr->shifted) {
3335 isl_tab_free(cgbr->shifted);
3336 cgbr->shifted = NULL;
3339 if (snap->cone_snap) {
3340 if (isl_tab_rollback(cgbr->cone, snap->cone_snap) < 0)
3341 goto error;
3342 } else if (cgbr->cone) {
3343 isl_tab_free(cgbr->cone);
3344 cgbr->cone = NULL;
3347 free(snap);
3349 return;
3350 error:
3351 free(snap);
3352 isl_tab_free(cgbr->tab);
3353 cgbr->tab = NULL;
3356 static void context_gbr_discard(void *save)
3358 struct isl_gbr_tab_undo *snap = (struct isl_gbr_tab_undo *)save;
3359 free(snap);
3362 static int context_gbr_is_ok(struct isl_context *context)
3364 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3365 return !!cgbr->tab;
3368 static void context_gbr_invalidate(struct isl_context *context)
3370 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3371 isl_tab_free(cgbr->tab);
3372 cgbr->tab = NULL;
3375 static __isl_null struct isl_context *context_gbr_free(
3376 struct isl_context *context)
3378 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3379 isl_tab_free(cgbr->tab);
3380 isl_tab_free(cgbr->shifted);
3381 isl_tab_free(cgbr->cone);
3382 free(cgbr);
3384 return NULL;
3387 struct isl_context_op isl_context_gbr_op = {
3388 context_gbr_detect_nonnegative_parameters,
3389 context_gbr_peek_basic_set,
3390 context_gbr_peek_tab,
3391 context_gbr_add_eq,
3392 context_gbr_add_ineq,
3393 context_gbr_ineq_sign,
3394 context_gbr_test_ineq,
3395 context_gbr_get_div,
3396 context_gbr_insert_div,
3397 context_gbr_detect_equalities,
3398 context_gbr_best_split,
3399 context_gbr_is_empty,
3400 context_gbr_is_ok,
3401 context_gbr_save,
3402 context_gbr_restore,
3403 context_gbr_discard,
3404 context_gbr_invalidate,
3405 context_gbr_free,
3408 static struct isl_context *isl_context_gbr_alloc(__isl_keep isl_basic_set *dom)
3410 struct isl_context_gbr *cgbr;
3412 if (!dom)
3413 return NULL;
3415 cgbr = isl_calloc_type(dom->ctx, struct isl_context_gbr);
3416 if (!cgbr)
3417 return NULL;
3419 cgbr->context.op = &isl_context_gbr_op;
3421 cgbr->shifted = NULL;
3422 cgbr->cone = NULL;
3423 cgbr->tab = isl_tab_from_basic_set(dom, 1);
3424 cgbr->tab = isl_tab_init_samples(cgbr->tab);
3425 if (!cgbr->tab)
3426 goto error;
3427 check_gbr_integer_feasible(cgbr);
3429 return &cgbr->context;
3430 error:
3431 cgbr->context.op->free(&cgbr->context);
3432 return NULL;
3435 /* Allocate a context corresponding to "dom".
3436 * The representation specific fields are initialized by
3437 * isl_context_lex_alloc or isl_context_gbr_alloc.
3438 * The shared "n_unknown" field is initialized to the number
3439 * of final unknown integer divisions in "dom".
3441 static struct isl_context *isl_context_alloc(__isl_keep isl_basic_set *dom)
3443 struct isl_context *context;
3444 int first;
3446 if (!dom)
3447 return NULL;
3449 if (dom->ctx->opt->context == ISL_CONTEXT_LEXMIN)
3450 context = isl_context_lex_alloc(dom);
3451 else
3452 context = isl_context_gbr_alloc(dom);
3454 if (!context)
3455 return NULL;
3457 first = isl_basic_set_first_unknown_div(dom);
3458 if (first < 0)
3459 return context->op->free(context);
3460 context->n_unknown = isl_basic_set_dim(dom, isl_dim_div) - first;
3462 return context;
3465 /* Construct an isl_sol_map structure for accumulating the solution.
3466 * If track_empty is set, then we also keep track of the parts
3467 * of the context where there is no solution.
3468 * If max is set, then we are solving a maximization, rather than
3469 * a minimization problem, which means that the variables in the
3470 * tableau have value "M - x" rather than "M + x".
3472 static struct isl_sol *sol_map_init(struct isl_basic_map *bmap,
3473 struct isl_basic_set *dom, int track_empty, int max)
3475 struct isl_sol_map *sol_map = NULL;
3477 if (!bmap)
3478 goto error;
3480 sol_map = isl_calloc_type(bmap->ctx, struct isl_sol_map);
3481 if (!sol_map)
3482 goto error;
3484 sol_map->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
3485 sol_map->sol.dec_level.callback.run = &sol_dec_level_wrap;
3486 sol_map->sol.dec_level.sol = &sol_map->sol;
3487 sol_map->sol.max = max;
3488 sol_map->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
3489 sol_map->sol.add = &sol_map_add_wrap;
3490 sol_map->sol.add_empty = track_empty ? &sol_map_add_empty_wrap : NULL;
3491 sol_map->sol.free = &sol_map_free_wrap;
3492 sol_map->map = isl_map_alloc_space(isl_basic_map_get_space(bmap), 1,
3493 ISL_MAP_DISJOINT);
3494 if (!sol_map->map)
3495 goto error;
3497 sol_map->sol.context = isl_context_alloc(dom);
3498 if (!sol_map->sol.context)
3499 goto error;
3501 if (track_empty) {
3502 sol_map->empty = isl_set_alloc_space(isl_basic_set_get_space(dom),
3503 1, ISL_SET_DISJOINT);
3504 if (!sol_map->empty)
3505 goto error;
3508 isl_basic_set_free(dom);
3509 return &sol_map->sol;
3510 error:
3511 isl_basic_set_free(dom);
3512 sol_map_free(sol_map);
3513 return NULL;
3516 /* Check whether all coefficients of (non-parameter) variables
3517 * are non-positive, meaning that no pivots can be performed on the row.
3519 static int is_critical(struct isl_tab *tab, int row)
3521 int j;
3522 unsigned off = 2 + tab->M;
3524 for (j = tab->n_dead; j < tab->n_col; ++j) {
3525 if (tab->col_var[j] >= 0 &&
3526 (tab->col_var[j] < tab->n_param ||
3527 tab->col_var[j] >= tab->n_var - tab->n_div))
3528 continue;
3530 if (isl_int_is_pos(tab->mat->row[row][off + j]))
3531 return 0;
3534 return 1;
3537 /* Check whether the inequality represented by vec is strict over the integers,
3538 * i.e., there are no integer values satisfying the constraint with
3539 * equality. This happens if the gcd of the coefficients is not a divisor
3540 * of the constant term. If so, scale the constraint down by the gcd
3541 * of the coefficients.
3543 static int is_strict(struct isl_vec *vec)
3545 isl_int gcd;
3546 int strict = 0;
3548 isl_int_init(gcd);
3549 isl_seq_gcd(vec->el + 1, vec->size - 1, &gcd);
3550 if (!isl_int_is_one(gcd)) {
3551 strict = !isl_int_is_divisible_by(vec->el[0], gcd);
3552 isl_int_fdiv_q(vec->el[0], vec->el[0], gcd);
3553 isl_seq_scale_down(vec->el + 1, vec->el + 1, gcd, vec->size-1);
3555 isl_int_clear(gcd);
3557 return strict;
3560 /* Determine the sign of the given row of the main tableau.
3561 * The result is one of
3562 * isl_tab_row_pos: always non-negative; no pivot needed
3563 * isl_tab_row_neg: always non-positive; pivot
3564 * isl_tab_row_any: can be both positive and negative; split
3566 * We first handle some simple cases
3567 * - the row sign may be known already
3568 * - the row may be obviously non-negative
3569 * - the parametric constant may be equal to that of another row
3570 * for which we know the sign. This sign will be either "pos" or
3571 * "any". If it had been "neg" then we would have pivoted before.
3573 * If none of these cases hold, we check the value of the row for each
3574 * of the currently active samples. Based on the signs of these values
3575 * we make an initial determination of the sign of the row.
3577 * all zero -> unk(nown)
3578 * all non-negative -> pos
3579 * all non-positive -> neg
3580 * both negative and positive -> all
3582 * If we end up with "all", we are done.
3583 * Otherwise, we perform a check for positive and/or negative
3584 * values as follows.
3586 * samples neg unk pos
3587 * <0 ? Y N Y N
3588 * pos any pos
3589 * >0 ? Y N Y N
3590 * any neg any neg
3592 * There is no special sign for "zero", because we can usually treat zero
3593 * as either non-negative or non-positive, whatever works out best.
3594 * However, if the row is "critical", meaning that pivoting is impossible
3595 * then we don't want to limp zero with the non-positive case, because
3596 * then we we would lose the solution for those values of the parameters
3597 * where the value of the row is zero. Instead, we treat 0 as non-negative
3598 * ensuring a split if the row can attain both zero and negative values.
3599 * The same happens when the original constraint was one that could not
3600 * be satisfied with equality by any integer values of the parameters.
3601 * In this case, we normalize the constraint, but then a value of zero
3602 * for the normalized constraint is actually a positive value for the
3603 * original constraint, so again we need to treat zero as non-negative.
3604 * In both these cases, we have the following decision tree instead:
3606 * all non-negative -> pos
3607 * all negative -> neg
3608 * both negative and non-negative -> all
3610 * samples neg pos
3611 * <0 ? Y N
3612 * any pos
3613 * >=0 ? Y N
3614 * any neg
3616 static enum isl_tab_row_sign row_sign(struct isl_tab *tab,
3617 struct isl_sol *sol, int row)
3619 struct isl_vec *ineq = NULL;
3620 enum isl_tab_row_sign res = isl_tab_row_unknown;
3621 int critical;
3622 int strict;
3623 int row2;
3625 if (tab->row_sign[row] != isl_tab_row_unknown)
3626 return tab->row_sign[row];
3627 if (is_obviously_nonneg(tab, row))
3628 return isl_tab_row_pos;
3629 for (row2 = tab->n_redundant; row2 < tab->n_row; ++row2) {
3630 if (tab->row_sign[row2] == isl_tab_row_unknown)
3631 continue;
3632 if (identical_parameter_line(tab, row, row2))
3633 return tab->row_sign[row2];
3636 critical = is_critical(tab, row);
3638 ineq = get_row_parameter_ineq(tab, row);
3639 if (!ineq)
3640 goto error;
3642 strict = is_strict(ineq);
3644 res = sol->context->op->ineq_sign(sol->context, ineq->el,
3645 critical || strict);
3647 if (res == isl_tab_row_unknown || res == isl_tab_row_pos) {
3648 /* test for negative values */
3649 int feasible;
3650 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3651 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3653 feasible = sol->context->op->test_ineq(sol->context, ineq->el);
3654 if (feasible < 0)
3655 goto error;
3656 if (!feasible)
3657 res = isl_tab_row_pos;
3658 else
3659 res = (res == isl_tab_row_unknown) ? isl_tab_row_neg
3660 : isl_tab_row_any;
3661 if (res == isl_tab_row_neg) {
3662 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3663 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3667 if (res == isl_tab_row_neg) {
3668 /* test for positive values */
3669 int feasible;
3670 if (!critical && !strict)
3671 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3673 feasible = sol->context->op->test_ineq(sol->context, ineq->el);
3674 if (feasible < 0)
3675 goto error;
3676 if (feasible)
3677 res = isl_tab_row_any;
3680 isl_vec_free(ineq);
3681 return res;
3682 error:
3683 isl_vec_free(ineq);
3684 return isl_tab_row_unknown;
3687 static void find_solutions(struct isl_sol *sol, struct isl_tab *tab);
3689 /* Find solutions for values of the parameters that satisfy the given
3690 * inequality.
3692 * We currently take a snapshot of the context tableau that is reset
3693 * when we return from this function, while we make a copy of the main
3694 * tableau, leaving the original main tableau untouched.
3695 * These are fairly arbitrary choices. Making a copy also of the context
3696 * tableau would obviate the need to undo any changes made to it later,
3697 * while taking a snapshot of the main tableau could reduce memory usage.
3698 * If we were to switch to taking a snapshot of the main tableau,
3699 * we would have to keep in mind that we need to save the row signs
3700 * and that we need to do this before saving the current basis
3701 * such that the basis has been restore before we restore the row signs.
3703 static void find_in_pos(struct isl_sol *sol, struct isl_tab *tab, isl_int *ineq)
3705 void *saved;
3707 if (!sol->context)
3708 goto error;
3709 saved = sol->context->op->save(sol->context);
3711 tab = isl_tab_dup(tab);
3712 if (!tab)
3713 goto error;
3715 sol->context->op->add_ineq(sol->context, ineq, 0, 1);
3717 find_solutions(sol, tab);
3719 if (!sol->error)
3720 sol->context->op->restore(sol->context, saved);
3721 else
3722 sol->context->op->discard(saved);
3723 return;
3724 error:
3725 sol->error = 1;
3728 /* Record the absence of solutions for those values of the parameters
3729 * that do not satisfy the given inequality with equality.
3731 static void no_sol_in_strict(struct isl_sol *sol,
3732 struct isl_tab *tab, struct isl_vec *ineq)
3734 int empty;
3735 void *saved;
3737 if (!sol->context || sol->error)
3738 goto error;
3739 saved = sol->context->op->save(sol->context);
3741 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3743 sol->context->op->add_ineq(sol->context, ineq->el, 1, 0);
3744 if (!sol->context)
3745 goto error;
3747 empty = tab->empty;
3748 tab->empty = 1;
3749 sol_add(sol, tab);
3750 tab->empty = empty;
3752 isl_int_add_ui(ineq->el[0], ineq->el[0], 1);
3754 sol->context->op->restore(sol->context, saved);
3755 return;
3756 error:
3757 sol->error = 1;
3760 /* Reset all row variables that are marked to have a sign that may
3761 * be both positive and negative to have an unknown sign.
3763 static void reset_any_to_unknown(struct isl_tab *tab)
3765 int row;
3767 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3768 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
3769 continue;
3770 if (tab->row_sign[row] == isl_tab_row_any)
3771 tab->row_sign[row] = isl_tab_row_unknown;
3775 /* Compute the lexicographic minimum of the set represented by the main
3776 * tableau "tab" within the context "sol->context_tab".
3777 * On entry the sample value of the main tableau is lexicographically
3778 * less than or equal to this lexicographic minimum.
3779 * Pivots are performed until a feasible point is found, which is then
3780 * necessarily equal to the minimum, or until the tableau is found to
3781 * be infeasible. Some pivots may need to be performed for only some
3782 * feasible values of the context tableau. If so, the context tableau
3783 * is split into a part where the pivot is needed and a part where it is not.
3785 * Whenever we enter the main loop, the main tableau is such that no
3786 * "obvious" pivots need to be performed on it, where "obvious" means
3787 * that the given row can be seen to be negative without looking at
3788 * the context tableau. In particular, for non-parametric problems,
3789 * no pivots need to be performed on the main tableau.
3790 * The caller of find_solutions is responsible for making this property
3791 * hold prior to the first iteration of the loop, while restore_lexmin
3792 * is called before every other iteration.
3794 * Inside the main loop, we first examine the signs of the rows of
3795 * the main tableau within the context of the context tableau.
3796 * If we find a row that is always non-positive for all values of
3797 * the parameters satisfying the context tableau and negative for at
3798 * least one value of the parameters, we perform the appropriate pivot
3799 * and start over. An exception is the case where no pivot can be
3800 * performed on the row. In this case, we require that the sign of
3801 * the row is negative for all values of the parameters (rather than just
3802 * non-positive). This special case is handled inside row_sign, which
3803 * will say that the row can have any sign if it determines that it can
3804 * attain both negative and zero values.
3806 * If we can't find a row that always requires a pivot, but we can find
3807 * one or more rows that require a pivot for some values of the parameters
3808 * (i.e., the row can attain both positive and negative signs), then we split
3809 * the context tableau into two parts, one where we force the sign to be
3810 * non-negative and one where we force is to be negative.
3811 * The non-negative part is handled by a recursive call (through find_in_pos).
3812 * Upon returning from this call, we continue with the negative part and
3813 * perform the required pivot.
3815 * If no such rows can be found, all rows are non-negative and we have
3816 * found a (rational) feasible point. If we only wanted a rational point
3817 * then we are done.
3818 * Otherwise, we check if all values of the sample point of the tableau
3819 * are integral for the variables. If so, we have found the minimal
3820 * integral point and we are done.
3821 * If the sample point is not integral, then we need to make a distinction
3822 * based on whether the constant term is non-integral or the coefficients
3823 * of the parameters. Furthermore, in order to decide how to handle
3824 * the non-integrality, we also need to know whether the coefficients
3825 * of the other columns in the tableau are integral. This leads
3826 * to the following table. The first two rows do not correspond
3827 * to a non-integral sample point and are only mentioned for completeness.
3829 * constant parameters other
3831 * int int int |
3832 * int int rat | -> no problem
3834 * rat int int -> fail
3836 * rat int rat -> cut
3838 * int rat rat |
3839 * rat rat rat | -> parametric cut
3841 * int rat int |
3842 * rat rat int | -> split context
3844 * If the parametric constant is completely integral, then there is nothing
3845 * to be done. If the constant term is non-integral, but all the other
3846 * coefficient are integral, then there is nothing that can be done
3847 * and the tableau has no integral solution.
3848 * If, on the other hand, one or more of the other columns have rational
3849 * coefficients, but the parameter coefficients are all integral, then
3850 * we can perform a regular (non-parametric) cut.
3851 * Finally, if there is any parameter coefficient that is non-integral,
3852 * then we need to involve the context tableau. There are two cases here.
3853 * If at least one other column has a rational coefficient, then we
3854 * can perform a parametric cut in the main tableau by adding a new
3855 * integer division in the context tableau.
3856 * If all other columns have integral coefficients, then we need to
3857 * enforce that the rational combination of parameters (c + \sum a_i y_i)/m
3858 * is always integral. We do this by introducing an integer division
3859 * q = floor((c + \sum a_i y_i)/m) and stipulating that its argument should
3860 * always be integral in the context tableau, i.e., m q = c + \sum a_i y_i.
3861 * Since q is expressed in the tableau as
3862 * c + \sum a_i y_i - m q >= 0
3863 * -c - \sum a_i y_i + m q + m - 1 >= 0
3864 * it is sufficient to add the inequality
3865 * -c - \sum a_i y_i + m q >= 0
3866 * In the part of the context where this inequality does not hold, the
3867 * main tableau is marked as being empty.
3869 static void find_solutions(struct isl_sol *sol, struct isl_tab *tab)
3871 struct isl_context *context;
3872 int r;
3874 if (!tab || sol->error)
3875 goto error;
3877 context = sol->context;
3879 if (tab->empty)
3880 goto done;
3881 if (context->op->is_empty(context))
3882 goto done;
3884 for (r = 0; r >= 0 && tab && !tab->empty; r = restore_lexmin(tab)) {
3885 int flags;
3886 int row;
3887 enum isl_tab_row_sign sgn;
3888 int split = -1;
3889 int n_split = 0;
3891 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3892 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
3893 continue;
3894 sgn = row_sign(tab, sol, row);
3895 if (!sgn)
3896 goto error;
3897 tab->row_sign[row] = sgn;
3898 if (sgn == isl_tab_row_any)
3899 n_split++;
3900 if (sgn == isl_tab_row_any && split == -1)
3901 split = row;
3902 if (sgn == isl_tab_row_neg)
3903 break;
3905 if (row < tab->n_row)
3906 continue;
3907 if (split != -1) {
3908 struct isl_vec *ineq;
3909 if (n_split != 1)
3910 split = context->op->best_split(context, tab);
3911 if (split < 0)
3912 goto error;
3913 ineq = get_row_parameter_ineq(tab, split);
3914 if (!ineq)
3915 goto error;
3916 is_strict(ineq);
3917 reset_any_to_unknown(tab);
3918 tab->row_sign[split] = isl_tab_row_pos;
3919 sol_inc_level(sol);
3920 find_in_pos(sol, tab, ineq->el);
3921 tab->row_sign[split] = isl_tab_row_neg;
3922 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3923 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3924 if (!sol->error)
3925 context->op->add_ineq(context, ineq->el, 0, 1);
3926 isl_vec_free(ineq);
3927 if (sol->error)
3928 goto error;
3929 continue;
3931 if (tab->rational)
3932 break;
3933 row = first_non_integer_row(tab, &flags);
3934 if (row < 0)
3935 break;
3936 if (ISL_FL_ISSET(flags, I_PAR)) {
3937 if (ISL_FL_ISSET(flags, I_VAR)) {
3938 if (isl_tab_mark_empty(tab) < 0)
3939 goto error;
3940 break;
3942 row = add_cut(tab, row);
3943 } else if (ISL_FL_ISSET(flags, I_VAR)) {
3944 struct isl_vec *div;
3945 struct isl_vec *ineq;
3946 int d;
3947 div = get_row_split_div(tab, row);
3948 if (!div)
3949 goto error;
3950 d = context->op->get_div(context, tab, div);
3951 isl_vec_free(div);
3952 if (d < 0)
3953 goto error;
3954 ineq = ineq_for_div(context->op->peek_basic_set(context), d);
3955 if (!ineq)
3956 goto error;
3957 sol_inc_level(sol);
3958 no_sol_in_strict(sol, tab, ineq);
3959 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3960 context->op->add_ineq(context, ineq->el, 1, 1);
3961 isl_vec_free(ineq);
3962 if (sol->error || !context->op->is_ok(context))
3963 goto error;
3964 tab = set_row_cst_to_div(tab, row, d);
3965 if (context->op->is_empty(context))
3966 break;
3967 } else
3968 row = add_parametric_cut(tab, row, context);
3969 if (row < 0)
3970 goto error;
3972 if (r < 0)
3973 goto error;
3974 done:
3975 sol_add(sol, tab);
3976 isl_tab_free(tab);
3977 return;
3978 error:
3979 isl_tab_free(tab);
3980 sol->error = 1;
3983 /* Does "sol" contain a pair of partial solutions that could potentially
3984 * be merged?
3986 * We currently only check that "sol" is not in an error state
3987 * and that there are at least two partial solutions of which the final two
3988 * are defined at the same level.
3990 static int sol_has_mergeable_solutions(struct isl_sol *sol)
3992 if (sol->error)
3993 return 0;
3994 if (!sol->partial)
3995 return 0;
3996 if (!sol->partial->next)
3997 return 0;
3998 return sol->partial->level == sol->partial->next->level;
4001 /* Compute the lexicographic minimum of the set represented by the main
4002 * tableau "tab" within the context "sol->context_tab".
4004 * As a preprocessing step, we first transfer all the purely parametric
4005 * equalities from the main tableau to the context tableau, i.e.,
4006 * parameters that have been pivoted to a row.
4007 * These equalities are ignored by the main algorithm, because the
4008 * corresponding rows may not be marked as being non-negative.
4009 * In parts of the context where the added equality does not hold,
4010 * the main tableau is marked as being empty.
4012 * Before we embark on the actual computation, we save a copy
4013 * of the context. When we return, we check if there are any
4014 * partial solutions that can potentially be merged. If so,
4015 * we perform a rollback to the initial state of the context.
4016 * The merging of partial solutions happens inside calls to
4017 * sol_dec_level that are pushed onto the undo stack of the context.
4018 * If there are no partial solutions that can potentially be merged
4019 * then the rollback is skipped as it would just be wasted effort.
4021 static void find_solutions_main(struct isl_sol *sol, struct isl_tab *tab)
4023 int row;
4024 void *saved;
4026 if (!tab)
4027 goto error;
4029 sol->level = 0;
4031 for (row = tab->n_redundant; row < tab->n_row; ++row) {
4032 int p;
4033 struct isl_vec *eq;
4035 if (tab->row_var[row] < 0)
4036 continue;
4037 if (tab->row_var[row] >= tab->n_param &&
4038 tab->row_var[row] < tab->n_var - tab->n_div)
4039 continue;
4040 if (tab->row_var[row] < tab->n_param)
4041 p = tab->row_var[row];
4042 else
4043 p = tab->row_var[row]
4044 + tab->n_param - (tab->n_var - tab->n_div);
4046 eq = isl_vec_alloc(tab->mat->ctx, 1+tab->n_param+tab->n_div);
4047 if (!eq)
4048 goto error;
4049 get_row_parameter_line(tab, row, eq->el);
4050 isl_int_neg(eq->el[1 + p], tab->mat->row[row][0]);
4051 eq = isl_vec_normalize(eq);
4053 sol_inc_level(sol);
4054 no_sol_in_strict(sol, tab, eq);
4056 isl_seq_neg(eq->el, eq->el, eq->size);
4057 sol_inc_level(sol);
4058 no_sol_in_strict(sol, tab, eq);
4059 isl_seq_neg(eq->el, eq->el, eq->size);
4061 sol->context->op->add_eq(sol->context, eq->el, 1, 1);
4063 isl_vec_free(eq);
4065 if (isl_tab_mark_redundant(tab, row) < 0)
4066 goto error;
4068 if (sol->context->op->is_empty(sol->context))
4069 break;
4071 row = tab->n_redundant - 1;
4074 saved = sol->context->op->save(sol->context);
4076 find_solutions(sol, tab);
4078 if (sol_has_mergeable_solutions(sol))
4079 sol->context->op->restore(sol->context, saved);
4080 else
4081 sol->context->op->discard(saved);
4083 sol->level = 0;
4084 sol_pop(sol);
4086 return;
4087 error:
4088 isl_tab_free(tab);
4089 sol->error = 1;
4092 /* Check if integer division "div" of "dom" also occurs in "bmap".
4093 * If so, return its position within the divs.
4094 * If not, return -1.
4096 static int find_context_div(struct isl_basic_map *bmap,
4097 struct isl_basic_set *dom, unsigned div)
4099 int i;
4100 unsigned b_dim = isl_space_dim(bmap->dim, isl_dim_all);
4101 unsigned d_dim = isl_space_dim(dom->dim, isl_dim_all);
4103 if (isl_int_is_zero(dom->div[div][0]))
4104 return -1;
4105 if (isl_seq_first_non_zero(dom->div[div] + 2 + d_dim, dom->n_div) != -1)
4106 return -1;
4108 for (i = 0; i < bmap->n_div; ++i) {
4109 if (isl_int_is_zero(bmap->div[i][0]))
4110 continue;
4111 if (isl_seq_first_non_zero(bmap->div[i] + 2 + d_dim,
4112 (b_dim - d_dim) + bmap->n_div) != -1)
4113 continue;
4114 if (isl_seq_eq(bmap->div[i], dom->div[div], 2 + d_dim))
4115 return i;
4117 return -1;
4120 /* The correspondence between the variables in the main tableau,
4121 * the context tableau, and the input map and domain is as follows.
4122 * The first n_param and the last n_div variables of the main tableau
4123 * form the variables of the context tableau.
4124 * In the basic map, these n_param variables correspond to the
4125 * parameters and the input dimensions. In the domain, they correspond
4126 * to the parameters and the set dimensions.
4127 * The n_div variables correspond to the integer divisions in the domain.
4128 * To ensure that everything lines up, we may need to copy some of the
4129 * integer divisions of the domain to the map. These have to be placed
4130 * in the same order as those in the context and they have to be placed
4131 * after any other integer divisions that the map may have.
4132 * This function performs the required reordering.
4134 static struct isl_basic_map *align_context_divs(struct isl_basic_map *bmap,
4135 struct isl_basic_set *dom)
4137 int i;
4138 int common = 0;
4139 int other;
4141 for (i = 0; i < dom->n_div; ++i)
4142 if (find_context_div(bmap, dom, i) != -1)
4143 common++;
4144 other = bmap->n_div - common;
4145 if (dom->n_div - common > 0) {
4146 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
4147 dom->n_div - common, 0, 0);
4148 if (!bmap)
4149 return NULL;
4151 for (i = 0; i < dom->n_div; ++i) {
4152 int pos = find_context_div(bmap, dom, i);
4153 if (pos < 0) {
4154 pos = isl_basic_map_alloc_div(bmap);
4155 if (pos < 0)
4156 goto error;
4157 isl_int_set_si(bmap->div[pos][0], 0);
4159 if (pos != other + i)
4160 isl_basic_map_swap_div(bmap, pos, other + i);
4162 return bmap;
4163 error:
4164 isl_basic_map_free(bmap);
4165 return NULL;
4168 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4169 * some obvious symmetries.
4171 * We make sure the divs in the domain are properly ordered,
4172 * because they will be added one by one in the given order
4173 * during the construction of the solution map.
4175 static struct isl_sol *basic_map_partial_lexopt_base_sol(
4176 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4177 __isl_give isl_set **empty, int max,
4178 struct isl_sol *(*init)(__isl_keep isl_basic_map *bmap,
4179 __isl_take isl_basic_set *dom, int track_empty, int max))
4181 struct isl_tab *tab;
4182 struct isl_sol *sol = NULL;
4183 struct isl_context *context;
4185 if (dom->n_div) {
4186 dom = isl_basic_set_order_divs(dom);
4187 bmap = align_context_divs(bmap, dom);
4189 sol = init(bmap, dom, !!empty, max);
4190 if (!sol)
4191 goto error;
4193 context = sol->context;
4194 if (isl_basic_set_plain_is_empty(context->op->peek_basic_set(context)))
4195 /* nothing */;
4196 else if (isl_basic_map_plain_is_empty(bmap)) {
4197 if (sol->add_empty)
4198 sol->add_empty(sol,
4199 isl_basic_set_copy(context->op->peek_basic_set(context)));
4200 } else {
4201 tab = tab_for_lexmin(bmap,
4202 context->op->peek_basic_set(context), 1, max);
4203 tab = context->op->detect_nonnegative_parameters(context, tab);
4204 find_solutions_main(sol, tab);
4206 if (sol->error)
4207 goto error;
4209 isl_basic_map_free(bmap);
4210 return sol;
4211 error:
4212 sol_free(sol);
4213 isl_basic_map_free(bmap);
4214 return NULL;
4217 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4218 * some obvious symmetries.
4220 * We call basic_map_partial_lexopt_base_sol and extract the results.
4222 static __isl_give isl_map *basic_map_partial_lexopt_base(
4223 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4224 __isl_give isl_set **empty, int max)
4226 isl_map *result = NULL;
4227 struct isl_sol *sol;
4228 struct isl_sol_map *sol_map;
4230 sol = basic_map_partial_lexopt_base_sol(bmap, dom, empty, max,
4231 &sol_map_init);
4232 if (!sol)
4233 return NULL;
4234 sol_map = (struct isl_sol_map *) sol;
4236 result = isl_map_copy(sol_map->map);
4237 if (empty)
4238 *empty = isl_set_copy(sol_map->empty);
4239 sol_free(&sol_map->sol);
4240 return result;
4243 /* Return a count of the number of occurrences of the "n" first
4244 * variables in the inequality constraints of "bmap".
4246 static __isl_give int *count_occurrences(__isl_keep isl_basic_map *bmap,
4247 int n)
4249 int i, j;
4250 isl_ctx *ctx;
4251 int *occurrences;
4253 if (!bmap)
4254 return NULL;
4255 ctx = isl_basic_map_get_ctx(bmap);
4256 occurrences = isl_calloc_array(ctx, int, n);
4257 if (!occurrences)
4258 return NULL;
4260 for (i = 0; i < bmap->n_ineq; ++i) {
4261 for (j = 0; j < n; ++j) {
4262 if (!isl_int_is_zero(bmap->ineq[i][1 + j]))
4263 occurrences[j]++;
4267 return occurrences;
4270 /* Do all of the "n" variables with non-zero coefficients in "c"
4271 * occur in exactly a single constraint.
4272 * "occurrences" is an array of length "n" containing the number
4273 * of occurrences of each of the variables in the inequality constraints.
4275 static int single_occurrence(int n, isl_int *c, int *occurrences)
4277 int i;
4279 for (i = 0; i < n; ++i) {
4280 if (isl_int_is_zero(c[i]))
4281 continue;
4282 if (occurrences[i] != 1)
4283 return 0;
4286 return 1;
4289 /* Do all of the "n" initial variables that occur in inequality constraint
4290 * "ineq" of "bmap" only occur in that constraint?
4292 static int all_single_occurrence(__isl_keep isl_basic_map *bmap, int ineq,
4293 int n)
4295 int i, j;
4297 for (i = 0; i < n; ++i) {
4298 if (isl_int_is_zero(bmap->ineq[ineq][1 + i]))
4299 continue;
4300 for (j = 0; j < bmap->n_ineq; ++j) {
4301 if (j == ineq)
4302 continue;
4303 if (!isl_int_is_zero(bmap->ineq[j][1 + i]))
4304 return 0;
4308 return 1;
4311 /* Structure used during detection of parallel constraints.
4312 * n_in: number of "input" variables: isl_dim_param + isl_dim_in
4313 * n_out: number of "output" variables: isl_dim_out + isl_dim_div
4314 * val: the coefficients of the output variables
4316 struct isl_constraint_equal_info {
4317 isl_basic_map *bmap;
4318 unsigned n_in;
4319 unsigned n_out;
4320 isl_int *val;
4323 /* Check whether the coefficients of the output variables
4324 * of the constraint in "entry" are equal to info->val.
4326 static int constraint_equal(const void *entry, const void *val)
4328 isl_int **row = (isl_int **)entry;
4329 const struct isl_constraint_equal_info *info = val;
4331 return isl_seq_eq((*row) + 1 + info->n_in, info->val, info->n_out);
4334 /* Check whether "bmap" has a pair of constraints that have
4335 * the same coefficients for the output variables.
4336 * Note that the coefficients of the existentially quantified
4337 * variables need to be zero since the existentially quantified
4338 * of the result are usually not the same as those of the input.
4339 * Furthermore, check that each of the input variables that occur
4340 * in those constraints does not occur in any other constraint.
4341 * If so, return 1 and return the row indices of the two constraints
4342 * in *first and *second.
4344 static int parallel_constraints(__isl_keep isl_basic_map *bmap,
4345 int *first, int *second)
4347 int i;
4348 isl_ctx *ctx;
4349 int *occurrences = NULL;
4350 struct isl_hash_table *table = NULL;
4351 struct isl_hash_table_entry *entry;
4352 struct isl_constraint_equal_info info;
4353 unsigned n_out;
4354 unsigned n_div;
4356 ctx = isl_basic_map_get_ctx(bmap);
4357 table = isl_hash_table_alloc(ctx, bmap->n_ineq);
4358 if (!table)
4359 goto error;
4361 info.n_in = isl_basic_map_dim(bmap, isl_dim_param) +
4362 isl_basic_map_dim(bmap, isl_dim_in);
4363 occurrences = count_occurrences(bmap, info.n_in);
4364 if (info.n_in && !occurrences)
4365 goto error;
4366 info.bmap = bmap;
4367 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4368 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4369 info.n_out = n_out + n_div;
4370 for (i = 0; i < bmap->n_ineq; ++i) {
4371 uint32_t hash;
4373 info.val = bmap->ineq[i] + 1 + info.n_in;
4374 if (isl_seq_first_non_zero(info.val, n_out) < 0)
4375 continue;
4376 if (isl_seq_first_non_zero(info.val + n_out, n_div) >= 0)
4377 continue;
4378 if (!single_occurrence(info.n_in, bmap->ineq[i] + 1,
4379 occurrences))
4380 continue;
4381 hash = isl_seq_get_hash(info.val, info.n_out);
4382 entry = isl_hash_table_find(ctx, table, hash,
4383 constraint_equal, &info, 1);
4384 if (!entry)
4385 goto error;
4386 if (entry->data)
4387 break;
4388 entry->data = &bmap->ineq[i];
4391 if (i < bmap->n_ineq) {
4392 *first = ((isl_int **)entry->data) - bmap->ineq;
4393 *second = i;
4396 isl_hash_table_free(ctx, table);
4397 free(occurrences);
4399 return i < bmap->n_ineq;
4400 error:
4401 isl_hash_table_free(ctx, table);
4402 free(occurrences);
4403 return -1;
4406 /* Given a set of upper bounds in "var", add constraints to "bset"
4407 * that make the i-th bound smallest.
4409 * In particular, if there are n bounds b_i, then add the constraints
4411 * b_i <= b_j for j > i
4412 * b_i < b_j for j < i
4414 static __isl_give isl_basic_set *select_minimum(__isl_take isl_basic_set *bset,
4415 __isl_keep isl_mat *var, int i)
4417 isl_ctx *ctx;
4418 int j, k;
4420 ctx = isl_mat_get_ctx(var);
4422 for (j = 0; j < var->n_row; ++j) {
4423 if (j == i)
4424 continue;
4425 k = isl_basic_set_alloc_inequality(bset);
4426 if (k < 0)
4427 goto error;
4428 isl_seq_combine(bset->ineq[k], ctx->one, var->row[j],
4429 ctx->negone, var->row[i], var->n_col);
4430 isl_int_set_si(bset->ineq[k][var->n_col], 0);
4431 if (j < i)
4432 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
4435 bset = isl_basic_set_finalize(bset);
4437 return bset;
4438 error:
4439 isl_basic_set_free(bset);
4440 return NULL;
4443 /* Given a set of upper bounds on the last "input" variable m,
4444 * construct a set that assigns the minimal upper bound to m, i.e.,
4445 * construct a set that divides the space into cells where one
4446 * of the upper bounds is smaller than all the others and assign
4447 * this upper bound to m.
4449 * In particular, if there are n bounds b_i, then the result
4450 * consists of n basic sets, each one of the form
4452 * m = b_i
4453 * b_i <= b_j for j > i
4454 * b_i < b_j for j < i
4456 static __isl_give isl_set *set_minimum(__isl_take isl_space *dim,
4457 __isl_take isl_mat *var)
4459 int i, k;
4460 isl_basic_set *bset = NULL;
4461 isl_set *set = NULL;
4463 if (!dim || !var)
4464 goto error;
4466 set = isl_set_alloc_space(isl_space_copy(dim),
4467 var->n_row, ISL_SET_DISJOINT);
4469 for (i = 0; i < var->n_row; ++i) {
4470 bset = isl_basic_set_alloc_space(isl_space_copy(dim), 0,
4471 1, var->n_row - 1);
4472 k = isl_basic_set_alloc_equality(bset);
4473 if (k < 0)
4474 goto error;
4475 isl_seq_cpy(bset->eq[k], var->row[i], var->n_col);
4476 isl_int_set_si(bset->eq[k][var->n_col], -1);
4477 bset = select_minimum(bset, var, i);
4478 set = isl_set_add_basic_set(set, bset);
4481 isl_space_free(dim);
4482 isl_mat_free(var);
4483 return set;
4484 error:
4485 isl_basic_set_free(bset);
4486 isl_set_free(set);
4487 isl_space_free(dim);
4488 isl_mat_free(var);
4489 return NULL;
4492 /* Given that the last input variable of "bmap" represents the minimum
4493 * of the bounds in "cst", check whether we need to split the domain
4494 * based on which bound attains the minimum.
4496 * A split is needed when the minimum appears in an integer division
4497 * or in an equality. Otherwise, it is only needed if it appears in
4498 * an upper bound that is different from the upper bounds on which it
4499 * is defined.
4501 static int need_split_basic_map(__isl_keep isl_basic_map *bmap,
4502 __isl_keep isl_mat *cst)
4504 int i, j;
4505 unsigned total;
4506 unsigned pos;
4508 pos = cst->n_col - 1;
4509 total = isl_basic_map_dim(bmap, isl_dim_all);
4511 for (i = 0; i < bmap->n_div; ++i)
4512 if (!isl_int_is_zero(bmap->div[i][2 + pos]))
4513 return 1;
4515 for (i = 0; i < bmap->n_eq; ++i)
4516 if (!isl_int_is_zero(bmap->eq[i][1 + pos]))
4517 return 1;
4519 for (i = 0; i < bmap->n_ineq; ++i) {
4520 if (isl_int_is_nonneg(bmap->ineq[i][1 + pos]))
4521 continue;
4522 if (!isl_int_is_negone(bmap->ineq[i][1 + pos]))
4523 return 1;
4524 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + pos + 1,
4525 total - pos - 1) >= 0)
4526 return 1;
4528 for (j = 0; j < cst->n_row; ++j)
4529 if (isl_seq_eq(bmap->ineq[i], cst->row[j], cst->n_col))
4530 break;
4531 if (j >= cst->n_row)
4532 return 1;
4535 return 0;
4538 /* Given that the last set variable of "bset" represents the minimum
4539 * of the bounds in "cst", check whether we need to split the domain
4540 * based on which bound attains the minimum.
4542 * We simply call need_split_basic_map here. This is safe because
4543 * the position of the minimum is computed from "cst" and not
4544 * from "bmap".
4546 static int need_split_basic_set(__isl_keep isl_basic_set *bset,
4547 __isl_keep isl_mat *cst)
4549 return need_split_basic_map((isl_basic_map *)bset, cst);
4552 /* Given that the last set variable of "set" represents the minimum
4553 * of the bounds in "cst", check whether we need to split the domain
4554 * based on which bound attains the minimum.
4556 static int need_split_set(__isl_keep isl_set *set, __isl_keep isl_mat *cst)
4558 int i;
4560 for (i = 0; i < set->n; ++i)
4561 if (need_split_basic_set(set->p[i], cst))
4562 return 1;
4564 return 0;
4567 /* Given a set of which the last set variable is the minimum
4568 * of the bounds in "cst", split each basic set in the set
4569 * in pieces where one of the bounds is (strictly) smaller than the others.
4570 * This subdivision is given in "min_expr".
4571 * The variable is subsequently projected out.
4573 * We only do the split when it is needed.
4574 * For example if the last input variable m = min(a,b) and the only
4575 * constraints in the given basic set are lower bounds on m,
4576 * i.e., l <= m = min(a,b), then we can simply project out m
4577 * to obtain l <= a and l <= b, without having to split on whether
4578 * m is equal to a or b.
4580 static __isl_give isl_set *split(__isl_take isl_set *empty,
4581 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
4583 int n_in;
4584 int i;
4585 isl_space *dim;
4586 isl_set *res;
4588 if (!empty || !min_expr || !cst)
4589 goto error;
4591 n_in = isl_set_dim(empty, isl_dim_set);
4592 dim = isl_set_get_space(empty);
4593 dim = isl_space_drop_dims(dim, isl_dim_set, n_in - 1, 1);
4594 res = isl_set_empty(dim);
4596 for (i = 0; i < empty->n; ++i) {
4597 isl_set *set;
4599 set = isl_set_from_basic_set(isl_basic_set_copy(empty->p[i]));
4600 if (need_split_basic_set(empty->p[i], cst))
4601 set = isl_set_intersect(set, isl_set_copy(min_expr));
4602 set = isl_set_remove_dims(set, isl_dim_set, n_in - 1, 1);
4604 res = isl_set_union_disjoint(res, set);
4607 isl_set_free(empty);
4608 isl_set_free(min_expr);
4609 isl_mat_free(cst);
4610 return res;
4611 error:
4612 isl_set_free(empty);
4613 isl_set_free(min_expr);
4614 isl_mat_free(cst);
4615 return NULL;
4618 /* Given a map of which the last input variable is the minimum
4619 * of the bounds in "cst", split each basic set in the set
4620 * in pieces where one of the bounds is (strictly) smaller than the others.
4621 * This subdivision is given in "min_expr".
4622 * The variable is subsequently projected out.
4624 * The implementation is essentially the same as that of "split".
4626 static __isl_give isl_map *split_domain(__isl_take isl_map *opt,
4627 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
4629 int n_in;
4630 int i;
4631 isl_space *dim;
4632 isl_map *res;
4634 if (!opt || !min_expr || !cst)
4635 goto error;
4637 n_in = isl_map_dim(opt, isl_dim_in);
4638 dim = isl_map_get_space(opt);
4639 dim = isl_space_drop_dims(dim, isl_dim_in, n_in - 1, 1);
4640 res = isl_map_empty(dim);
4642 for (i = 0; i < opt->n; ++i) {
4643 isl_map *map;
4645 map = isl_map_from_basic_map(isl_basic_map_copy(opt->p[i]));
4646 if (need_split_basic_map(opt->p[i], cst))
4647 map = isl_map_intersect_domain(map,
4648 isl_set_copy(min_expr));
4649 map = isl_map_remove_dims(map, isl_dim_in, n_in - 1, 1);
4651 res = isl_map_union_disjoint(res, map);
4654 isl_map_free(opt);
4655 isl_set_free(min_expr);
4656 isl_mat_free(cst);
4657 return res;
4658 error:
4659 isl_map_free(opt);
4660 isl_set_free(min_expr);
4661 isl_mat_free(cst);
4662 return NULL;
4665 static __isl_give isl_map *basic_map_partial_lexopt(
4666 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4667 __isl_give isl_set **empty, int max);
4669 /* This function is called from basic_map_partial_lexopt_symm.
4670 * The last variable of "bmap" and "dom" corresponds to the minimum
4671 * of the bounds in "cst". "map_space" is the space of the original
4672 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
4673 * is the space of the original domain.
4675 * We recursively call basic_map_partial_lexopt and then plug in
4676 * the definition of the minimum in the result.
4678 static __isl_give isl_map *basic_map_partial_lexopt_symm_core(
4679 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4680 __isl_give isl_set **empty, int max, __isl_take isl_mat *cst,
4681 __isl_take isl_space *map_space, __isl_take isl_space *set_space)
4683 isl_map *opt;
4684 isl_set *min_expr;
4686 min_expr = set_minimum(isl_basic_set_get_space(dom), isl_mat_copy(cst));
4688 opt = basic_map_partial_lexopt(bmap, dom, empty, max);
4690 if (empty) {
4691 *empty = split(*empty,
4692 isl_set_copy(min_expr), isl_mat_copy(cst));
4693 *empty = isl_set_reset_space(*empty, set_space);
4696 opt = split_domain(opt, min_expr, cst);
4697 opt = isl_map_reset_space(opt, map_space);
4699 return opt;
4702 /* Extract a domain from "bmap" for the purpose of computing
4703 * a lexicographic optimum.
4705 * This function is only called when the caller wants to compute a full
4706 * lexicographic optimum, i.e., without specifying a domain. In this case,
4707 * the caller is not interested in the part of the domain space where
4708 * there is no solution and the domain can be initialized to those constraints
4709 * of "bmap" that only involve the parameters and the input dimensions.
4710 * This relieves the parametric programming engine from detecting those
4711 * inequalities and transferring them to the context. More importantly,
4712 * it ensures that those inequalities are transferred first and not
4713 * intermixed with inequalities that actually split the domain.
4715 * If the caller does not require the absence of existentially quantified
4716 * variables in the result (i.e., if ISL_OPT_QE is not set in "flags"),
4717 * then the actual domain of "bmap" can be used. This ensures that
4718 * the domain does not need to be split at all just to separate out
4719 * pieces of the domain that do not have a solution from piece that do.
4720 * This domain cannot be used in general because it may involve
4721 * (unknown) existentially quantified variables which will then also
4722 * appear in the solution.
4724 static __isl_give isl_basic_set *extract_domain(__isl_keep isl_basic_map *bmap,
4725 unsigned flags)
4727 int n_div;
4728 int n_out;
4730 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4731 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4732 bmap = isl_basic_map_copy(bmap);
4733 if (ISL_FL_ISSET(flags, ISL_OPT_QE)) {
4734 bmap = isl_basic_map_drop_constraints_involving_dims(bmap,
4735 isl_dim_div, 0, n_div);
4736 bmap = isl_basic_map_drop_constraints_involving_dims(bmap,
4737 isl_dim_out, 0, n_out);
4739 return isl_basic_map_domain(bmap);
4742 #undef TYPE
4743 #define TYPE isl_map
4744 #undef SUFFIX
4745 #define SUFFIX
4746 #include "isl_tab_lexopt_templ.c"
4748 struct isl_sol_for {
4749 struct isl_sol sol;
4750 int (*fn)(__isl_take isl_basic_set *dom,
4751 __isl_take isl_aff_list *list, void *user);
4752 void *user;
4755 static void sol_for_free(struct isl_sol_for *sol_for)
4757 if (!sol_for)
4758 return;
4759 if (sol_for->sol.context)
4760 sol_for->sol.context->op->free(sol_for->sol.context);
4761 free(sol_for);
4764 static void sol_for_free_wrap(struct isl_sol *sol)
4766 sol_for_free((struct isl_sol_for *)sol);
4769 /* Add the solution identified by the tableau and the context tableau.
4771 * See documentation of sol_add for more details.
4773 * Instead of constructing a basic map, this function calls a user
4774 * defined function with the current context as a basic set and
4775 * a list of affine expressions representing the relation between
4776 * the input and output. The space over which the affine expressions
4777 * are defined is the same as that of the domain. The number of
4778 * affine expressions in the list is equal to the number of output variables.
4780 static void sol_for_add(struct isl_sol_for *sol,
4781 struct isl_basic_set *dom, struct isl_mat *M)
4783 int i;
4784 isl_ctx *ctx;
4785 isl_local_space *ls;
4786 isl_aff *aff;
4787 isl_aff_list *list;
4789 if (sol->sol.error || !dom || !M)
4790 goto error;
4792 ctx = isl_basic_set_get_ctx(dom);
4793 ls = isl_basic_set_get_local_space(dom);
4794 list = isl_aff_list_alloc(ctx, M->n_row - 1);
4795 for (i = 1; i < M->n_row; ++i) {
4796 aff = isl_aff_alloc(isl_local_space_copy(ls));
4797 if (aff) {
4798 isl_int_set(aff->v->el[0], M->row[0][0]);
4799 isl_seq_cpy(aff->v->el + 1, M->row[i], M->n_col);
4801 aff = isl_aff_normalize(aff);
4802 list = isl_aff_list_add(list, aff);
4804 isl_local_space_free(ls);
4806 dom = isl_basic_set_finalize(dom);
4808 if (sol->fn(isl_basic_set_copy(dom), list, sol->user) < 0)
4809 goto error;
4811 isl_basic_set_free(dom);
4812 isl_mat_free(M);
4813 return;
4814 error:
4815 isl_basic_set_free(dom);
4816 isl_mat_free(M);
4817 sol->sol.error = 1;
4820 static void sol_for_add_wrap(struct isl_sol *sol,
4821 struct isl_basic_set *dom, struct isl_mat *M)
4823 sol_for_add((struct isl_sol_for *)sol, dom, M);
4826 static struct isl_sol_for *sol_for_init(struct isl_basic_map *bmap, int max,
4827 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4828 void *user),
4829 void *user)
4831 struct isl_sol_for *sol_for = NULL;
4832 isl_space *dom_dim;
4833 struct isl_basic_set *dom = NULL;
4835 sol_for = isl_calloc_type(bmap->ctx, struct isl_sol_for);
4836 if (!sol_for)
4837 goto error;
4839 dom_dim = isl_space_domain(isl_space_copy(bmap->dim));
4840 dom = isl_basic_set_universe(dom_dim);
4842 sol_for->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
4843 sol_for->sol.dec_level.callback.run = &sol_dec_level_wrap;
4844 sol_for->sol.dec_level.sol = &sol_for->sol;
4845 sol_for->fn = fn;
4846 sol_for->user = user;
4847 sol_for->sol.max = max;
4848 sol_for->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
4849 sol_for->sol.add = &sol_for_add_wrap;
4850 sol_for->sol.add_empty = NULL;
4851 sol_for->sol.free = &sol_for_free_wrap;
4853 sol_for->sol.context = isl_context_alloc(dom);
4854 if (!sol_for->sol.context)
4855 goto error;
4857 isl_basic_set_free(dom);
4858 return sol_for;
4859 error:
4860 isl_basic_set_free(dom);
4861 sol_for_free(sol_for);
4862 return NULL;
4865 static void sol_for_find_solutions(struct isl_sol_for *sol_for,
4866 struct isl_tab *tab)
4868 find_solutions_main(&sol_for->sol, tab);
4871 int isl_basic_map_foreach_lexopt(__isl_keep isl_basic_map *bmap, int max,
4872 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4873 void *user),
4874 void *user)
4876 struct isl_sol_for *sol_for = NULL;
4878 bmap = isl_basic_map_copy(bmap);
4879 bmap = isl_basic_map_detect_equalities(bmap);
4880 if (!bmap)
4881 return -1;
4883 sol_for = sol_for_init(bmap, max, fn, user);
4884 if (!sol_for)
4885 goto error;
4887 if (isl_basic_map_plain_is_empty(bmap))
4888 /* nothing */;
4889 else {
4890 struct isl_tab *tab;
4891 struct isl_context *context = sol_for->sol.context;
4892 tab = tab_for_lexmin(bmap,
4893 context->op->peek_basic_set(context), 1, max);
4894 tab = context->op->detect_nonnegative_parameters(context, tab);
4895 sol_for_find_solutions(sol_for, tab);
4896 if (sol_for->sol.error)
4897 goto error;
4900 sol_free(&sol_for->sol);
4901 isl_basic_map_free(bmap);
4902 return 0;
4903 error:
4904 sol_free(&sol_for->sol);
4905 isl_basic_map_free(bmap);
4906 return -1;
4909 int isl_basic_set_foreach_lexopt(__isl_keep isl_basic_set *bset, int max,
4910 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4911 void *user),
4912 void *user)
4914 return isl_basic_map_foreach_lexopt(bset, max, fn, user);
4917 /* Check if the given sequence of len variables starting at pos
4918 * represents a trivial (i.e., zero) solution.
4919 * The variables are assumed to be non-negative and to come in pairs,
4920 * with each pair representing a variable of unrestricted sign.
4921 * The solution is trivial if each such pair in the sequence consists
4922 * of two identical values, meaning that the variable being represented
4923 * has value zero.
4925 static int region_is_trivial(struct isl_tab *tab, int pos, int len)
4927 int i;
4929 if (len == 0)
4930 return 0;
4932 for (i = 0; i < len; i += 2) {
4933 int neg_row;
4934 int pos_row;
4936 neg_row = tab->var[pos + i].is_row ?
4937 tab->var[pos + i].index : -1;
4938 pos_row = tab->var[pos + i + 1].is_row ?
4939 tab->var[pos + i + 1].index : -1;
4941 if ((neg_row < 0 ||
4942 isl_int_is_zero(tab->mat->row[neg_row][1])) &&
4943 (pos_row < 0 ||
4944 isl_int_is_zero(tab->mat->row[pos_row][1])))
4945 continue;
4947 if (neg_row < 0 || pos_row < 0)
4948 return 0;
4949 if (isl_int_ne(tab->mat->row[neg_row][1],
4950 tab->mat->row[pos_row][1]))
4951 return 0;
4954 return 1;
4957 /* Return the index of the first trivial region or -1 if all regions
4958 * are non-trivial.
4960 static int first_trivial_region(struct isl_tab *tab,
4961 int n_region, struct isl_region *region)
4963 int i;
4965 for (i = 0; i < n_region; ++i) {
4966 if (region_is_trivial(tab, region[i].pos, region[i].len))
4967 return i;
4970 return -1;
4973 /* Check if the solution is optimal, i.e., whether the first
4974 * n_op entries are zero.
4976 static int is_optimal(__isl_keep isl_vec *sol, int n_op)
4978 int i;
4980 for (i = 0; i < n_op; ++i)
4981 if (!isl_int_is_zero(sol->el[1 + i]))
4982 return 0;
4983 return 1;
4986 /* Add constraints to "tab" that ensure that any solution is significantly
4987 * better than that represented by "sol". That is, find the first
4988 * relevant (within first n_op) non-zero coefficient and force it (along
4989 * with all previous coefficients) to be zero.
4990 * If the solution is already optimal (all relevant coefficients are zero),
4991 * then just mark the table as empty.
4993 * This function assumes that at least 2 * n_op more rows and at least
4994 * 2 * n_op more elements in the constraint array are available in the tableau.
4996 static int force_better_solution(struct isl_tab *tab,
4997 __isl_keep isl_vec *sol, int n_op)
4999 int i;
5000 isl_ctx *ctx;
5001 isl_vec *v = NULL;
5003 if (!sol)
5004 return -1;
5006 for (i = 0; i < n_op; ++i)
5007 if (!isl_int_is_zero(sol->el[1 + i]))
5008 break;
5010 if (i == n_op) {
5011 if (isl_tab_mark_empty(tab) < 0)
5012 return -1;
5013 return 0;
5016 ctx = isl_vec_get_ctx(sol);
5017 v = isl_vec_alloc(ctx, 1 + tab->n_var);
5018 if (!v)
5019 return -1;
5021 for (; i >= 0; --i) {
5022 v = isl_vec_clr(v);
5023 isl_int_set_si(v->el[1 + i], -1);
5024 if (add_lexmin_eq(tab, v->el) < 0)
5025 goto error;
5028 isl_vec_free(v);
5029 return 0;
5030 error:
5031 isl_vec_free(v);
5032 return -1;
5035 struct isl_trivial {
5036 int update;
5037 int region;
5038 int side;
5039 struct isl_tab_undo *snap;
5042 /* Return the lexicographically smallest non-trivial solution of the
5043 * given ILP problem.
5045 * All variables are assumed to be non-negative.
5047 * n_op is the number of initial coordinates to optimize.
5048 * That is, once a solution has been found, we will only continue looking
5049 * for solution that result in significantly better values for those
5050 * initial coordinates. That is, we only continue looking for solutions
5051 * that increase the number of initial zeros in this sequence.
5053 * A solution is non-trivial, if it is non-trivial on each of the
5054 * specified regions. Each region represents a sequence of pairs
5055 * of variables. A solution is non-trivial on such a region if
5056 * at least one of these pairs consists of different values, i.e.,
5057 * such that the non-negative variable represented by the pair is non-zero.
5059 * Whenever a conflict is encountered, all constraints involved are
5060 * reported to the caller through a call to "conflict".
5062 * We perform a simple branch-and-bound backtracking search.
5063 * Each level in the search represents initially trivial region that is forced
5064 * to be non-trivial.
5065 * At each level we consider n cases, where n is the length of the region.
5066 * In terms of the n/2 variables of unrestricted signs being encoded by
5067 * the region, we consider the cases
5068 * x_0 >= 1
5069 * x_0 <= -1
5070 * x_0 = 0 and x_1 >= 1
5071 * x_0 = 0 and x_1 <= -1
5072 * x_0 = 0 and x_1 = 0 and x_2 >= 1
5073 * x_0 = 0 and x_1 = 0 and x_2 <= -1
5074 * ...
5075 * The cases are considered in this order, assuming that each pair
5076 * x_i_a x_i_b represents the value x_i_b - x_i_a.
5077 * That is, x_0 >= 1 is enforced by adding the constraint
5078 * x_0_b - x_0_a >= 1
5080 __isl_give isl_vec *isl_tab_basic_set_non_trivial_lexmin(
5081 __isl_take isl_basic_set *bset, int n_op, int n_region,
5082 struct isl_region *region,
5083 int (*conflict)(int con, void *user), void *user)
5085 int i, j;
5086 int r;
5087 isl_ctx *ctx;
5088 isl_vec *v = NULL;
5089 isl_vec *sol = NULL;
5090 struct isl_tab *tab;
5091 struct isl_trivial *triv = NULL;
5092 int level, init;
5094 if (!bset)
5095 return NULL;
5097 ctx = isl_basic_set_get_ctx(bset);
5098 sol = isl_vec_alloc(ctx, 0);
5100 tab = tab_for_lexmin(bset, NULL, 0, 0);
5101 if (!tab)
5102 goto error;
5103 tab->conflict = conflict;
5104 tab->conflict_user = user;
5106 v = isl_vec_alloc(ctx, 1 + tab->n_var);
5107 triv = isl_calloc_array(ctx, struct isl_trivial, n_region);
5108 if (!v || (n_region && !triv))
5109 goto error;
5111 level = 0;
5112 init = 1;
5114 while (level >= 0) {
5115 int side, base;
5117 if (init) {
5118 tab = cut_to_integer_lexmin(tab, CUT_ONE);
5119 if (!tab)
5120 goto error;
5121 if (tab->empty)
5122 goto backtrack;
5123 r = first_trivial_region(tab, n_region, region);
5124 if (r < 0) {
5125 for (i = 0; i < level; ++i)
5126 triv[i].update = 1;
5127 isl_vec_free(sol);
5128 sol = isl_tab_get_sample_value(tab);
5129 if (!sol)
5130 goto error;
5131 if (is_optimal(sol, n_op))
5132 break;
5133 goto backtrack;
5135 if (level >= n_region)
5136 isl_die(ctx, isl_error_internal,
5137 "nesting level too deep", goto error);
5138 if (isl_tab_extend_cons(tab,
5139 2 * region[r].len + 2 * n_op) < 0)
5140 goto error;
5141 triv[level].region = r;
5142 triv[level].side = 0;
5145 r = triv[level].region;
5146 side = triv[level].side;
5147 base = 2 * (side/2);
5149 if (side >= region[r].len) {
5150 backtrack:
5151 level--;
5152 init = 0;
5153 if (level >= 0)
5154 if (isl_tab_rollback(tab, triv[level].snap) < 0)
5155 goto error;
5156 continue;
5159 if (triv[level].update) {
5160 if (force_better_solution(tab, sol, n_op) < 0)
5161 goto error;
5162 triv[level].update = 0;
5165 if (side == base && base >= 2) {
5166 for (j = base - 2; j < base; ++j) {
5167 v = isl_vec_clr(v);
5168 isl_int_set_si(v->el[1 + region[r].pos + j], 1);
5169 if (add_lexmin_eq(tab, v->el) < 0)
5170 goto error;
5174 triv[level].snap = isl_tab_snap(tab);
5175 if (isl_tab_push_basis(tab) < 0)
5176 goto error;
5178 v = isl_vec_clr(v);
5179 isl_int_set_si(v->el[0], -1);
5180 isl_int_set_si(v->el[1 + region[r].pos + side], -1);
5181 isl_int_set_si(v->el[1 + region[r].pos + (side ^ 1)], 1);
5182 tab = add_lexmin_ineq(tab, v->el);
5184 triv[level].side++;
5185 level++;
5186 init = 1;
5189 free(triv);
5190 isl_vec_free(v);
5191 isl_tab_free(tab);
5192 isl_basic_set_free(bset);
5194 return sol;
5195 error:
5196 free(triv);
5197 isl_vec_free(v);
5198 isl_tab_free(tab);
5199 isl_basic_set_free(bset);
5200 isl_vec_free(sol);
5201 return NULL;
5204 /* Wrapper for a tableau that is used for computing
5205 * the lexicographically smallest rational point of a non-negative set.
5206 * This point is represented by the sample value of "tab",
5207 * unless "tab" is empty.
5209 struct isl_tab_lexmin {
5210 isl_ctx *ctx;
5211 struct isl_tab *tab;
5214 /* Free "tl" and return NULL.
5216 __isl_null isl_tab_lexmin *isl_tab_lexmin_free(__isl_take isl_tab_lexmin *tl)
5218 if (!tl)
5219 return NULL;
5220 isl_ctx_deref(tl->ctx);
5221 isl_tab_free(tl->tab);
5222 free(tl);
5224 return NULL;
5227 /* Construct an isl_tab_lexmin for computing
5228 * the lexicographically smallest rational point in "bset",
5229 * assuming that all variables are non-negative.
5231 __isl_give isl_tab_lexmin *isl_tab_lexmin_from_basic_set(
5232 __isl_take isl_basic_set *bset)
5234 isl_ctx *ctx;
5235 isl_tab_lexmin *tl;
5237 if (!bset)
5238 return NULL;
5240 ctx = isl_basic_set_get_ctx(bset);
5241 tl = isl_calloc_type(ctx, struct isl_tab_lexmin);
5242 if (!tl)
5243 goto error;
5244 tl->ctx = ctx;
5245 isl_ctx_ref(ctx);
5246 tl->tab = tab_for_lexmin(bset, NULL, 0, 0);
5247 isl_basic_set_free(bset);
5248 if (!tl->tab)
5249 return isl_tab_lexmin_free(tl);
5250 return tl;
5251 error:
5252 isl_basic_set_free(bset);
5253 isl_tab_lexmin_free(tl);
5254 return NULL;
5257 /* Return the dimension of the set represented by "tl".
5259 int isl_tab_lexmin_dim(__isl_keep isl_tab_lexmin *tl)
5261 return tl ? tl->tab->n_var : -1;
5264 /* Add the equality with coefficients "eq" to "tl", updating the optimal
5265 * solution if needed.
5266 * The equality is added as two opposite inequality constraints.
5268 __isl_give isl_tab_lexmin *isl_tab_lexmin_add_eq(__isl_take isl_tab_lexmin *tl,
5269 isl_int *eq)
5271 unsigned n_var;
5273 if (!tl || !eq)
5274 return isl_tab_lexmin_free(tl);
5276 if (isl_tab_extend_cons(tl->tab, 2) < 0)
5277 return isl_tab_lexmin_free(tl);
5278 n_var = tl->tab->n_var;
5279 isl_seq_neg(eq, eq, 1 + n_var);
5280 tl->tab = add_lexmin_ineq(tl->tab, eq);
5281 isl_seq_neg(eq, eq, 1 + n_var);
5282 tl->tab = add_lexmin_ineq(tl->tab, eq);
5284 if (!tl->tab)
5285 return isl_tab_lexmin_free(tl);
5287 return tl;
5290 /* Return the lexicographically smallest rational point in the basic set
5291 * from which "tl" was constructed.
5292 * If the original input was empty, then return a zero-length vector.
5294 __isl_give isl_vec *isl_tab_lexmin_get_solution(__isl_keep isl_tab_lexmin *tl)
5296 if (!tl)
5297 return NULL;
5298 if (tl->tab->empty)
5299 return isl_vec_alloc(tl->ctx, 0);
5300 else
5301 return isl_tab_get_sample_value(tl->tab);
5304 /* Return the lexicographically smallest rational point in "bset",
5305 * assuming that all variables are non-negative.
5306 * If "bset" is empty, then return a zero-length vector.
5308 __isl_give isl_vec *isl_tab_basic_set_non_neg_lexmin(
5309 __isl_take isl_basic_set *bset)
5311 isl_tab_lexmin *tl;
5312 isl_vec *sol;
5314 tl = isl_tab_lexmin_from_basic_set(bset);
5315 sol = isl_tab_lexmin_get_solution(tl);
5316 isl_tab_lexmin_free(tl);
5317 return sol;
5320 struct isl_sol_pma {
5321 struct isl_sol sol;
5322 isl_pw_multi_aff *pma;
5323 isl_set *empty;
5326 static void sol_pma_free(struct isl_sol_pma *sol_pma)
5328 if (!sol_pma)
5329 return;
5330 if (sol_pma->sol.context)
5331 sol_pma->sol.context->op->free(sol_pma->sol.context);
5332 isl_pw_multi_aff_free(sol_pma->pma);
5333 isl_set_free(sol_pma->empty);
5334 free(sol_pma);
5337 /* This function is called for parts of the context where there is
5338 * no solution, with "bset" corresponding to the context tableau.
5339 * Simply add the basic set to the set "empty".
5341 static void sol_pma_add_empty(struct isl_sol_pma *sol,
5342 __isl_take isl_basic_set *bset)
5344 if (!bset || !sol->empty)
5345 goto error;
5347 sol->empty = isl_set_grow(sol->empty, 1);
5348 bset = isl_basic_set_simplify(bset);
5349 bset = isl_basic_set_finalize(bset);
5350 sol->empty = isl_set_add_basic_set(sol->empty, bset);
5351 if (!sol->empty)
5352 sol->sol.error = 1;
5353 return;
5354 error:
5355 isl_basic_set_free(bset);
5356 sol->sol.error = 1;
5359 /* Set the affine expressions in "ma" according to the rows in "M", which
5360 * are defined over the local space "ls".
5361 * The matrix "M" may have extra (zero) columns beyond the number
5362 * of variables in "ls".
5364 static __isl_give isl_multi_aff *set_from_affine_matrix(
5365 __isl_take isl_multi_aff *ma, __isl_take isl_local_space *ls,
5366 __isl_take isl_mat *M)
5368 int i, dim;
5369 isl_aff *aff;
5371 if (!ma || !ls || !M)
5372 goto error;
5374 dim = isl_local_space_dim(ls, isl_dim_all);
5375 for (i = 1; i < M->n_row; ++i) {
5376 aff = isl_aff_alloc(isl_local_space_copy(ls));
5377 if (aff) {
5378 isl_int_set(aff->v->el[0], M->row[0][0]);
5379 isl_seq_cpy(aff->v->el + 1, M->row[i], 1 + dim);
5381 aff = isl_aff_normalize(aff);
5382 ma = isl_multi_aff_set_aff(ma, i - 1, aff);
5384 isl_local_space_free(ls);
5385 isl_mat_free(M);
5387 return ma;
5388 error:
5389 isl_local_space_free(ls);
5390 isl_mat_free(M);
5391 isl_multi_aff_free(ma);
5392 return NULL;
5395 /* Given a basic map "dom" that represents the context and an affine
5396 * matrix "M" that maps the dimensions of the context to the
5397 * output variables, construct an isl_pw_multi_aff with a single
5398 * cell corresponding to "dom" and affine expressions copied from "M".
5400 * Note that the description of the initial context may have involved
5401 * existentially quantified variables, in which case they also appear
5402 * in "dom". These need to be removed before creating the affine
5403 * expression because an affine expression cannot be defined in terms
5404 * Since newly added integer divisions are inserted before these
5405 * existentially quantified variables, they are still in the final
5406 * positions and the corresponding final columns "M" are zero
5407 * because align_context_divs adds the existentially quantified
5408 * variables of the context to the main tableau without any constraints and
5409 * any equality constraints that are added later on can only serve
5410 * to eliminate these existentially quantified variables.
5412 static void sol_pma_add(struct isl_sol_pma *sol,
5413 __isl_take isl_basic_set *dom, __isl_take isl_mat *M)
5415 isl_local_space *ls;
5416 isl_multi_aff *maff;
5417 isl_pw_multi_aff *pma;
5418 int n_div, n_known;
5420 n_div = isl_basic_set_dim(dom, isl_dim_div);
5421 n_known = n_div - sol->sol.context->n_unknown;
5423 maff = isl_multi_aff_alloc(isl_pw_multi_aff_get_space(sol->pma));
5424 ls = isl_basic_set_get_local_space(dom);
5425 ls = isl_local_space_drop_dims(ls, isl_dim_div,
5426 n_known, n_div - n_known);
5427 maff = set_from_affine_matrix(maff, ls, M);
5428 dom = isl_basic_set_simplify(dom);
5429 dom = isl_basic_set_finalize(dom);
5430 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(dom), maff);
5431 sol->pma = isl_pw_multi_aff_add_disjoint(sol->pma, pma);
5432 if (!sol->pma)
5433 sol->sol.error = 1;
5436 static void sol_pma_free_wrap(struct isl_sol *sol)
5438 sol_pma_free((struct isl_sol_pma *)sol);
5441 static void sol_pma_add_empty_wrap(struct isl_sol *sol,
5442 __isl_take isl_basic_set *bset)
5444 sol_pma_add_empty((struct isl_sol_pma *)sol, bset);
5447 static void sol_pma_add_wrap(struct isl_sol *sol,
5448 __isl_take isl_basic_set *dom, __isl_take isl_mat *M)
5450 sol_pma_add((struct isl_sol_pma *)sol, dom, M);
5453 /* Construct an isl_sol_pma structure for accumulating the solution.
5454 * If track_empty is set, then we also keep track of the parts
5455 * of the context where there is no solution.
5456 * If max is set, then we are solving a maximization, rather than
5457 * a minimization problem, which means that the variables in the
5458 * tableau have value "M - x" rather than "M + x".
5460 static struct isl_sol *sol_pma_init(__isl_keep isl_basic_map *bmap,
5461 __isl_take isl_basic_set *dom, int track_empty, int max)
5463 struct isl_sol_pma *sol_pma = NULL;
5465 if (!bmap)
5466 goto error;
5468 sol_pma = isl_calloc_type(bmap->ctx, struct isl_sol_pma);
5469 if (!sol_pma)
5470 goto error;
5472 sol_pma->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
5473 sol_pma->sol.dec_level.callback.run = &sol_dec_level_wrap;
5474 sol_pma->sol.dec_level.sol = &sol_pma->sol;
5475 sol_pma->sol.max = max;
5476 sol_pma->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
5477 sol_pma->sol.add = &sol_pma_add_wrap;
5478 sol_pma->sol.add_empty = track_empty ? &sol_pma_add_empty_wrap : NULL;
5479 sol_pma->sol.free = &sol_pma_free_wrap;
5480 sol_pma->pma = isl_pw_multi_aff_empty(isl_basic_map_get_space(bmap));
5481 if (!sol_pma->pma)
5482 goto error;
5484 sol_pma->sol.context = isl_context_alloc(dom);
5485 if (!sol_pma->sol.context)
5486 goto error;
5488 if (track_empty) {
5489 sol_pma->empty = isl_set_alloc_space(isl_basic_set_get_space(dom),
5490 1, ISL_SET_DISJOINT);
5491 if (!sol_pma->empty)
5492 goto error;
5495 isl_basic_set_free(dom);
5496 return &sol_pma->sol;
5497 error:
5498 isl_basic_set_free(dom);
5499 sol_pma_free(sol_pma);
5500 return NULL;
5503 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
5504 * some obvious symmetries.
5506 * We call basic_map_partial_lexopt_base_sol and extract the results.
5508 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_base_pw_multi_aff(
5509 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5510 __isl_give isl_set **empty, int max)
5512 isl_pw_multi_aff *result = NULL;
5513 struct isl_sol *sol;
5514 struct isl_sol_pma *sol_pma;
5516 sol = basic_map_partial_lexopt_base_sol(bmap, dom, empty, max,
5517 &sol_pma_init);
5518 if (!sol)
5519 return NULL;
5520 sol_pma = (struct isl_sol_pma *) sol;
5522 result = isl_pw_multi_aff_copy(sol_pma->pma);
5523 if (empty)
5524 *empty = isl_set_copy(sol_pma->empty);
5525 sol_free(&sol_pma->sol);
5526 return result;
5529 /* Given that the last input variable of "maff" represents the minimum
5530 * of some bounds, check whether we need to plug in the expression
5531 * of the minimum.
5533 * In particular, check if the last input variable appears in any
5534 * of the expressions in "maff".
5536 static int need_substitution(__isl_keep isl_multi_aff *maff)
5538 int i;
5539 unsigned pos;
5541 pos = isl_multi_aff_dim(maff, isl_dim_in) - 1;
5543 for (i = 0; i < maff->n; ++i)
5544 if (isl_aff_involves_dims(maff->p[i], isl_dim_in, pos, 1))
5545 return 1;
5547 return 0;
5550 /* Given a set of upper bounds on the last "input" variable m,
5551 * construct a piecewise affine expression that selects
5552 * the minimal upper bound to m, i.e.,
5553 * divide the space into cells where one
5554 * of the upper bounds is smaller than all the others and select
5555 * this upper bound on that cell.
5557 * In particular, if there are n bounds b_i, then the result
5558 * consists of n cell, each one of the form
5560 * b_i <= b_j for j > i
5561 * b_i < b_j for j < i
5563 * The affine expression on this cell is
5565 * b_i
5567 static __isl_give isl_pw_aff *set_minimum_pa(__isl_take isl_space *space,
5568 __isl_take isl_mat *var)
5570 int i;
5571 isl_aff *aff = NULL;
5572 isl_basic_set *bset = NULL;
5573 isl_pw_aff *paff = NULL;
5574 isl_space *pw_space;
5575 isl_local_space *ls = NULL;
5577 if (!space || !var)
5578 goto error;
5580 ls = isl_local_space_from_space(isl_space_copy(space));
5581 pw_space = isl_space_copy(space);
5582 pw_space = isl_space_from_domain(pw_space);
5583 pw_space = isl_space_add_dims(pw_space, isl_dim_out, 1);
5584 paff = isl_pw_aff_alloc_size(pw_space, var->n_row);
5586 for (i = 0; i < var->n_row; ++i) {
5587 isl_pw_aff *paff_i;
5589 aff = isl_aff_alloc(isl_local_space_copy(ls));
5590 bset = isl_basic_set_alloc_space(isl_space_copy(space), 0,
5591 0, var->n_row - 1);
5592 if (!aff || !bset)
5593 goto error;
5594 isl_int_set_si(aff->v->el[0], 1);
5595 isl_seq_cpy(aff->v->el + 1, var->row[i], var->n_col);
5596 isl_int_set_si(aff->v->el[1 + var->n_col], 0);
5597 bset = select_minimum(bset, var, i);
5598 paff_i = isl_pw_aff_alloc(isl_set_from_basic_set(bset), aff);
5599 paff = isl_pw_aff_add_disjoint(paff, paff_i);
5602 isl_local_space_free(ls);
5603 isl_space_free(space);
5604 isl_mat_free(var);
5605 return paff;
5606 error:
5607 isl_aff_free(aff);
5608 isl_basic_set_free(bset);
5609 isl_pw_aff_free(paff);
5610 isl_local_space_free(ls);
5611 isl_space_free(space);
5612 isl_mat_free(var);
5613 return NULL;
5616 /* Given a piecewise multi-affine expression of which the last input variable
5617 * is the minimum of the bounds in "cst", plug in the value of the minimum.
5618 * This minimum expression is given in "min_expr_pa".
5619 * The set "min_expr" contains the same information, but in the form of a set.
5620 * The variable is subsequently projected out.
5622 * The implementation is similar to those of "split" and "split_domain".
5623 * If the variable appears in a given expression, then minimum expression
5624 * is plugged in. Otherwise, if the variable appears in the constraints
5625 * and a split is required, then the domain is split. Otherwise, no split
5626 * is performed.
5628 static __isl_give isl_pw_multi_aff *split_domain_pma(
5629 __isl_take isl_pw_multi_aff *opt, __isl_take isl_pw_aff *min_expr_pa,
5630 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
5632 int n_in;
5633 int i;
5634 isl_space *space;
5635 isl_pw_multi_aff *res;
5637 if (!opt || !min_expr || !cst)
5638 goto error;
5640 n_in = isl_pw_multi_aff_dim(opt, isl_dim_in);
5641 space = isl_pw_multi_aff_get_space(opt);
5642 space = isl_space_drop_dims(space, isl_dim_in, n_in - 1, 1);
5643 res = isl_pw_multi_aff_empty(space);
5645 for (i = 0; i < opt->n; ++i) {
5646 isl_pw_multi_aff *pma;
5648 pma = isl_pw_multi_aff_alloc(isl_set_copy(opt->p[i].set),
5649 isl_multi_aff_copy(opt->p[i].maff));
5650 if (need_substitution(opt->p[i].maff))
5651 pma = isl_pw_multi_aff_substitute(pma,
5652 isl_dim_in, n_in - 1, min_expr_pa);
5653 else if (need_split_set(opt->p[i].set, cst))
5654 pma = isl_pw_multi_aff_intersect_domain(pma,
5655 isl_set_copy(min_expr));
5656 pma = isl_pw_multi_aff_project_out(pma,
5657 isl_dim_in, n_in - 1, 1);
5659 res = isl_pw_multi_aff_add_disjoint(res, pma);
5662 isl_pw_multi_aff_free(opt);
5663 isl_pw_aff_free(min_expr_pa);
5664 isl_set_free(min_expr);
5665 isl_mat_free(cst);
5666 return res;
5667 error:
5668 isl_pw_multi_aff_free(opt);
5669 isl_pw_aff_free(min_expr_pa);
5670 isl_set_free(min_expr);
5671 isl_mat_free(cst);
5672 return NULL;
5675 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_pw_multi_aff(
5676 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5677 __isl_give isl_set **empty, int max);
5679 /* This function is called from basic_map_partial_lexopt_symm.
5680 * The last variable of "bmap" and "dom" corresponds to the minimum
5681 * of the bounds in "cst". "map_space" is the space of the original
5682 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
5683 * is the space of the original domain.
5685 * We recursively call basic_map_partial_lexopt and then plug in
5686 * the definition of the minimum in the result.
5688 static __isl_give isl_pw_multi_aff *
5689 basic_map_partial_lexopt_symm_core_pw_multi_aff(
5690 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5691 __isl_give isl_set **empty, int max, __isl_take isl_mat *cst,
5692 __isl_take isl_space *map_space, __isl_take isl_space *set_space)
5694 isl_pw_multi_aff *opt;
5695 isl_pw_aff *min_expr_pa;
5696 isl_set *min_expr;
5698 min_expr = set_minimum(isl_basic_set_get_space(dom), isl_mat_copy(cst));
5699 min_expr_pa = set_minimum_pa(isl_basic_set_get_space(dom),
5700 isl_mat_copy(cst));
5702 opt = basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, max);
5704 if (empty) {
5705 *empty = split(*empty,
5706 isl_set_copy(min_expr), isl_mat_copy(cst));
5707 *empty = isl_set_reset_space(*empty, set_space);
5710 opt = split_domain_pma(opt, min_expr_pa, min_expr, cst);
5711 opt = isl_pw_multi_aff_reset_space(opt, map_space);
5713 return opt;
5716 #undef TYPE
5717 #define TYPE isl_pw_multi_aff
5718 #undef SUFFIX
5719 #define SUFFIX _pw_multi_aff
5720 #include "isl_tab_lexopt_templ.c"