Handle error conditions returned by level_before in isl_flow
[isl.git] / isl_tab_pip.c
blob4c0e5fa4a8e756eb70880d5bd69f3cc0548adfbc
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2016 Sven Verdoolaege
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
14 #include <isl_ctx_private.h>
15 #include "isl_map_private.h"
16 #include <isl_seq.h>
17 #include "isl_tab.h"
18 #include "isl_sample.h"
19 #include <isl_mat_private.h>
20 #include <isl_vec_private.h>
21 #include <isl_aff_private.h>
22 #include <isl_constraint_private.h>
23 #include <isl_options_private.h>
24 #include <isl_config.h>
26 #include <bset_to_bmap.c>
29 * The implementation of parametric integer linear programming in this file
30 * was inspired by the paper "Parametric Integer Programming" and the
31 * report "Solving systems of affine (in)equalities" by Paul Feautrier
32 * (and others).
34 * The strategy used for obtaining a feasible solution is different
35 * from the one used in isl_tab.c. In particular, in isl_tab.c,
36 * upon finding a constraint that is not yet satisfied, we pivot
37 * in a row that increases the constant term of the row holding the
38 * constraint, making sure the sample solution remains feasible
39 * for all the constraints it already satisfied.
40 * Here, we always pivot in the row holding the constraint,
41 * choosing a column that induces the lexicographically smallest
42 * increment to the sample solution.
44 * By starting out from a sample value that is lexicographically
45 * smaller than any integer point in the problem space, the first
46 * feasible integer sample point we find will also be the lexicographically
47 * smallest. If all variables can be assumed to be non-negative,
48 * then the initial sample value may be chosen equal to zero.
49 * However, we will not make this assumption. Instead, we apply
50 * the "big parameter" trick. Any variable x is then not directly
51 * used in the tableau, but instead it is represented by another
52 * variable x' = M + x, where M is an arbitrarily large (positive)
53 * value. x' is therefore always non-negative, whatever the value of x.
54 * Taking as initial sample value x' = 0 corresponds to x = -M,
55 * which is always smaller than any possible value of x.
57 * The big parameter trick is used in the main tableau and
58 * also in the context tableau if isl_context_lex is used.
59 * In this case, each tableaus has its own big parameter.
60 * Before doing any real work, we check if all the parameters
61 * happen to be non-negative. If so, we drop the column corresponding
62 * to M from the initial context tableau.
63 * If isl_context_gbr is used, then the big parameter trick is only
64 * used in the main tableau.
67 struct isl_context;
68 struct isl_context_op {
69 /* detect nonnegative parameters in context and mark them in tab */
70 struct isl_tab *(*detect_nonnegative_parameters)(
71 struct isl_context *context, struct isl_tab *tab);
72 /* return temporary reference to basic set representation of context */
73 struct isl_basic_set *(*peek_basic_set)(struct isl_context *context);
74 /* return temporary reference to tableau representation of context */
75 struct isl_tab *(*peek_tab)(struct isl_context *context);
76 /* add equality; check is 1 if eq may not be valid;
77 * update is 1 if we may want to call ineq_sign on context later.
79 void (*add_eq)(struct isl_context *context, isl_int *eq,
80 int check, int update);
81 /* add inequality; check is 1 if ineq may not be valid;
82 * update is 1 if we may want to call ineq_sign on context later.
84 void (*add_ineq)(struct isl_context *context, isl_int *ineq,
85 int check, int update);
86 /* check sign of ineq based on previous information.
87 * strict is 1 if saturation should be treated as a positive sign.
89 enum isl_tab_row_sign (*ineq_sign)(struct isl_context *context,
90 isl_int *ineq, int strict);
91 /* check if inequality maintains feasibility */
92 int (*test_ineq)(struct isl_context *context, isl_int *ineq);
93 /* return index of a div that corresponds to "div" */
94 int (*get_div)(struct isl_context *context, struct isl_tab *tab,
95 struct isl_vec *div);
96 /* insert div "div" to context at "pos" and return non-negativity */
97 isl_bool (*insert_div)(struct isl_context *context, int pos,
98 __isl_keep isl_vec *div);
99 int (*detect_equalities)(struct isl_context *context,
100 struct isl_tab *tab);
101 /* return row index of "best" split */
102 int (*best_split)(struct isl_context *context, struct isl_tab *tab);
103 /* check if context has already been determined to be empty */
104 int (*is_empty)(struct isl_context *context);
105 /* check if context is still usable */
106 int (*is_ok)(struct isl_context *context);
107 /* save a copy/snapshot of context */
108 void *(*save)(struct isl_context *context);
109 /* restore saved context */
110 void (*restore)(struct isl_context *context, void *);
111 /* discard saved context */
112 void (*discard)(void *);
113 /* invalidate context */
114 void (*invalidate)(struct isl_context *context);
115 /* free context */
116 __isl_null struct isl_context *(*free)(struct isl_context *context);
119 /* Shared parts of context representation.
121 * "n_unknown" is the number of final unknown integer divisions
122 * in the input domain.
124 struct isl_context {
125 struct isl_context_op *op;
126 int n_unknown;
129 struct isl_context_lex {
130 struct isl_context context;
131 struct isl_tab *tab;
134 /* A stack (linked list) of solutions of subtrees of the search space.
136 * "M" describes the solution in terms of the dimensions of "dom".
137 * The number of columns of "M" is one more than the total number
138 * of dimensions of "dom".
140 * If "M" is NULL, then there is no solution on "dom".
142 struct isl_partial_sol {
143 int level;
144 struct isl_basic_set *dom;
145 struct isl_mat *M;
147 struct isl_partial_sol *next;
150 struct isl_sol;
151 struct isl_sol_callback {
152 struct isl_tab_callback callback;
153 struct isl_sol *sol;
156 /* isl_sol is an interface for constructing a solution to
157 * a parametric integer linear programming problem.
158 * Every time the algorithm reaches a state where a solution
159 * can be read off from the tableau (including cases where the tableau
160 * is empty), the function "add" is called on the isl_sol passed
161 * to find_solutions_main.
163 * The context tableau is owned by isl_sol and is updated incrementally.
165 * There are currently two implementations of this interface,
166 * isl_sol_map, which simply collects the solutions in an isl_map
167 * and (optionally) the parts of the context where there is no solution
168 * in an isl_set, and
169 * isl_sol_for, which calls a user-defined function for each part of
170 * the solution.
172 struct isl_sol {
173 int error;
174 int rational;
175 int level;
176 int max;
177 int n_out;
178 struct isl_context *context;
179 struct isl_partial_sol *partial;
180 void (*add)(struct isl_sol *sol,
181 struct isl_basic_set *dom, struct isl_mat *M);
182 void (*add_empty)(struct isl_sol *sol, struct isl_basic_set *bset);
183 void (*free)(struct isl_sol *sol);
184 struct isl_sol_callback dec_level;
187 static void sol_free(struct isl_sol *sol)
189 struct isl_partial_sol *partial, *next;
190 if (!sol)
191 return;
192 for (partial = sol->partial; partial; partial = next) {
193 next = partial->next;
194 isl_basic_set_free(partial->dom);
195 isl_mat_free(partial->M);
196 free(partial);
198 sol->free(sol);
201 /* Push a partial solution represented by a domain and mapping M
202 * onto the stack of partial solutions.
204 static void sol_push_sol(struct isl_sol *sol,
205 struct isl_basic_set *dom, struct isl_mat *M)
207 struct isl_partial_sol *partial;
209 if (sol->error || !dom)
210 goto error;
212 partial = isl_alloc_type(dom->ctx, struct isl_partial_sol);
213 if (!partial)
214 goto error;
216 partial->level = sol->level;
217 partial->dom = dom;
218 partial->M = M;
219 partial->next = sol->partial;
221 sol->partial = partial;
223 return;
224 error:
225 isl_basic_set_free(dom);
226 isl_mat_free(M);
227 sol->error = 1;
230 /* Pop one partial solution from the partial solution stack and
231 * pass it on to sol->add or sol->add_empty.
233 static void sol_pop_one(struct isl_sol *sol)
235 struct isl_partial_sol *partial;
237 partial = sol->partial;
238 sol->partial = partial->next;
240 if (partial->M)
241 sol->add(sol, partial->dom, partial->M);
242 else
243 sol->add_empty(sol, partial->dom);
244 free(partial);
247 /* Return a fresh copy of the domain represented by the context tableau.
249 static struct isl_basic_set *sol_domain(struct isl_sol *sol)
251 struct isl_basic_set *bset;
253 if (sol->error)
254 return NULL;
256 bset = isl_basic_set_dup(sol->context->op->peek_basic_set(sol->context));
257 bset = isl_basic_set_update_from_tab(bset,
258 sol->context->op->peek_tab(sol->context));
260 return bset;
263 /* Check whether two partial solutions have the same mapping, where n_div
264 * is the number of divs that the two partial solutions have in common.
266 static int same_solution(struct isl_partial_sol *s1, struct isl_partial_sol *s2,
267 unsigned n_div)
269 int i;
270 unsigned dim;
272 if (!s1->M != !s2->M)
273 return 0;
274 if (!s1->M)
275 return 1;
277 dim = isl_basic_set_total_dim(s1->dom) - s1->dom->n_div;
279 for (i = 0; i < s1->M->n_row; ++i) {
280 if (isl_seq_first_non_zero(s1->M->row[i]+1+dim+n_div,
281 s1->M->n_col-1-dim-n_div) != -1)
282 return 0;
283 if (isl_seq_first_non_zero(s2->M->row[i]+1+dim+n_div,
284 s2->M->n_col-1-dim-n_div) != -1)
285 return 0;
286 if (!isl_seq_eq(s1->M->row[i], s2->M->row[i], 1+dim+n_div))
287 return 0;
289 return 1;
292 /* Pop all solutions from the partial solution stack that were pushed onto
293 * the stack at levels that are deeper than the current level.
294 * If the two topmost elements on the stack have the same level
295 * and represent the same solution, then their domains are combined.
296 * This combined domain is the same as the current context domain
297 * as sol_pop is called each time we move back to a higher level.
298 * If the outer level (0) has been reached, then all partial solutions
299 * at the current level are also popped off.
301 static void sol_pop(struct isl_sol *sol)
303 struct isl_partial_sol *partial;
304 unsigned n_div;
306 if (sol->error)
307 return;
309 partial = sol->partial;
310 if (!partial)
311 return;
313 if (partial->level == 0 && sol->level == 0) {
314 for (partial = sol->partial; partial; partial = sol->partial)
315 sol_pop_one(sol);
316 return;
319 if (partial->level <= sol->level)
320 return;
322 if (partial->next && partial->next->level == partial->level) {
323 n_div = isl_basic_set_dim(
324 sol->context->op->peek_basic_set(sol->context),
325 isl_dim_div);
327 if (!same_solution(partial, partial->next, n_div)) {
328 sol_pop_one(sol);
329 sol_pop_one(sol);
330 } else {
331 struct isl_basic_set *bset;
332 isl_mat *M;
333 unsigned n;
335 n = isl_basic_set_dim(partial->next->dom, isl_dim_div);
336 n -= n_div;
337 bset = sol_domain(sol);
338 isl_basic_set_free(partial->next->dom);
339 partial->next->dom = bset;
340 M = partial->next->M;
341 if (M) {
342 M = isl_mat_drop_cols(M, M->n_col - n, n);
343 partial->next->M = M;
344 if (!M)
345 goto error;
347 partial->next->level = sol->level;
349 if (!bset)
350 goto error;
352 sol->partial = partial->next;
353 isl_basic_set_free(partial->dom);
354 isl_mat_free(partial->M);
355 free(partial);
357 } else
358 sol_pop_one(sol);
360 if (sol->level == 0) {
361 for (partial = sol->partial; partial; partial = sol->partial)
362 sol_pop_one(sol);
363 return;
366 if (0)
367 error: sol->error = 1;
370 static void sol_dec_level(struct isl_sol *sol)
372 if (sol->error)
373 return;
375 sol->level--;
377 sol_pop(sol);
380 static int sol_dec_level_wrap(struct isl_tab_callback *cb)
382 struct isl_sol_callback *callback = (struct isl_sol_callback *)cb;
384 sol_dec_level(callback->sol);
386 return callback->sol->error ? -1 : 0;
389 /* Move down to next level and push callback onto context tableau
390 * to decrease the level again when it gets rolled back across
391 * the current state. That is, dec_level will be called with
392 * the context tableau in the same state as it is when inc_level
393 * is called.
395 static void sol_inc_level(struct isl_sol *sol)
397 struct isl_tab *tab;
399 if (sol->error)
400 return;
402 sol->level++;
403 tab = sol->context->op->peek_tab(sol->context);
404 if (isl_tab_push_callback(tab, &sol->dec_level.callback) < 0)
405 sol->error = 1;
408 static void scale_rows(struct isl_mat *mat, isl_int m, int n_row)
410 int i;
412 if (isl_int_is_one(m))
413 return;
415 for (i = 0; i < n_row; ++i)
416 isl_seq_scale(mat->row[i], mat->row[i], m, mat->n_col);
419 /* Add the solution identified by the tableau and the context tableau.
421 * The layout of the variables is as follows.
422 * tab->n_var is equal to the total number of variables in the input
423 * map (including divs that were copied from the context)
424 * + the number of extra divs constructed
425 * Of these, the first tab->n_param and the last tab->n_div variables
426 * correspond to the variables in the context, i.e.,
427 * tab->n_param + tab->n_div = context_tab->n_var
428 * tab->n_param is equal to the number of parameters and input
429 * dimensions in the input map
430 * tab->n_div is equal to the number of divs in the context
432 * If there is no solution, then call add_empty with a basic set
433 * that corresponds to the context tableau. (If add_empty is NULL,
434 * then do nothing).
436 * If there is a solution, then first construct a matrix that maps
437 * all dimensions of the context to the output variables, i.e.,
438 * the output dimensions in the input map.
439 * The divs in the input map (if any) that do not correspond to any
440 * div in the context do not appear in the solution.
441 * The algorithm will make sure that they have an integer value,
442 * but these values themselves are of no interest.
443 * We have to be careful not to drop or rearrange any divs in the
444 * context because that would change the meaning of the matrix.
446 * To extract the value of the output variables, it should be noted
447 * that we always use a big parameter M in the main tableau and so
448 * the variable stored in this tableau is not an output variable x itself, but
449 * x' = M + x (in case of minimization)
450 * or
451 * x' = M - x (in case of maximization)
452 * If x' appears in a column, then its optimal value is zero,
453 * which means that the optimal value of x is an unbounded number
454 * (-M for minimization and M for maximization).
455 * We currently assume that the output dimensions in the original map
456 * are bounded, so this cannot occur.
457 * Similarly, when x' appears in a row, then the coefficient of M in that
458 * row is necessarily 1.
459 * If the row in the tableau represents
460 * d x' = c + d M + e(y)
461 * then, in case of minimization, the corresponding row in the matrix
462 * will be
463 * a c + a e(y)
464 * with a d = m, the (updated) common denominator of the matrix.
465 * In case of maximization, the row will be
466 * -a c - a e(y)
468 static void sol_add(struct isl_sol *sol, struct isl_tab *tab)
470 struct isl_basic_set *bset = NULL;
471 struct isl_mat *mat = NULL;
472 unsigned off;
473 int row;
474 isl_int m;
476 if (sol->error || !tab)
477 goto error;
479 if (tab->empty && !sol->add_empty)
480 return;
481 if (sol->context->op->is_empty(sol->context))
482 return;
484 bset = sol_domain(sol);
486 if (tab->empty) {
487 sol_push_sol(sol, bset, NULL);
488 return;
491 off = 2 + tab->M;
493 mat = isl_mat_alloc(tab->mat->ctx, 1 + sol->n_out,
494 1 + tab->n_param + tab->n_div);
495 if (!mat)
496 goto error;
498 isl_int_init(m);
500 isl_seq_clr(mat->row[0] + 1, mat->n_col - 1);
501 isl_int_set_si(mat->row[0][0], 1);
502 for (row = 0; row < sol->n_out; ++row) {
503 int i = tab->n_param + row;
504 int r, j;
506 isl_seq_clr(mat->row[1 + row], mat->n_col);
507 if (!tab->var[i].is_row) {
508 if (tab->M)
509 isl_die(mat->ctx, isl_error_invalid,
510 "unbounded optimum", goto error2);
511 continue;
514 r = tab->var[i].index;
515 if (tab->M &&
516 isl_int_ne(tab->mat->row[r][2], tab->mat->row[r][0]))
517 isl_die(mat->ctx, isl_error_invalid,
518 "unbounded optimum", goto error2);
519 isl_int_gcd(m, mat->row[0][0], tab->mat->row[r][0]);
520 isl_int_divexact(m, tab->mat->row[r][0], m);
521 scale_rows(mat, m, 1 + row);
522 isl_int_divexact(m, mat->row[0][0], tab->mat->row[r][0]);
523 isl_int_mul(mat->row[1 + row][0], m, tab->mat->row[r][1]);
524 for (j = 0; j < tab->n_param; ++j) {
525 int col;
526 if (tab->var[j].is_row)
527 continue;
528 col = tab->var[j].index;
529 isl_int_mul(mat->row[1 + row][1 + j], m,
530 tab->mat->row[r][off + col]);
532 for (j = 0; j < tab->n_div; ++j) {
533 int col;
534 if (tab->var[tab->n_var - tab->n_div+j].is_row)
535 continue;
536 col = tab->var[tab->n_var - tab->n_div+j].index;
537 isl_int_mul(mat->row[1 + row][1 + tab->n_param + j], m,
538 tab->mat->row[r][off + col]);
540 if (sol->max)
541 isl_seq_neg(mat->row[1 + row], mat->row[1 + row],
542 mat->n_col);
545 isl_int_clear(m);
547 sol_push_sol(sol, bset, mat);
548 return;
549 error2:
550 isl_int_clear(m);
551 error:
552 isl_basic_set_free(bset);
553 isl_mat_free(mat);
554 sol->error = 1;
557 struct isl_sol_map {
558 struct isl_sol sol;
559 struct isl_map *map;
560 struct isl_set *empty;
563 static void sol_map_free(struct isl_sol_map *sol_map)
565 if (!sol_map)
566 return;
567 if (sol_map->sol.context)
568 sol_map->sol.context->op->free(sol_map->sol.context);
569 isl_map_free(sol_map->map);
570 isl_set_free(sol_map->empty);
571 free(sol_map);
574 static void sol_map_free_wrap(struct isl_sol *sol)
576 sol_map_free((struct isl_sol_map *)sol);
579 /* This function is called for parts of the context where there is
580 * no solution, with "bset" corresponding to the context tableau.
581 * Simply add the basic set to the set "empty".
583 static void sol_map_add_empty(struct isl_sol_map *sol,
584 struct isl_basic_set *bset)
586 if (!bset || !sol->empty)
587 goto error;
589 sol->empty = isl_set_grow(sol->empty, 1);
590 bset = isl_basic_set_simplify(bset);
591 bset = isl_basic_set_finalize(bset);
592 sol->empty = isl_set_add_basic_set(sol->empty, isl_basic_set_copy(bset));
593 if (!sol->empty)
594 goto error;
595 isl_basic_set_free(bset);
596 return;
597 error:
598 isl_basic_set_free(bset);
599 sol->sol.error = 1;
602 static void sol_map_add_empty_wrap(struct isl_sol *sol,
603 struct isl_basic_set *bset)
605 sol_map_add_empty((struct isl_sol_map *)sol, bset);
608 /* Given a basic set "dom" that represents the context and an affine
609 * matrix "M" that maps the dimensions of the context to the
610 * output variables, construct a basic map with the same parameters
611 * and divs as the context, the dimensions of the context as input
612 * dimensions and a number of output dimensions that is equal to
613 * the number of output dimensions in the input map.
615 * The constraints and divs of the context are simply copied
616 * from "dom". For each row
617 * x = c + e(y)
618 * an equality
619 * c + e(y) - d x = 0
620 * is added, with d the common denominator of M.
622 static void sol_map_add(struct isl_sol_map *sol,
623 struct isl_basic_set *dom, struct isl_mat *M)
625 int i;
626 struct isl_basic_map *bmap = NULL;
627 unsigned n_eq;
628 unsigned n_ineq;
629 unsigned nparam;
630 unsigned total;
631 unsigned n_div;
632 unsigned n_out;
634 if (sol->sol.error || !dom || !M)
635 goto error;
637 n_out = sol->sol.n_out;
638 n_eq = dom->n_eq + n_out;
639 n_ineq = dom->n_ineq;
640 n_div = dom->n_div;
641 nparam = isl_basic_set_total_dim(dom) - n_div;
642 total = isl_map_dim(sol->map, isl_dim_all);
643 bmap = isl_basic_map_alloc_space(isl_map_get_space(sol->map),
644 n_div, n_eq, 2 * n_div + n_ineq);
645 if (!bmap)
646 goto error;
647 if (sol->sol.rational)
648 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
649 for (i = 0; i < dom->n_div; ++i) {
650 int k = isl_basic_map_alloc_div(bmap);
651 if (k < 0)
652 goto error;
653 isl_seq_cpy(bmap->div[k], dom->div[i], 1 + 1 + nparam);
654 isl_seq_clr(bmap->div[k] + 1 + 1 + nparam, total - nparam);
655 isl_seq_cpy(bmap->div[k] + 1 + 1 + total,
656 dom->div[i] + 1 + 1 + nparam, i);
658 for (i = 0; i < dom->n_eq; ++i) {
659 int k = isl_basic_map_alloc_equality(bmap);
660 if (k < 0)
661 goto error;
662 isl_seq_cpy(bmap->eq[k], dom->eq[i], 1 + nparam);
663 isl_seq_clr(bmap->eq[k] + 1 + nparam, total - nparam);
664 isl_seq_cpy(bmap->eq[k] + 1 + total,
665 dom->eq[i] + 1 + nparam, n_div);
667 for (i = 0; i < dom->n_ineq; ++i) {
668 int k = isl_basic_map_alloc_inequality(bmap);
669 if (k < 0)
670 goto error;
671 isl_seq_cpy(bmap->ineq[k], dom->ineq[i], 1 + nparam);
672 isl_seq_clr(bmap->ineq[k] + 1 + nparam, total - nparam);
673 isl_seq_cpy(bmap->ineq[k] + 1 + total,
674 dom->ineq[i] + 1 + nparam, n_div);
676 for (i = 0; i < M->n_row - 1; ++i) {
677 int k = isl_basic_map_alloc_equality(bmap);
678 if (k < 0)
679 goto error;
680 isl_seq_cpy(bmap->eq[k], M->row[1 + i], 1 + nparam);
681 isl_seq_clr(bmap->eq[k] + 1 + nparam, n_out);
682 isl_int_neg(bmap->eq[k][1 + nparam + i], M->row[0][0]);
683 isl_seq_cpy(bmap->eq[k] + 1 + nparam + n_out,
684 M->row[1 + i] + 1 + nparam, n_div);
686 bmap = isl_basic_map_simplify(bmap);
687 bmap = isl_basic_map_finalize(bmap);
688 sol->map = isl_map_grow(sol->map, 1);
689 sol->map = isl_map_add_basic_map(sol->map, bmap);
690 isl_basic_set_free(dom);
691 isl_mat_free(M);
692 if (!sol->map)
693 sol->sol.error = 1;
694 return;
695 error:
696 isl_basic_set_free(dom);
697 isl_mat_free(M);
698 isl_basic_map_free(bmap);
699 sol->sol.error = 1;
702 static void sol_map_add_wrap(struct isl_sol *sol,
703 struct isl_basic_set *dom, struct isl_mat *M)
705 sol_map_add((struct isl_sol_map *)sol, dom, M);
709 /* Store the "parametric constant" of row "row" of tableau "tab" in "line",
710 * i.e., the constant term and the coefficients of all variables that
711 * appear in the context tableau.
712 * Note that the coefficient of the big parameter M is NOT copied.
713 * The context tableau may not have a big parameter and even when it
714 * does, it is a different big parameter.
716 static void get_row_parameter_line(struct isl_tab *tab, int row, isl_int *line)
718 int i;
719 unsigned off = 2 + tab->M;
721 isl_int_set(line[0], tab->mat->row[row][1]);
722 for (i = 0; i < tab->n_param; ++i) {
723 if (tab->var[i].is_row)
724 isl_int_set_si(line[1 + i], 0);
725 else {
726 int col = tab->var[i].index;
727 isl_int_set(line[1 + i], tab->mat->row[row][off + col]);
730 for (i = 0; i < tab->n_div; ++i) {
731 if (tab->var[tab->n_var - tab->n_div + i].is_row)
732 isl_int_set_si(line[1 + tab->n_param + i], 0);
733 else {
734 int col = tab->var[tab->n_var - tab->n_div + i].index;
735 isl_int_set(line[1 + tab->n_param + i],
736 tab->mat->row[row][off + col]);
741 /* Check if rows "row1" and "row2" have identical "parametric constants",
742 * as explained above.
743 * In this case, we also insist that the coefficients of the big parameter
744 * be the same as the values of the constants will only be the same
745 * if these coefficients are also the same.
747 static int identical_parameter_line(struct isl_tab *tab, int row1, int row2)
749 int i;
750 unsigned off = 2 + tab->M;
752 if (isl_int_ne(tab->mat->row[row1][1], tab->mat->row[row2][1]))
753 return 0;
755 if (tab->M && isl_int_ne(tab->mat->row[row1][2],
756 tab->mat->row[row2][2]))
757 return 0;
759 for (i = 0; i < tab->n_param + tab->n_div; ++i) {
760 int pos = i < tab->n_param ? i :
761 tab->n_var - tab->n_div + i - tab->n_param;
762 int col;
764 if (tab->var[pos].is_row)
765 continue;
766 col = tab->var[pos].index;
767 if (isl_int_ne(tab->mat->row[row1][off + col],
768 tab->mat->row[row2][off + col]))
769 return 0;
771 return 1;
774 /* Return an inequality that expresses that the "parametric constant"
775 * should be non-negative.
776 * This function is only called when the coefficient of the big parameter
777 * is equal to zero.
779 static struct isl_vec *get_row_parameter_ineq(struct isl_tab *tab, int row)
781 struct isl_vec *ineq;
783 ineq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_param + tab->n_div);
784 if (!ineq)
785 return NULL;
787 get_row_parameter_line(tab, row, ineq->el);
788 if (ineq)
789 ineq = isl_vec_normalize(ineq);
791 return ineq;
794 /* Normalize a div expression of the form
796 * [(g*f(x) + c)/(g * m)]
798 * with c the constant term and f(x) the remaining coefficients, to
800 * [(f(x) + [c/g])/m]
802 static void normalize_div(__isl_keep isl_vec *div)
804 isl_ctx *ctx = isl_vec_get_ctx(div);
805 int len = div->size - 2;
807 isl_seq_gcd(div->el + 2, len, &ctx->normalize_gcd);
808 isl_int_gcd(ctx->normalize_gcd, ctx->normalize_gcd, div->el[0]);
810 if (isl_int_is_one(ctx->normalize_gcd))
811 return;
813 isl_int_divexact(div->el[0], div->el[0], ctx->normalize_gcd);
814 isl_int_fdiv_q(div->el[1], div->el[1], ctx->normalize_gcd);
815 isl_seq_scale_down(div->el + 2, div->el + 2, ctx->normalize_gcd, len);
818 /* Return an integer division for use in a parametric cut based
819 * on the given row.
820 * In particular, let the parametric constant of the row be
822 * \sum_i a_i y_i
824 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
825 * The div returned is equal to
827 * floor(\sum_i {-a_i} y_i) = floor((\sum_i (-a_i mod d) y_i)/d)
829 static struct isl_vec *get_row_parameter_div(struct isl_tab *tab, int row)
831 struct isl_vec *div;
833 div = isl_vec_alloc(tab->mat->ctx, 1 + 1 + tab->n_param + tab->n_div);
834 if (!div)
835 return NULL;
837 isl_int_set(div->el[0], tab->mat->row[row][0]);
838 get_row_parameter_line(tab, row, div->el + 1);
839 isl_seq_neg(div->el + 1, div->el + 1, div->size - 1);
840 normalize_div(div);
841 isl_seq_fdiv_r(div->el + 1, div->el + 1, div->el[0], div->size - 1);
843 return div;
846 /* Return an integer division for use in transferring an integrality constraint
847 * to the context.
848 * In particular, let the parametric constant of the row be
850 * \sum_i a_i y_i
852 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
853 * The the returned div is equal to
855 * floor(\sum_i {a_i} y_i) = floor((\sum_i (a_i mod d) y_i)/d)
857 static struct isl_vec *get_row_split_div(struct isl_tab *tab, int row)
859 struct isl_vec *div;
861 div = isl_vec_alloc(tab->mat->ctx, 1 + 1 + tab->n_param + tab->n_div);
862 if (!div)
863 return NULL;
865 isl_int_set(div->el[0], tab->mat->row[row][0]);
866 get_row_parameter_line(tab, row, div->el + 1);
867 normalize_div(div);
868 isl_seq_fdiv_r(div->el + 1, div->el + 1, div->el[0], div->size - 1);
870 return div;
873 /* Construct and return an inequality that expresses an upper bound
874 * on the given div.
875 * In particular, if the div is given by
877 * d = floor(e/m)
879 * then the inequality expresses
881 * m d <= e
883 static struct isl_vec *ineq_for_div(struct isl_basic_set *bset, unsigned div)
885 unsigned total;
886 unsigned div_pos;
887 struct isl_vec *ineq;
889 if (!bset)
890 return NULL;
892 total = isl_basic_set_total_dim(bset);
893 div_pos = 1 + total - bset->n_div + div;
895 ineq = isl_vec_alloc(bset->ctx, 1 + total);
896 if (!ineq)
897 return NULL;
899 isl_seq_cpy(ineq->el, bset->div[div] + 1, 1 + total);
900 isl_int_neg(ineq->el[div_pos], bset->div[div][0]);
901 return ineq;
904 /* Given a row in the tableau and a div that was created
905 * using get_row_split_div and that has been constrained to equality, i.e.,
907 * d = floor(\sum_i {a_i} y_i) = \sum_i {a_i} y_i
909 * replace the expression "\sum_i {a_i} y_i" in the row by d,
910 * i.e., we subtract "\sum_i {a_i} y_i" and add 1 d.
911 * The coefficients of the non-parameters in the tableau have been
912 * verified to be integral. We can therefore simply replace coefficient b
913 * by floor(b). For the coefficients of the parameters we have
914 * floor(a_i) = a_i - {a_i}, while for the other coefficients, we have
915 * floor(b) = b.
917 static struct isl_tab *set_row_cst_to_div(struct isl_tab *tab, int row, int div)
919 isl_seq_fdiv_q(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
920 tab->mat->row[row][0], 1 + tab->M + tab->n_col);
922 isl_int_set_si(tab->mat->row[row][0], 1);
924 if (tab->var[tab->n_var - tab->n_div + div].is_row) {
925 int drow = tab->var[tab->n_var - tab->n_div + div].index;
927 isl_assert(tab->mat->ctx,
928 isl_int_is_one(tab->mat->row[drow][0]), goto error);
929 isl_seq_combine(tab->mat->row[row] + 1,
930 tab->mat->ctx->one, tab->mat->row[row] + 1,
931 tab->mat->ctx->one, tab->mat->row[drow] + 1,
932 1 + tab->M + tab->n_col);
933 } else {
934 int dcol = tab->var[tab->n_var - tab->n_div + div].index;
936 isl_int_add_ui(tab->mat->row[row][2 + tab->M + dcol],
937 tab->mat->row[row][2 + tab->M + dcol], 1);
940 return tab;
941 error:
942 isl_tab_free(tab);
943 return NULL;
946 /* Check if the (parametric) constant of the given row is obviously
947 * negative, meaning that we don't need to consult the context tableau.
948 * If there is a big parameter and its coefficient is non-zero,
949 * then this coefficient determines the outcome.
950 * Otherwise, we check whether the constant is negative and
951 * all non-zero coefficients of parameters are negative and
952 * belong to non-negative parameters.
954 static int is_obviously_neg(struct isl_tab *tab, int row)
956 int i;
957 int col;
958 unsigned off = 2 + tab->M;
960 if (tab->M) {
961 if (isl_int_is_pos(tab->mat->row[row][2]))
962 return 0;
963 if (isl_int_is_neg(tab->mat->row[row][2]))
964 return 1;
967 if (isl_int_is_nonneg(tab->mat->row[row][1]))
968 return 0;
969 for (i = 0; i < tab->n_param; ++i) {
970 /* Eliminated parameter */
971 if (tab->var[i].is_row)
972 continue;
973 col = tab->var[i].index;
974 if (isl_int_is_zero(tab->mat->row[row][off + col]))
975 continue;
976 if (!tab->var[i].is_nonneg)
977 return 0;
978 if (isl_int_is_pos(tab->mat->row[row][off + col]))
979 return 0;
981 for (i = 0; i < tab->n_div; ++i) {
982 if (tab->var[tab->n_var - tab->n_div + i].is_row)
983 continue;
984 col = tab->var[tab->n_var - tab->n_div + i].index;
985 if (isl_int_is_zero(tab->mat->row[row][off + col]))
986 continue;
987 if (!tab->var[tab->n_var - tab->n_div + i].is_nonneg)
988 return 0;
989 if (isl_int_is_pos(tab->mat->row[row][off + col]))
990 return 0;
992 return 1;
995 /* Check if the (parametric) constant of the given row is obviously
996 * non-negative, meaning that we don't need to consult the context tableau.
997 * If there is a big parameter and its coefficient is non-zero,
998 * then this coefficient determines the outcome.
999 * Otherwise, we check whether the constant is non-negative and
1000 * all non-zero coefficients of parameters are positive and
1001 * belong to non-negative parameters.
1003 static int is_obviously_nonneg(struct isl_tab *tab, int row)
1005 int i;
1006 int col;
1007 unsigned off = 2 + tab->M;
1009 if (tab->M) {
1010 if (isl_int_is_pos(tab->mat->row[row][2]))
1011 return 1;
1012 if (isl_int_is_neg(tab->mat->row[row][2]))
1013 return 0;
1016 if (isl_int_is_neg(tab->mat->row[row][1]))
1017 return 0;
1018 for (i = 0; i < tab->n_param; ++i) {
1019 /* Eliminated parameter */
1020 if (tab->var[i].is_row)
1021 continue;
1022 col = tab->var[i].index;
1023 if (isl_int_is_zero(tab->mat->row[row][off + col]))
1024 continue;
1025 if (!tab->var[i].is_nonneg)
1026 return 0;
1027 if (isl_int_is_neg(tab->mat->row[row][off + col]))
1028 return 0;
1030 for (i = 0; i < tab->n_div; ++i) {
1031 if (tab->var[tab->n_var - tab->n_div + i].is_row)
1032 continue;
1033 col = tab->var[tab->n_var - tab->n_div + i].index;
1034 if (isl_int_is_zero(tab->mat->row[row][off + col]))
1035 continue;
1036 if (!tab->var[tab->n_var - tab->n_div + i].is_nonneg)
1037 return 0;
1038 if (isl_int_is_neg(tab->mat->row[row][off + col]))
1039 return 0;
1041 return 1;
1044 /* Given a row r and two columns, return the column that would
1045 * lead to the lexicographically smallest increment in the sample
1046 * solution when leaving the basis in favor of the row.
1047 * Pivoting with column c will increment the sample value by a non-negative
1048 * constant times a_{V,c}/a_{r,c}, with a_{V,c} the elements of column c
1049 * corresponding to the non-parametric variables.
1050 * If variable v appears in a column c_v, the a_{v,c} = 1 iff c = c_v,
1051 * with all other entries in this virtual row equal to zero.
1052 * If variable v appears in a row, then a_{v,c} is the element in column c
1053 * of that row.
1055 * Let v be the first variable with a_{v,c1}/a_{r,c1} != a_{v,c2}/a_{r,c2}.
1056 * Then if a_{v,c1}/a_{r,c1} < a_{v,c2}/a_{r,c2}, i.e.,
1057 * a_{v,c2} a_{r,c1} - a_{v,c1} a_{r,c2} > 0, c1 results in the minimal
1058 * increment. Otherwise, it's c2.
1060 static int lexmin_col_pair(struct isl_tab *tab,
1061 int row, int col1, int col2, isl_int tmp)
1063 int i;
1064 isl_int *tr;
1066 tr = tab->mat->row[row] + 2 + tab->M;
1068 for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
1069 int s1, s2;
1070 isl_int *r;
1072 if (!tab->var[i].is_row) {
1073 if (tab->var[i].index == col1)
1074 return col2;
1075 if (tab->var[i].index == col2)
1076 return col1;
1077 continue;
1080 if (tab->var[i].index == row)
1081 continue;
1083 r = tab->mat->row[tab->var[i].index] + 2 + tab->M;
1084 s1 = isl_int_sgn(r[col1]);
1085 s2 = isl_int_sgn(r[col2]);
1086 if (s1 == 0 && s2 == 0)
1087 continue;
1088 if (s1 < s2)
1089 return col1;
1090 if (s2 < s1)
1091 return col2;
1093 isl_int_mul(tmp, r[col2], tr[col1]);
1094 isl_int_submul(tmp, r[col1], tr[col2]);
1095 if (isl_int_is_pos(tmp))
1096 return col1;
1097 if (isl_int_is_neg(tmp))
1098 return col2;
1100 return -1;
1103 /* Given a row in the tableau, find and return the column that would
1104 * result in the lexicographically smallest, but positive, increment
1105 * in the sample point.
1106 * If there is no such column, then return tab->n_col.
1107 * If anything goes wrong, return -1.
1109 static int lexmin_pivot_col(struct isl_tab *tab, int row)
1111 int j;
1112 int col = tab->n_col;
1113 isl_int *tr;
1114 isl_int tmp;
1116 tr = tab->mat->row[row] + 2 + tab->M;
1118 isl_int_init(tmp);
1120 for (j = tab->n_dead; j < tab->n_col; ++j) {
1121 if (tab->col_var[j] >= 0 &&
1122 (tab->col_var[j] < tab->n_param ||
1123 tab->col_var[j] >= tab->n_var - tab->n_div))
1124 continue;
1126 if (!isl_int_is_pos(tr[j]))
1127 continue;
1129 if (col == tab->n_col)
1130 col = j;
1131 else
1132 col = lexmin_col_pair(tab, row, col, j, tmp);
1133 isl_assert(tab->mat->ctx, col >= 0, goto error);
1136 isl_int_clear(tmp);
1137 return col;
1138 error:
1139 isl_int_clear(tmp);
1140 return -1;
1143 /* Return the first known violated constraint, i.e., a non-negative
1144 * constraint that currently has an either obviously negative value
1145 * or a previously determined to be negative value.
1147 * If any constraint has a negative coefficient for the big parameter,
1148 * if any, then we return one of these first.
1150 static int first_neg(struct isl_tab *tab)
1152 int row;
1154 if (tab->M)
1155 for (row = tab->n_redundant; row < tab->n_row; ++row) {
1156 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
1157 continue;
1158 if (!isl_int_is_neg(tab->mat->row[row][2]))
1159 continue;
1160 if (tab->row_sign)
1161 tab->row_sign[row] = isl_tab_row_neg;
1162 return row;
1164 for (row = tab->n_redundant; row < tab->n_row; ++row) {
1165 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
1166 continue;
1167 if (tab->row_sign) {
1168 if (tab->row_sign[row] == 0 &&
1169 is_obviously_neg(tab, row))
1170 tab->row_sign[row] = isl_tab_row_neg;
1171 if (tab->row_sign[row] != isl_tab_row_neg)
1172 continue;
1173 } else if (!is_obviously_neg(tab, row))
1174 continue;
1175 return row;
1177 return -1;
1180 /* Check whether the invariant that all columns are lexico-positive
1181 * is satisfied. This function is not called from the current code
1182 * but is useful during debugging.
1184 static void check_lexpos(struct isl_tab *tab) __attribute__ ((unused));
1185 static void check_lexpos(struct isl_tab *tab)
1187 unsigned off = 2 + tab->M;
1188 int col;
1189 int var;
1190 int row;
1192 for (col = tab->n_dead; col < tab->n_col; ++col) {
1193 if (tab->col_var[col] >= 0 &&
1194 (tab->col_var[col] < tab->n_param ||
1195 tab->col_var[col] >= tab->n_var - tab->n_div))
1196 continue;
1197 for (var = tab->n_param; var < tab->n_var - tab->n_div; ++var) {
1198 if (!tab->var[var].is_row) {
1199 if (tab->var[var].index == col)
1200 break;
1201 else
1202 continue;
1204 row = tab->var[var].index;
1205 if (isl_int_is_zero(tab->mat->row[row][off + col]))
1206 continue;
1207 if (isl_int_is_pos(tab->mat->row[row][off + col]))
1208 break;
1209 fprintf(stderr, "lexneg column %d (row %d)\n",
1210 col, row);
1212 if (var >= tab->n_var - tab->n_div)
1213 fprintf(stderr, "zero column %d\n", col);
1217 /* Report to the caller that the given constraint is part of an encountered
1218 * conflict.
1220 static int report_conflicting_constraint(struct isl_tab *tab, int con)
1222 return tab->conflict(con, tab->conflict_user);
1225 /* Given a conflicting row in the tableau, report all constraints
1226 * involved in the row to the caller. That is, the row itself
1227 * (if it represents a constraint) and all constraint columns with
1228 * non-zero (and therefore negative) coefficients.
1230 static int report_conflict(struct isl_tab *tab, int row)
1232 int j;
1233 isl_int *tr;
1235 if (!tab->conflict)
1236 return 0;
1238 if (tab->row_var[row] < 0 &&
1239 report_conflicting_constraint(tab, ~tab->row_var[row]) < 0)
1240 return -1;
1242 tr = tab->mat->row[row] + 2 + tab->M;
1244 for (j = tab->n_dead; j < tab->n_col; ++j) {
1245 if (tab->col_var[j] >= 0 &&
1246 (tab->col_var[j] < tab->n_param ||
1247 tab->col_var[j] >= tab->n_var - tab->n_div))
1248 continue;
1250 if (!isl_int_is_neg(tr[j]))
1251 continue;
1253 if (tab->col_var[j] < 0 &&
1254 report_conflicting_constraint(tab, ~tab->col_var[j]) < 0)
1255 return -1;
1258 return 0;
1261 /* Resolve all known or obviously violated constraints through pivoting.
1262 * In particular, as long as we can find any violated constraint, we
1263 * look for a pivoting column that would result in the lexicographically
1264 * smallest increment in the sample point. If there is no such column
1265 * then the tableau is infeasible.
1267 static int restore_lexmin(struct isl_tab *tab) WARN_UNUSED;
1268 static int restore_lexmin(struct isl_tab *tab)
1270 int row, col;
1272 if (!tab)
1273 return -1;
1274 if (tab->empty)
1275 return 0;
1276 while ((row = first_neg(tab)) != -1) {
1277 col = lexmin_pivot_col(tab, row);
1278 if (col >= tab->n_col) {
1279 if (report_conflict(tab, row) < 0)
1280 return -1;
1281 if (isl_tab_mark_empty(tab) < 0)
1282 return -1;
1283 return 0;
1285 if (col < 0)
1286 return -1;
1287 if (isl_tab_pivot(tab, row, col) < 0)
1288 return -1;
1290 return 0;
1293 /* Given a row that represents an equality, look for an appropriate
1294 * pivoting column.
1295 * In particular, if there are any non-zero coefficients among
1296 * the non-parameter variables, then we take the last of these
1297 * variables. Eliminating this variable in terms of the other
1298 * variables and/or parameters does not influence the property
1299 * that all column in the initial tableau are lexicographically
1300 * positive. The row corresponding to the eliminated variable
1301 * will only have non-zero entries below the diagonal of the
1302 * initial tableau. That is, we transform
1304 * I I
1305 * 1 into a
1306 * I I
1308 * If there is no such non-parameter variable, then we are dealing with
1309 * pure parameter equality and we pick any parameter with coefficient 1 or -1
1310 * for elimination. This will ensure that the eliminated parameter
1311 * always has an integer value whenever all the other parameters are integral.
1312 * If there is no such parameter then we return -1.
1314 static int last_var_col_or_int_par_col(struct isl_tab *tab, int row)
1316 unsigned off = 2 + tab->M;
1317 int i;
1319 for (i = tab->n_var - tab->n_div - 1; i >= 0 && i >= tab->n_param; --i) {
1320 int col;
1321 if (tab->var[i].is_row)
1322 continue;
1323 col = tab->var[i].index;
1324 if (col <= tab->n_dead)
1325 continue;
1326 if (!isl_int_is_zero(tab->mat->row[row][off + col]))
1327 return col;
1329 for (i = tab->n_dead; i < tab->n_col; ++i) {
1330 if (isl_int_is_one(tab->mat->row[row][off + i]))
1331 return i;
1332 if (isl_int_is_negone(tab->mat->row[row][off + i]))
1333 return i;
1335 return -1;
1338 /* Add an equality that is known to be valid to the tableau.
1339 * We first check if we can eliminate a variable or a parameter.
1340 * If not, we add the equality as two inequalities.
1341 * In this case, the equality was a pure parameter equality and there
1342 * is no need to resolve any constraint violations.
1344 * This function assumes that at least two more rows and at least
1345 * two more elements in the constraint array are available in the tableau.
1347 static struct isl_tab *add_lexmin_valid_eq(struct isl_tab *tab, isl_int *eq)
1349 int i;
1350 int r;
1352 if (!tab)
1353 return NULL;
1354 r = isl_tab_add_row(tab, eq);
1355 if (r < 0)
1356 goto error;
1358 r = tab->con[r].index;
1359 i = last_var_col_or_int_par_col(tab, r);
1360 if (i < 0) {
1361 tab->con[r].is_nonneg = 1;
1362 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1363 goto error;
1364 isl_seq_neg(eq, eq, 1 + tab->n_var);
1365 r = isl_tab_add_row(tab, eq);
1366 if (r < 0)
1367 goto error;
1368 tab->con[r].is_nonneg = 1;
1369 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1370 goto error;
1371 } else {
1372 if (isl_tab_pivot(tab, r, i) < 0)
1373 goto error;
1374 if (isl_tab_kill_col(tab, i) < 0)
1375 goto error;
1376 tab->n_eq++;
1379 return tab;
1380 error:
1381 isl_tab_free(tab);
1382 return NULL;
1385 /* Check if the given row is a pure constant.
1387 static int is_constant(struct isl_tab *tab, int row)
1389 unsigned off = 2 + tab->M;
1391 return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1392 tab->n_col - tab->n_dead) == -1;
1395 /* Add an equality that may or may not be valid to the tableau.
1396 * If the resulting row is a pure constant, then it must be zero.
1397 * Otherwise, the resulting tableau is empty.
1399 * If the row is not a pure constant, then we add two inequalities,
1400 * each time checking that they can be satisfied.
1401 * In the end we try to use one of the two constraints to eliminate
1402 * a column.
1404 * This function assumes that at least two more rows and at least
1405 * two more elements in the constraint array are available in the tableau.
1407 static int add_lexmin_eq(struct isl_tab *tab, isl_int *eq) WARN_UNUSED;
1408 static int add_lexmin_eq(struct isl_tab *tab, isl_int *eq)
1410 int r1, r2;
1411 int row;
1412 struct isl_tab_undo *snap;
1414 if (!tab)
1415 return -1;
1416 snap = isl_tab_snap(tab);
1417 r1 = isl_tab_add_row(tab, eq);
1418 if (r1 < 0)
1419 return -1;
1420 tab->con[r1].is_nonneg = 1;
1421 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r1]) < 0)
1422 return -1;
1424 row = tab->con[r1].index;
1425 if (is_constant(tab, row)) {
1426 if (!isl_int_is_zero(tab->mat->row[row][1]) ||
1427 (tab->M && !isl_int_is_zero(tab->mat->row[row][2]))) {
1428 if (isl_tab_mark_empty(tab) < 0)
1429 return -1;
1430 return 0;
1432 if (isl_tab_rollback(tab, snap) < 0)
1433 return -1;
1434 return 0;
1437 if (restore_lexmin(tab) < 0)
1438 return -1;
1439 if (tab->empty)
1440 return 0;
1442 isl_seq_neg(eq, eq, 1 + tab->n_var);
1444 r2 = isl_tab_add_row(tab, eq);
1445 if (r2 < 0)
1446 return -1;
1447 tab->con[r2].is_nonneg = 1;
1448 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r2]) < 0)
1449 return -1;
1451 if (restore_lexmin(tab) < 0)
1452 return -1;
1453 if (tab->empty)
1454 return 0;
1456 if (!tab->con[r1].is_row) {
1457 if (isl_tab_kill_col(tab, tab->con[r1].index) < 0)
1458 return -1;
1459 } else if (!tab->con[r2].is_row) {
1460 if (isl_tab_kill_col(tab, tab->con[r2].index) < 0)
1461 return -1;
1464 if (tab->bmap) {
1465 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1466 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1467 return -1;
1468 isl_seq_neg(eq, eq, 1 + tab->n_var);
1469 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1470 isl_seq_neg(eq, eq, 1 + tab->n_var);
1471 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1472 return -1;
1473 if (!tab->bmap)
1474 return -1;
1477 return 0;
1480 /* Add an inequality to the tableau, resolving violations using
1481 * restore_lexmin.
1483 * This function assumes that at least one more row and at least
1484 * one more element in the constraint array are available in the tableau.
1486 static struct isl_tab *add_lexmin_ineq(struct isl_tab *tab, isl_int *ineq)
1488 int r;
1490 if (!tab)
1491 return NULL;
1492 if (tab->bmap) {
1493 tab->bmap = isl_basic_map_add_ineq(tab->bmap, ineq);
1494 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1495 goto error;
1496 if (!tab->bmap)
1497 goto error;
1499 r = isl_tab_add_row(tab, ineq);
1500 if (r < 0)
1501 goto error;
1502 tab->con[r].is_nonneg = 1;
1503 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1504 goto error;
1505 if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1506 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1507 goto error;
1508 return tab;
1511 if (restore_lexmin(tab) < 0)
1512 goto error;
1513 if (!tab->empty && tab->con[r].is_row &&
1514 isl_tab_row_is_redundant(tab, tab->con[r].index))
1515 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1516 goto error;
1517 return tab;
1518 error:
1519 isl_tab_free(tab);
1520 return NULL;
1523 /* Check if the coefficients of the parameters are all integral.
1525 static int integer_parameter(struct isl_tab *tab, int row)
1527 int i;
1528 int col;
1529 unsigned off = 2 + tab->M;
1531 for (i = 0; i < tab->n_param; ++i) {
1532 /* Eliminated parameter */
1533 if (tab->var[i].is_row)
1534 continue;
1535 col = tab->var[i].index;
1536 if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
1537 tab->mat->row[row][0]))
1538 return 0;
1540 for (i = 0; i < tab->n_div; ++i) {
1541 if (tab->var[tab->n_var - tab->n_div + i].is_row)
1542 continue;
1543 col = tab->var[tab->n_var - tab->n_div + i].index;
1544 if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
1545 tab->mat->row[row][0]))
1546 return 0;
1548 return 1;
1551 /* Check if the coefficients of the non-parameter variables are all integral.
1553 static int integer_variable(struct isl_tab *tab, int row)
1555 int i;
1556 unsigned off = 2 + tab->M;
1558 for (i = tab->n_dead; i < tab->n_col; ++i) {
1559 if (tab->col_var[i] >= 0 &&
1560 (tab->col_var[i] < tab->n_param ||
1561 tab->col_var[i] >= tab->n_var - tab->n_div))
1562 continue;
1563 if (!isl_int_is_divisible_by(tab->mat->row[row][off + i],
1564 tab->mat->row[row][0]))
1565 return 0;
1567 return 1;
1570 /* Check if the constant term is integral.
1572 static int integer_constant(struct isl_tab *tab, int row)
1574 return isl_int_is_divisible_by(tab->mat->row[row][1],
1575 tab->mat->row[row][0]);
1578 #define I_CST 1 << 0
1579 #define I_PAR 1 << 1
1580 #define I_VAR 1 << 2
1582 /* Check for next (non-parameter) variable after "var" (first if var == -1)
1583 * that is non-integer and therefore requires a cut and return
1584 * the index of the variable.
1585 * For parametric tableaus, there are three parts in a row,
1586 * the constant, the coefficients of the parameters and the rest.
1587 * For each part, we check whether the coefficients in that part
1588 * are all integral and if so, set the corresponding flag in *f.
1589 * If the constant and the parameter part are integral, then the
1590 * current sample value is integral and no cut is required
1591 * (irrespective of whether the variable part is integral).
1593 static int next_non_integer_var(struct isl_tab *tab, int var, int *f)
1595 var = var < 0 ? tab->n_param : var + 1;
1597 for (; var < tab->n_var - tab->n_div; ++var) {
1598 int flags = 0;
1599 int row;
1600 if (!tab->var[var].is_row)
1601 continue;
1602 row = tab->var[var].index;
1603 if (integer_constant(tab, row))
1604 ISL_FL_SET(flags, I_CST);
1605 if (integer_parameter(tab, row))
1606 ISL_FL_SET(flags, I_PAR);
1607 if (ISL_FL_ISSET(flags, I_CST) && ISL_FL_ISSET(flags, I_PAR))
1608 continue;
1609 if (integer_variable(tab, row))
1610 ISL_FL_SET(flags, I_VAR);
1611 *f = flags;
1612 return var;
1614 return -1;
1617 /* Check for first (non-parameter) variable that is non-integer and
1618 * therefore requires a cut and return the corresponding row.
1619 * For parametric tableaus, there are three parts in a row,
1620 * the constant, the coefficients of the parameters and the rest.
1621 * For each part, we check whether the coefficients in that part
1622 * are all integral and if so, set the corresponding flag in *f.
1623 * If the constant and the parameter part are integral, then the
1624 * current sample value is integral and no cut is required
1625 * (irrespective of whether the variable part is integral).
1627 static int first_non_integer_row(struct isl_tab *tab, int *f)
1629 int var = next_non_integer_var(tab, -1, f);
1631 return var < 0 ? -1 : tab->var[var].index;
1634 /* Add a (non-parametric) cut to cut away the non-integral sample
1635 * value of the given row.
1637 * If the row is given by
1639 * m r = f + \sum_i a_i y_i
1641 * then the cut is
1643 * c = - {-f/m} + \sum_i {a_i/m} y_i >= 0
1645 * The big parameter, if any, is ignored, since it is assumed to be big
1646 * enough to be divisible by any integer.
1647 * If the tableau is actually a parametric tableau, then this function
1648 * is only called when all coefficients of the parameters are integral.
1649 * The cut therefore has zero coefficients for the parameters.
1651 * The current value is known to be negative, so row_sign, if it
1652 * exists, is set accordingly.
1654 * Return the row of the cut or -1.
1656 static int add_cut(struct isl_tab *tab, int row)
1658 int i;
1659 int r;
1660 isl_int *r_row;
1661 unsigned off = 2 + tab->M;
1663 if (isl_tab_extend_cons(tab, 1) < 0)
1664 return -1;
1665 r = isl_tab_allocate_con(tab);
1666 if (r < 0)
1667 return -1;
1669 r_row = tab->mat->row[tab->con[r].index];
1670 isl_int_set(r_row[0], tab->mat->row[row][0]);
1671 isl_int_neg(r_row[1], tab->mat->row[row][1]);
1672 isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
1673 isl_int_neg(r_row[1], r_row[1]);
1674 if (tab->M)
1675 isl_int_set_si(r_row[2], 0);
1676 for (i = 0; i < tab->n_col; ++i)
1677 isl_int_fdiv_r(r_row[off + i],
1678 tab->mat->row[row][off + i], tab->mat->row[row][0]);
1680 tab->con[r].is_nonneg = 1;
1681 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1682 return -1;
1683 if (tab->row_sign)
1684 tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
1686 return tab->con[r].index;
1689 #define CUT_ALL 1
1690 #define CUT_ONE 0
1692 /* Given a non-parametric tableau, add cuts until an integer
1693 * sample point is obtained or until the tableau is determined
1694 * to be integer infeasible.
1695 * As long as there is any non-integer value in the sample point,
1696 * we add appropriate cuts, if possible, for each of these
1697 * non-integer values and then resolve the violated
1698 * cut constraints using restore_lexmin.
1699 * If one of the corresponding rows is equal to an integral
1700 * combination of variables/constraints plus a non-integral constant,
1701 * then there is no way to obtain an integer point and we return
1702 * a tableau that is marked empty.
1703 * The parameter cutting_strategy controls the strategy used when adding cuts
1704 * to remove non-integer points. CUT_ALL adds all possible cuts
1705 * before continuing the search. CUT_ONE adds only one cut at a time.
1707 static struct isl_tab *cut_to_integer_lexmin(struct isl_tab *tab,
1708 int cutting_strategy)
1710 int var;
1711 int row;
1712 int flags;
1714 if (!tab)
1715 return NULL;
1716 if (tab->empty)
1717 return tab;
1719 while ((var = next_non_integer_var(tab, -1, &flags)) != -1) {
1720 do {
1721 if (ISL_FL_ISSET(flags, I_VAR)) {
1722 if (isl_tab_mark_empty(tab) < 0)
1723 goto error;
1724 return tab;
1726 row = tab->var[var].index;
1727 row = add_cut(tab, row);
1728 if (row < 0)
1729 goto error;
1730 if (cutting_strategy == CUT_ONE)
1731 break;
1732 } while ((var = next_non_integer_var(tab, var, &flags)) != -1);
1733 if (restore_lexmin(tab) < 0)
1734 goto error;
1735 if (tab->empty)
1736 break;
1738 return tab;
1739 error:
1740 isl_tab_free(tab);
1741 return NULL;
1744 /* Check whether all the currently active samples also satisfy the inequality
1745 * "ineq" (treated as an equality if eq is set).
1746 * Remove those samples that do not.
1748 static struct isl_tab *check_samples(struct isl_tab *tab, isl_int *ineq, int eq)
1750 int i;
1751 isl_int v;
1753 if (!tab)
1754 return NULL;
1756 isl_assert(tab->mat->ctx, tab->bmap, goto error);
1757 isl_assert(tab->mat->ctx, tab->samples, goto error);
1758 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, goto error);
1760 isl_int_init(v);
1761 for (i = tab->n_outside; i < tab->n_sample; ++i) {
1762 int sgn;
1763 isl_seq_inner_product(ineq, tab->samples->row[i],
1764 1 + tab->n_var, &v);
1765 sgn = isl_int_sgn(v);
1766 if (eq ? (sgn == 0) : (sgn >= 0))
1767 continue;
1768 tab = isl_tab_drop_sample(tab, i);
1769 if (!tab)
1770 break;
1772 isl_int_clear(v);
1774 return tab;
1775 error:
1776 isl_tab_free(tab);
1777 return NULL;
1780 /* Check whether the sample value of the tableau is finite,
1781 * i.e., either the tableau does not use a big parameter, or
1782 * all values of the variables are equal to the big parameter plus
1783 * some constant. This constant is the actual sample value.
1785 static int sample_is_finite(struct isl_tab *tab)
1787 int i;
1789 if (!tab->M)
1790 return 1;
1792 for (i = 0; i < tab->n_var; ++i) {
1793 int row;
1794 if (!tab->var[i].is_row)
1795 return 0;
1796 row = tab->var[i].index;
1797 if (isl_int_ne(tab->mat->row[row][0], tab->mat->row[row][2]))
1798 return 0;
1800 return 1;
1803 /* Check if the context tableau of sol has any integer points.
1804 * Leave tab in empty state if no integer point can be found.
1805 * If an integer point can be found and if moreover it is finite,
1806 * then it is added to the list of sample values.
1808 * This function is only called when none of the currently active sample
1809 * values satisfies the most recently added constraint.
1811 static struct isl_tab *check_integer_feasible(struct isl_tab *tab)
1813 struct isl_tab_undo *snap;
1815 if (!tab)
1816 return NULL;
1818 snap = isl_tab_snap(tab);
1819 if (isl_tab_push_basis(tab) < 0)
1820 goto error;
1822 tab = cut_to_integer_lexmin(tab, CUT_ALL);
1823 if (!tab)
1824 goto error;
1826 if (!tab->empty && sample_is_finite(tab)) {
1827 struct isl_vec *sample;
1829 sample = isl_tab_get_sample_value(tab);
1831 if (isl_tab_add_sample(tab, sample) < 0)
1832 goto error;
1835 if (!tab->empty && isl_tab_rollback(tab, snap) < 0)
1836 goto error;
1838 return tab;
1839 error:
1840 isl_tab_free(tab);
1841 return NULL;
1844 /* Check if any of the currently active sample values satisfies
1845 * the inequality "ineq" (an equality if eq is set).
1847 static int tab_has_valid_sample(struct isl_tab *tab, isl_int *ineq, int eq)
1849 int i;
1850 isl_int v;
1852 if (!tab)
1853 return -1;
1855 isl_assert(tab->mat->ctx, tab->bmap, return -1);
1856 isl_assert(tab->mat->ctx, tab->samples, return -1);
1857 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, return -1);
1859 isl_int_init(v);
1860 for (i = tab->n_outside; i < tab->n_sample; ++i) {
1861 int sgn;
1862 isl_seq_inner_product(ineq, tab->samples->row[i],
1863 1 + tab->n_var, &v);
1864 sgn = isl_int_sgn(v);
1865 if (eq ? (sgn == 0) : (sgn >= 0))
1866 break;
1868 isl_int_clear(v);
1870 return i < tab->n_sample;
1873 /* Insert a div specified by "div" to the tableau "tab" at position "pos" and
1874 * return isl_bool_true if the div is obviously non-negative.
1876 static isl_bool context_tab_insert_div(struct isl_tab *tab, int pos,
1877 __isl_keep isl_vec *div,
1878 int (*add_ineq)(void *user, isl_int *), void *user)
1880 int i;
1881 int r;
1882 struct isl_mat *samples;
1883 int nonneg;
1885 r = isl_tab_insert_div(tab, pos, div, add_ineq, user);
1886 if (r < 0)
1887 return isl_bool_error;
1888 nonneg = tab->var[r].is_nonneg;
1889 tab->var[r].frozen = 1;
1891 samples = isl_mat_extend(tab->samples,
1892 tab->n_sample, 1 + tab->n_var);
1893 tab->samples = samples;
1894 if (!samples)
1895 return isl_bool_error;
1896 for (i = tab->n_outside; i < samples->n_row; ++i) {
1897 isl_seq_inner_product(div->el + 1, samples->row[i],
1898 div->size - 1, &samples->row[i][samples->n_col - 1]);
1899 isl_int_fdiv_q(samples->row[i][samples->n_col - 1],
1900 samples->row[i][samples->n_col - 1], div->el[0]);
1902 tab->samples = isl_mat_move_cols(tab->samples, 1 + pos,
1903 1 + tab->n_var - 1, 1);
1904 if (!tab->samples)
1905 return isl_bool_error;
1907 return nonneg;
1910 /* Add a div specified by "div" to both the main tableau and
1911 * the context tableau. In case of the main tableau, we only
1912 * need to add an extra div. In the context tableau, we also
1913 * need to express the meaning of the div.
1914 * Return the index of the div or -1 if anything went wrong.
1916 * The new integer division is added before any unknown integer
1917 * divisions in the context to ensure that it does not get
1918 * equated to some linear combination involving unknown integer
1919 * divisions.
1921 static int add_div(struct isl_tab *tab, struct isl_context *context,
1922 __isl_keep isl_vec *div)
1924 int r;
1925 int pos;
1926 isl_bool nonneg;
1927 struct isl_tab *context_tab = context->op->peek_tab(context);
1929 if (!tab || !context_tab)
1930 goto error;
1932 pos = context_tab->n_var - context->n_unknown;
1933 if ((nonneg = context->op->insert_div(context, pos, div)) < 0)
1934 goto error;
1936 if (!context->op->is_ok(context))
1937 goto error;
1939 pos = tab->n_var - context->n_unknown;
1940 if (isl_tab_extend_vars(tab, 1) < 0)
1941 goto error;
1942 r = isl_tab_insert_var(tab, pos);
1943 if (r < 0)
1944 goto error;
1945 if (nonneg)
1946 tab->var[r].is_nonneg = 1;
1947 tab->var[r].frozen = 1;
1948 tab->n_div++;
1950 return tab->n_div - 1 - context->n_unknown;
1951 error:
1952 context->op->invalidate(context);
1953 return -1;
1956 static int find_div(struct isl_tab *tab, isl_int *div, isl_int denom)
1958 int i;
1959 unsigned total = isl_basic_map_total_dim(tab->bmap);
1961 for (i = 0; i < tab->bmap->n_div; ++i) {
1962 if (isl_int_ne(tab->bmap->div[i][0], denom))
1963 continue;
1964 if (!isl_seq_eq(tab->bmap->div[i] + 1, div, 1 + total))
1965 continue;
1966 return i;
1968 return -1;
1971 /* Return the index of a div that corresponds to "div".
1972 * We first check if we already have such a div and if not, we create one.
1974 static int get_div(struct isl_tab *tab, struct isl_context *context,
1975 struct isl_vec *div)
1977 int d;
1978 struct isl_tab *context_tab = context->op->peek_tab(context);
1980 if (!context_tab)
1981 return -1;
1983 d = find_div(context_tab, div->el + 1, div->el[0]);
1984 if (d != -1)
1985 return d;
1987 return add_div(tab, context, div);
1990 /* Add a parametric cut to cut away the non-integral sample value
1991 * of the give row.
1992 * Let a_i be the coefficients of the constant term and the parameters
1993 * and let b_i be the coefficients of the variables or constraints
1994 * in basis of the tableau.
1995 * Let q be the div q = floor(\sum_i {-a_i} y_i).
1997 * The cut is expressed as
1999 * c = \sum_i -{-a_i} y_i + \sum_i {b_i} x_i + q >= 0
2001 * If q did not already exist in the context tableau, then it is added first.
2002 * If q is in a column of the main tableau then the "+ q" can be accomplished
2003 * by setting the corresponding entry to the denominator of the constraint.
2004 * If q happens to be in a row of the main tableau, then the corresponding
2005 * row needs to be added instead (taking care of the denominators).
2006 * Note that this is very unlikely, but perhaps not entirely impossible.
2008 * The current value of the cut is known to be negative (or at least
2009 * non-positive), so row_sign is set accordingly.
2011 * Return the row of the cut or -1.
2013 static int add_parametric_cut(struct isl_tab *tab, int row,
2014 struct isl_context *context)
2016 struct isl_vec *div;
2017 int d;
2018 int i;
2019 int r;
2020 isl_int *r_row;
2021 int col;
2022 int n;
2023 unsigned off = 2 + tab->M;
2025 if (!context)
2026 return -1;
2028 div = get_row_parameter_div(tab, row);
2029 if (!div)
2030 return -1;
2032 n = tab->n_div - context->n_unknown;
2033 d = context->op->get_div(context, tab, div);
2034 isl_vec_free(div);
2035 if (d < 0)
2036 return -1;
2038 if (isl_tab_extend_cons(tab, 1) < 0)
2039 return -1;
2040 r = isl_tab_allocate_con(tab);
2041 if (r < 0)
2042 return -1;
2044 r_row = tab->mat->row[tab->con[r].index];
2045 isl_int_set(r_row[0], tab->mat->row[row][0]);
2046 isl_int_neg(r_row[1], tab->mat->row[row][1]);
2047 isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
2048 isl_int_neg(r_row[1], r_row[1]);
2049 if (tab->M)
2050 isl_int_set_si(r_row[2], 0);
2051 for (i = 0; i < tab->n_param; ++i) {
2052 if (tab->var[i].is_row)
2053 continue;
2054 col = tab->var[i].index;
2055 isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
2056 isl_int_fdiv_r(r_row[off + col], r_row[off + col],
2057 tab->mat->row[row][0]);
2058 isl_int_neg(r_row[off + col], r_row[off + col]);
2060 for (i = 0; i < tab->n_div; ++i) {
2061 if (tab->var[tab->n_var - tab->n_div + i].is_row)
2062 continue;
2063 col = tab->var[tab->n_var - tab->n_div + i].index;
2064 isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
2065 isl_int_fdiv_r(r_row[off + col], r_row[off + col],
2066 tab->mat->row[row][0]);
2067 isl_int_neg(r_row[off + col], r_row[off + col]);
2069 for (i = 0; i < tab->n_col; ++i) {
2070 if (tab->col_var[i] >= 0 &&
2071 (tab->col_var[i] < tab->n_param ||
2072 tab->col_var[i] >= tab->n_var - tab->n_div))
2073 continue;
2074 isl_int_fdiv_r(r_row[off + i],
2075 tab->mat->row[row][off + i], tab->mat->row[row][0]);
2077 if (tab->var[tab->n_var - tab->n_div + d].is_row) {
2078 isl_int gcd;
2079 int d_row = tab->var[tab->n_var - tab->n_div + d].index;
2080 isl_int_init(gcd);
2081 isl_int_gcd(gcd, tab->mat->row[d_row][0], r_row[0]);
2082 isl_int_divexact(r_row[0], r_row[0], gcd);
2083 isl_int_divexact(gcd, tab->mat->row[d_row][0], gcd);
2084 isl_seq_combine(r_row + 1, gcd, r_row + 1,
2085 r_row[0], tab->mat->row[d_row] + 1,
2086 off - 1 + tab->n_col);
2087 isl_int_mul(r_row[0], r_row[0], tab->mat->row[d_row][0]);
2088 isl_int_clear(gcd);
2089 } else {
2090 col = tab->var[tab->n_var - tab->n_div + d].index;
2091 isl_int_set(r_row[off + col], tab->mat->row[row][0]);
2094 tab->con[r].is_nonneg = 1;
2095 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
2096 return -1;
2097 if (tab->row_sign)
2098 tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
2100 row = tab->con[r].index;
2102 if (d >= n && context->op->detect_equalities(context, tab) < 0)
2103 return -1;
2105 return row;
2108 /* Construct a tableau for bmap that can be used for computing
2109 * the lexicographic minimum (or maximum) of bmap.
2110 * If not NULL, then dom is the domain where the minimum
2111 * should be computed. In this case, we set up a parametric
2112 * tableau with row signs (initialized to "unknown").
2113 * If M is set, then the tableau will use a big parameter.
2114 * If max is set, then a maximum should be computed instead of a minimum.
2115 * This means that for each variable x, the tableau will contain the variable
2116 * x' = M - x, rather than x' = M + x. This in turn means that the coefficient
2117 * of the variables in all constraints are negated prior to adding them
2118 * to the tableau.
2120 static struct isl_tab *tab_for_lexmin(struct isl_basic_map *bmap,
2121 struct isl_basic_set *dom, unsigned M, int max)
2123 int i;
2124 struct isl_tab *tab;
2125 unsigned n_var;
2126 unsigned o_var;
2128 tab = isl_tab_alloc(bmap->ctx, 2 * bmap->n_eq + bmap->n_ineq + 1,
2129 isl_basic_map_total_dim(bmap), M);
2130 if (!tab)
2131 return NULL;
2133 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
2134 if (dom) {
2135 tab->n_param = isl_basic_set_total_dim(dom) - dom->n_div;
2136 tab->n_div = dom->n_div;
2137 tab->row_sign = isl_calloc_array(bmap->ctx,
2138 enum isl_tab_row_sign, tab->mat->n_row);
2139 if (tab->mat->n_row && !tab->row_sign)
2140 goto error;
2142 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
2143 if (isl_tab_mark_empty(tab) < 0)
2144 goto error;
2145 return tab;
2148 for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
2149 tab->var[i].is_nonneg = 1;
2150 tab->var[i].frozen = 1;
2152 o_var = 1 + tab->n_param;
2153 n_var = tab->n_var - tab->n_param - tab->n_div;
2154 for (i = 0; i < bmap->n_eq; ++i) {
2155 if (max)
2156 isl_seq_neg(bmap->eq[i] + o_var,
2157 bmap->eq[i] + o_var, n_var);
2158 tab = add_lexmin_valid_eq(tab, bmap->eq[i]);
2159 if (max)
2160 isl_seq_neg(bmap->eq[i] + o_var,
2161 bmap->eq[i] + o_var, n_var);
2162 if (!tab || tab->empty)
2163 return tab;
2165 if (bmap->n_eq && restore_lexmin(tab) < 0)
2166 goto error;
2167 for (i = 0; i < bmap->n_ineq; ++i) {
2168 if (max)
2169 isl_seq_neg(bmap->ineq[i] + o_var,
2170 bmap->ineq[i] + o_var, n_var);
2171 tab = add_lexmin_ineq(tab, bmap->ineq[i]);
2172 if (max)
2173 isl_seq_neg(bmap->ineq[i] + o_var,
2174 bmap->ineq[i] + o_var, n_var);
2175 if (!tab || tab->empty)
2176 return tab;
2178 return tab;
2179 error:
2180 isl_tab_free(tab);
2181 return NULL;
2184 /* Given a main tableau where more than one row requires a split,
2185 * determine and return the "best" row to split on.
2187 * Given two rows in the main tableau, if the inequality corresponding
2188 * to the first row is redundant with respect to that of the second row
2189 * in the current tableau, then it is better to split on the second row,
2190 * since in the positive part, both rows will be positive.
2191 * (In the negative part a pivot will have to be performed and just about
2192 * anything can happen to the sign of the other row.)
2194 * As a simple heuristic, we therefore select the row that makes the most
2195 * of the other rows redundant.
2197 * Perhaps it would also be useful to look at the number of constraints
2198 * that conflict with any given constraint.
2200 * best is the best row so far (-1 when we have not found any row yet).
2201 * best_r is the number of other rows made redundant by row best.
2202 * When best is still -1, bset_r is meaningless, but it is initialized
2203 * to some arbitrary value (0) anyway. Without this redundant initialization
2204 * valgrind may warn about uninitialized memory accesses when isl
2205 * is compiled with some versions of gcc.
2207 static int best_split(struct isl_tab *tab, struct isl_tab *context_tab)
2209 struct isl_tab_undo *snap;
2210 int split;
2211 int row;
2212 int best = -1;
2213 int best_r = 0;
2215 if (isl_tab_extend_cons(context_tab, 2) < 0)
2216 return -1;
2218 snap = isl_tab_snap(context_tab);
2220 for (split = tab->n_redundant; split < tab->n_row; ++split) {
2221 struct isl_tab_undo *snap2;
2222 struct isl_vec *ineq = NULL;
2223 int r = 0;
2224 int ok;
2226 if (!isl_tab_var_from_row(tab, split)->is_nonneg)
2227 continue;
2228 if (tab->row_sign[split] != isl_tab_row_any)
2229 continue;
2231 ineq = get_row_parameter_ineq(tab, split);
2232 if (!ineq)
2233 return -1;
2234 ok = isl_tab_add_ineq(context_tab, ineq->el) >= 0;
2235 isl_vec_free(ineq);
2236 if (!ok)
2237 return -1;
2239 snap2 = isl_tab_snap(context_tab);
2241 for (row = tab->n_redundant; row < tab->n_row; ++row) {
2242 struct isl_tab_var *var;
2244 if (row == split)
2245 continue;
2246 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
2247 continue;
2248 if (tab->row_sign[row] != isl_tab_row_any)
2249 continue;
2251 ineq = get_row_parameter_ineq(tab, row);
2252 if (!ineq)
2253 return -1;
2254 ok = isl_tab_add_ineq(context_tab, ineq->el) >= 0;
2255 isl_vec_free(ineq);
2256 if (!ok)
2257 return -1;
2258 var = &context_tab->con[context_tab->n_con - 1];
2259 if (!context_tab->empty &&
2260 !isl_tab_min_at_most_neg_one(context_tab, var))
2261 r++;
2262 if (isl_tab_rollback(context_tab, snap2) < 0)
2263 return -1;
2265 if (best == -1 || r > best_r) {
2266 best = split;
2267 best_r = r;
2269 if (isl_tab_rollback(context_tab, snap) < 0)
2270 return -1;
2273 return best;
2276 static struct isl_basic_set *context_lex_peek_basic_set(
2277 struct isl_context *context)
2279 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2280 if (!clex->tab)
2281 return NULL;
2282 return isl_tab_peek_bset(clex->tab);
2285 static struct isl_tab *context_lex_peek_tab(struct isl_context *context)
2287 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2288 return clex->tab;
2291 static void context_lex_add_eq(struct isl_context *context, isl_int *eq,
2292 int check, int update)
2294 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2295 if (isl_tab_extend_cons(clex->tab, 2) < 0)
2296 goto error;
2297 if (add_lexmin_eq(clex->tab, eq) < 0)
2298 goto error;
2299 if (check) {
2300 int v = tab_has_valid_sample(clex->tab, eq, 1);
2301 if (v < 0)
2302 goto error;
2303 if (!v)
2304 clex->tab = check_integer_feasible(clex->tab);
2306 if (update)
2307 clex->tab = check_samples(clex->tab, eq, 1);
2308 return;
2309 error:
2310 isl_tab_free(clex->tab);
2311 clex->tab = NULL;
2314 static void context_lex_add_ineq(struct isl_context *context, isl_int *ineq,
2315 int check, int update)
2317 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2318 if (isl_tab_extend_cons(clex->tab, 1) < 0)
2319 goto error;
2320 clex->tab = add_lexmin_ineq(clex->tab, ineq);
2321 if (check) {
2322 int v = tab_has_valid_sample(clex->tab, ineq, 0);
2323 if (v < 0)
2324 goto error;
2325 if (!v)
2326 clex->tab = check_integer_feasible(clex->tab);
2328 if (update)
2329 clex->tab = check_samples(clex->tab, ineq, 0);
2330 return;
2331 error:
2332 isl_tab_free(clex->tab);
2333 clex->tab = NULL;
2336 static int context_lex_add_ineq_wrap(void *user, isl_int *ineq)
2338 struct isl_context *context = (struct isl_context *)user;
2339 context_lex_add_ineq(context, ineq, 0, 0);
2340 return context->op->is_ok(context) ? 0 : -1;
2343 /* Check which signs can be obtained by "ineq" on all the currently
2344 * active sample values. See row_sign for more information.
2346 static enum isl_tab_row_sign tab_ineq_sign(struct isl_tab *tab, isl_int *ineq,
2347 int strict)
2349 int i;
2350 int sgn;
2351 isl_int tmp;
2352 enum isl_tab_row_sign res = isl_tab_row_unknown;
2354 isl_assert(tab->mat->ctx, tab->samples, return isl_tab_row_unknown);
2355 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var,
2356 return isl_tab_row_unknown);
2358 isl_int_init(tmp);
2359 for (i = tab->n_outside; i < tab->n_sample; ++i) {
2360 isl_seq_inner_product(tab->samples->row[i], ineq,
2361 1 + tab->n_var, &tmp);
2362 sgn = isl_int_sgn(tmp);
2363 if (sgn > 0 || (sgn == 0 && strict)) {
2364 if (res == isl_tab_row_unknown)
2365 res = isl_tab_row_pos;
2366 if (res == isl_tab_row_neg)
2367 res = isl_tab_row_any;
2369 if (sgn < 0) {
2370 if (res == isl_tab_row_unknown)
2371 res = isl_tab_row_neg;
2372 if (res == isl_tab_row_pos)
2373 res = isl_tab_row_any;
2375 if (res == isl_tab_row_any)
2376 break;
2378 isl_int_clear(tmp);
2380 return res;
2383 static enum isl_tab_row_sign context_lex_ineq_sign(struct isl_context *context,
2384 isl_int *ineq, int strict)
2386 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2387 return tab_ineq_sign(clex->tab, ineq, strict);
2390 /* Check whether "ineq" can be added to the tableau without rendering
2391 * it infeasible.
2393 static int context_lex_test_ineq(struct isl_context *context, isl_int *ineq)
2395 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2396 struct isl_tab_undo *snap;
2397 int feasible;
2399 if (!clex->tab)
2400 return -1;
2402 if (isl_tab_extend_cons(clex->tab, 1) < 0)
2403 return -1;
2405 snap = isl_tab_snap(clex->tab);
2406 if (isl_tab_push_basis(clex->tab) < 0)
2407 return -1;
2408 clex->tab = add_lexmin_ineq(clex->tab, ineq);
2409 clex->tab = check_integer_feasible(clex->tab);
2410 if (!clex->tab)
2411 return -1;
2412 feasible = !clex->tab->empty;
2413 if (isl_tab_rollback(clex->tab, snap) < 0)
2414 return -1;
2416 return feasible;
2419 static int context_lex_get_div(struct isl_context *context, struct isl_tab *tab,
2420 struct isl_vec *div)
2422 return get_div(tab, context, div);
2425 /* Insert a div specified by "div" to the context tableau at position "pos" and
2426 * return isl_bool_true if the div is obviously non-negative.
2427 * context_tab_add_div will always return isl_bool_true, because all variables
2428 * in a isl_context_lex tableau are non-negative.
2429 * However, if we are using a big parameter in the context, then this only
2430 * reflects the non-negativity of the variable used to _encode_ the
2431 * div, i.e., div' = M + div, so we can't draw any conclusions.
2433 static isl_bool context_lex_insert_div(struct isl_context *context, int pos,
2434 __isl_keep isl_vec *div)
2436 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2437 isl_bool nonneg;
2438 nonneg = context_tab_insert_div(clex->tab, pos, div,
2439 context_lex_add_ineq_wrap, context);
2440 if (nonneg < 0)
2441 return isl_bool_error;
2442 if (clex->tab->M)
2443 return isl_bool_false;
2444 return nonneg;
2447 static int context_lex_detect_equalities(struct isl_context *context,
2448 struct isl_tab *tab)
2450 return 0;
2453 static int context_lex_best_split(struct isl_context *context,
2454 struct isl_tab *tab)
2456 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2457 struct isl_tab_undo *snap;
2458 int r;
2460 snap = isl_tab_snap(clex->tab);
2461 if (isl_tab_push_basis(clex->tab) < 0)
2462 return -1;
2463 r = best_split(tab, clex->tab);
2465 if (r >= 0 && isl_tab_rollback(clex->tab, snap) < 0)
2466 return -1;
2468 return r;
2471 static int context_lex_is_empty(struct isl_context *context)
2473 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2474 if (!clex->tab)
2475 return -1;
2476 return clex->tab->empty;
2479 static void *context_lex_save(struct isl_context *context)
2481 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2482 struct isl_tab_undo *snap;
2484 snap = isl_tab_snap(clex->tab);
2485 if (isl_tab_push_basis(clex->tab) < 0)
2486 return NULL;
2487 if (isl_tab_save_samples(clex->tab) < 0)
2488 return NULL;
2490 return snap;
2493 static void context_lex_restore(struct isl_context *context, void *save)
2495 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2496 if (isl_tab_rollback(clex->tab, (struct isl_tab_undo *)save) < 0) {
2497 isl_tab_free(clex->tab);
2498 clex->tab = NULL;
2502 static void context_lex_discard(void *save)
2506 static int context_lex_is_ok(struct isl_context *context)
2508 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2509 return !!clex->tab;
2512 /* For each variable in the context tableau, check if the variable can
2513 * only attain non-negative values. If so, mark the parameter as non-negative
2514 * in the main tableau. This allows for a more direct identification of some
2515 * cases of violated constraints.
2517 static struct isl_tab *tab_detect_nonnegative_parameters(struct isl_tab *tab,
2518 struct isl_tab *context_tab)
2520 int i;
2521 struct isl_tab_undo *snap;
2522 struct isl_vec *ineq = NULL;
2523 struct isl_tab_var *var;
2524 int n;
2526 if (context_tab->n_var == 0)
2527 return tab;
2529 ineq = isl_vec_alloc(tab->mat->ctx, 1 + context_tab->n_var);
2530 if (!ineq)
2531 goto error;
2533 if (isl_tab_extend_cons(context_tab, 1) < 0)
2534 goto error;
2536 snap = isl_tab_snap(context_tab);
2538 n = 0;
2539 isl_seq_clr(ineq->el, ineq->size);
2540 for (i = 0; i < context_tab->n_var; ++i) {
2541 isl_int_set_si(ineq->el[1 + i], 1);
2542 if (isl_tab_add_ineq(context_tab, ineq->el) < 0)
2543 goto error;
2544 var = &context_tab->con[context_tab->n_con - 1];
2545 if (!context_tab->empty &&
2546 !isl_tab_min_at_most_neg_one(context_tab, var)) {
2547 int j = i;
2548 if (i >= tab->n_param)
2549 j = i - tab->n_param + tab->n_var - tab->n_div;
2550 tab->var[j].is_nonneg = 1;
2551 n++;
2553 isl_int_set_si(ineq->el[1 + i], 0);
2554 if (isl_tab_rollback(context_tab, snap) < 0)
2555 goto error;
2558 if (context_tab->M && n == context_tab->n_var) {
2559 context_tab->mat = isl_mat_drop_cols(context_tab->mat, 2, 1);
2560 context_tab->M = 0;
2563 isl_vec_free(ineq);
2564 return tab;
2565 error:
2566 isl_vec_free(ineq);
2567 isl_tab_free(tab);
2568 return NULL;
2571 static struct isl_tab *context_lex_detect_nonnegative_parameters(
2572 struct isl_context *context, struct isl_tab *tab)
2574 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2575 struct isl_tab_undo *snap;
2577 if (!tab)
2578 return NULL;
2580 snap = isl_tab_snap(clex->tab);
2581 if (isl_tab_push_basis(clex->tab) < 0)
2582 goto error;
2584 tab = tab_detect_nonnegative_parameters(tab, clex->tab);
2586 if (isl_tab_rollback(clex->tab, snap) < 0)
2587 goto error;
2589 return tab;
2590 error:
2591 isl_tab_free(tab);
2592 return NULL;
2595 static void context_lex_invalidate(struct isl_context *context)
2597 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2598 isl_tab_free(clex->tab);
2599 clex->tab = NULL;
2602 static __isl_null struct isl_context *context_lex_free(
2603 struct isl_context *context)
2605 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2606 isl_tab_free(clex->tab);
2607 free(clex);
2609 return NULL;
2612 struct isl_context_op isl_context_lex_op = {
2613 context_lex_detect_nonnegative_parameters,
2614 context_lex_peek_basic_set,
2615 context_lex_peek_tab,
2616 context_lex_add_eq,
2617 context_lex_add_ineq,
2618 context_lex_ineq_sign,
2619 context_lex_test_ineq,
2620 context_lex_get_div,
2621 context_lex_insert_div,
2622 context_lex_detect_equalities,
2623 context_lex_best_split,
2624 context_lex_is_empty,
2625 context_lex_is_ok,
2626 context_lex_save,
2627 context_lex_restore,
2628 context_lex_discard,
2629 context_lex_invalidate,
2630 context_lex_free,
2633 static struct isl_tab *context_tab_for_lexmin(struct isl_basic_set *bset)
2635 struct isl_tab *tab;
2637 if (!bset)
2638 return NULL;
2639 tab = tab_for_lexmin(bset_to_bmap(bset), NULL, 1, 0);
2640 if (isl_tab_track_bset(tab, bset) < 0)
2641 goto error;
2642 tab = isl_tab_init_samples(tab);
2643 return tab;
2644 error:
2645 isl_tab_free(tab);
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.
4174 * Furthermore, make sure that the known integer divisions
4175 * appear before any unknown integer division because the solution
4176 * may depend on the known integer divisions, while anything that
4177 * depends on any variable starting from the first unknown integer
4178 * division is ignored in sol_pma_add.
4180 static struct isl_sol *basic_map_partial_lexopt_base_sol(
4181 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4182 __isl_give isl_set **empty, int max,
4183 struct isl_sol *(*init)(__isl_keep isl_basic_map *bmap,
4184 __isl_take isl_basic_set *dom, int track_empty, int max))
4186 struct isl_tab *tab;
4187 struct isl_sol *sol = NULL;
4188 struct isl_context *context;
4190 if (dom->n_div) {
4191 dom = isl_basic_set_sort_divs(dom);
4192 bmap = align_context_divs(bmap, dom);
4194 sol = init(bmap, dom, !!empty, max);
4195 if (!sol)
4196 goto error;
4198 context = sol->context;
4199 if (isl_basic_set_plain_is_empty(context->op->peek_basic_set(context)))
4200 /* nothing */;
4201 else if (isl_basic_map_plain_is_empty(bmap)) {
4202 if (sol->add_empty)
4203 sol->add_empty(sol,
4204 isl_basic_set_copy(context->op->peek_basic_set(context)));
4205 } else {
4206 tab = tab_for_lexmin(bmap,
4207 context->op->peek_basic_set(context), 1, max);
4208 tab = context->op->detect_nonnegative_parameters(context, tab);
4209 find_solutions_main(sol, tab);
4211 if (sol->error)
4212 goto error;
4214 isl_basic_map_free(bmap);
4215 return sol;
4216 error:
4217 sol_free(sol);
4218 isl_basic_map_free(bmap);
4219 return NULL;
4222 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4223 * some obvious symmetries.
4225 * We call basic_map_partial_lexopt_base_sol and extract the results.
4227 static __isl_give isl_map *basic_map_partial_lexopt_base(
4228 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4229 __isl_give isl_set **empty, int max)
4231 isl_map *result = NULL;
4232 struct isl_sol *sol;
4233 struct isl_sol_map *sol_map;
4235 sol = basic_map_partial_lexopt_base_sol(bmap, dom, empty, max,
4236 &sol_map_init);
4237 if (!sol)
4238 return NULL;
4239 sol_map = (struct isl_sol_map *) sol;
4241 result = isl_map_copy(sol_map->map);
4242 if (empty)
4243 *empty = isl_set_copy(sol_map->empty);
4244 sol_free(&sol_map->sol);
4245 return result;
4248 /* Return a count of the number of occurrences of the "n" first
4249 * variables in the inequality constraints of "bmap".
4251 static __isl_give int *count_occurrences(__isl_keep isl_basic_map *bmap,
4252 int n)
4254 int i, j;
4255 isl_ctx *ctx;
4256 int *occurrences;
4258 if (!bmap)
4259 return NULL;
4260 ctx = isl_basic_map_get_ctx(bmap);
4261 occurrences = isl_calloc_array(ctx, int, n);
4262 if (!occurrences)
4263 return NULL;
4265 for (i = 0; i < bmap->n_ineq; ++i) {
4266 for (j = 0; j < n; ++j) {
4267 if (!isl_int_is_zero(bmap->ineq[i][1 + j]))
4268 occurrences[j]++;
4272 return occurrences;
4275 /* Do all of the "n" variables with non-zero coefficients in "c"
4276 * occur in exactly a single constraint.
4277 * "occurrences" is an array of length "n" containing the number
4278 * of occurrences of each of the variables in the inequality constraints.
4280 static int single_occurrence(int n, isl_int *c, int *occurrences)
4282 int i;
4284 for (i = 0; i < n; ++i) {
4285 if (isl_int_is_zero(c[i]))
4286 continue;
4287 if (occurrences[i] != 1)
4288 return 0;
4291 return 1;
4294 /* Do all of the "n" initial variables that occur in inequality constraint
4295 * "ineq" of "bmap" only occur in that constraint?
4297 static int all_single_occurrence(__isl_keep isl_basic_map *bmap, int ineq,
4298 int n)
4300 int i, j;
4302 for (i = 0; i < n; ++i) {
4303 if (isl_int_is_zero(bmap->ineq[ineq][1 + i]))
4304 continue;
4305 for (j = 0; j < bmap->n_ineq; ++j) {
4306 if (j == ineq)
4307 continue;
4308 if (!isl_int_is_zero(bmap->ineq[j][1 + i]))
4309 return 0;
4313 return 1;
4316 /* Structure used during detection of parallel constraints.
4317 * n_in: number of "input" variables: isl_dim_param + isl_dim_in
4318 * n_out: number of "output" variables: isl_dim_out + isl_dim_div
4319 * val: the coefficients of the output variables
4321 struct isl_constraint_equal_info {
4322 isl_basic_map *bmap;
4323 unsigned n_in;
4324 unsigned n_out;
4325 isl_int *val;
4328 /* Check whether the coefficients of the output variables
4329 * of the constraint in "entry" are equal to info->val.
4331 static int constraint_equal(const void *entry, const void *val)
4333 isl_int **row = (isl_int **)entry;
4334 const struct isl_constraint_equal_info *info = val;
4336 return isl_seq_eq((*row) + 1 + info->n_in, info->val, info->n_out);
4339 /* Check whether "bmap" has a pair of constraints that have
4340 * the same coefficients for the output variables.
4341 * Note that the coefficients of the existentially quantified
4342 * variables need to be zero since the existentially quantified
4343 * of the result are usually not the same as those of the input.
4344 * Furthermore, check that each of the input variables that occur
4345 * in those constraints does not occur in any other constraint.
4346 * If so, return 1 and return the row indices of the two constraints
4347 * in *first and *second.
4349 static int parallel_constraints(__isl_keep isl_basic_map *bmap,
4350 int *first, int *second)
4352 int i;
4353 isl_ctx *ctx;
4354 int *occurrences = NULL;
4355 struct isl_hash_table *table = NULL;
4356 struct isl_hash_table_entry *entry;
4357 struct isl_constraint_equal_info info;
4358 unsigned n_out;
4359 unsigned n_div;
4361 ctx = isl_basic_map_get_ctx(bmap);
4362 table = isl_hash_table_alloc(ctx, bmap->n_ineq);
4363 if (!table)
4364 goto error;
4366 info.n_in = isl_basic_map_dim(bmap, isl_dim_param) +
4367 isl_basic_map_dim(bmap, isl_dim_in);
4368 occurrences = count_occurrences(bmap, info.n_in);
4369 if (info.n_in && !occurrences)
4370 goto error;
4371 info.bmap = bmap;
4372 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4373 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4374 info.n_out = n_out + n_div;
4375 for (i = 0; i < bmap->n_ineq; ++i) {
4376 uint32_t hash;
4378 info.val = bmap->ineq[i] + 1 + info.n_in;
4379 if (isl_seq_first_non_zero(info.val, n_out) < 0)
4380 continue;
4381 if (isl_seq_first_non_zero(info.val + n_out, n_div) >= 0)
4382 continue;
4383 if (!single_occurrence(info.n_in, bmap->ineq[i] + 1,
4384 occurrences))
4385 continue;
4386 hash = isl_seq_get_hash(info.val, info.n_out);
4387 entry = isl_hash_table_find(ctx, table, hash,
4388 constraint_equal, &info, 1);
4389 if (!entry)
4390 goto error;
4391 if (entry->data)
4392 break;
4393 entry->data = &bmap->ineq[i];
4396 if (i < bmap->n_ineq) {
4397 *first = ((isl_int **)entry->data) - bmap->ineq;
4398 *second = i;
4401 isl_hash_table_free(ctx, table);
4402 free(occurrences);
4404 return i < bmap->n_ineq;
4405 error:
4406 isl_hash_table_free(ctx, table);
4407 free(occurrences);
4408 return -1;
4411 /* Given a set of upper bounds in "var", add constraints to "bset"
4412 * that make the i-th bound smallest.
4414 * In particular, if there are n bounds b_i, then add the constraints
4416 * b_i <= b_j for j > i
4417 * b_i < b_j for j < i
4419 static __isl_give isl_basic_set *select_minimum(__isl_take isl_basic_set *bset,
4420 __isl_keep isl_mat *var, int i)
4422 isl_ctx *ctx;
4423 int j, k;
4425 ctx = isl_mat_get_ctx(var);
4427 for (j = 0; j < var->n_row; ++j) {
4428 if (j == i)
4429 continue;
4430 k = isl_basic_set_alloc_inequality(bset);
4431 if (k < 0)
4432 goto error;
4433 isl_seq_combine(bset->ineq[k], ctx->one, var->row[j],
4434 ctx->negone, var->row[i], var->n_col);
4435 isl_int_set_si(bset->ineq[k][var->n_col], 0);
4436 if (j < i)
4437 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
4440 bset = isl_basic_set_finalize(bset);
4442 return bset;
4443 error:
4444 isl_basic_set_free(bset);
4445 return NULL;
4448 /* Given a set of upper bounds on the last "input" variable m,
4449 * construct a set that assigns the minimal upper bound to m, i.e.,
4450 * construct a set that divides the space into cells where one
4451 * of the upper bounds is smaller than all the others and assign
4452 * this upper bound to m.
4454 * In particular, if there are n bounds b_i, then the result
4455 * consists of n basic sets, each one of the form
4457 * m = b_i
4458 * b_i <= b_j for j > i
4459 * b_i < b_j for j < i
4461 static __isl_give isl_set *set_minimum(__isl_take isl_space *dim,
4462 __isl_take isl_mat *var)
4464 int i, k;
4465 isl_basic_set *bset = NULL;
4466 isl_set *set = NULL;
4468 if (!dim || !var)
4469 goto error;
4471 set = isl_set_alloc_space(isl_space_copy(dim),
4472 var->n_row, ISL_SET_DISJOINT);
4474 for (i = 0; i < var->n_row; ++i) {
4475 bset = isl_basic_set_alloc_space(isl_space_copy(dim), 0,
4476 1, var->n_row - 1);
4477 k = isl_basic_set_alloc_equality(bset);
4478 if (k < 0)
4479 goto error;
4480 isl_seq_cpy(bset->eq[k], var->row[i], var->n_col);
4481 isl_int_set_si(bset->eq[k][var->n_col], -1);
4482 bset = select_minimum(bset, var, i);
4483 set = isl_set_add_basic_set(set, bset);
4486 isl_space_free(dim);
4487 isl_mat_free(var);
4488 return set;
4489 error:
4490 isl_basic_set_free(bset);
4491 isl_set_free(set);
4492 isl_space_free(dim);
4493 isl_mat_free(var);
4494 return NULL;
4497 /* Given that the last input variable of "bmap" represents the minimum
4498 * of the bounds in "cst", check whether we need to split the domain
4499 * based on which bound attains the minimum.
4501 * A split is needed when the minimum appears in an integer division
4502 * or in an equality. Otherwise, it is only needed if it appears in
4503 * an upper bound that is different from the upper bounds on which it
4504 * is defined.
4506 static int need_split_basic_map(__isl_keep isl_basic_map *bmap,
4507 __isl_keep isl_mat *cst)
4509 int i, j;
4510 unsigned total;
4511 unsigned pos;
4513 pos = cst->n_col - 1;
4514 total = isl_basic_map_dim(bmap, isl_dim_all);
4516 for (i = 0; i < bmap->n_div; ++i)
4517 if (!isl_int_is_zero(bmap->div[i][2 + pos]))
4518 return 1;
4520 for (i = 0; i < bmap->n_eq; ++i)
4521 if (!isl_int_is_zero(bmap->eq[i][1 + pos]))
4522 return 1;
4524 for (i = 0; i < bmap->n_ineq; ++i) {
4525 if (isl_int_is_nonneg(bmap->ineq[i][1 + pos]))
4526 continue;
4527 if (!isl_int_is_negone(bmap->ineq[i][1 + pos]))
4528 return 1;
4529 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + pos + 1,
4530 total - pos - 1) >= 0)
4531 return 1;
4533 for (j = 0; j < cst->n_row; ++j)
4534 if (isl_seq_eq(bmap->ineq[i], cst->row[j], cst->n_col))
4535 break;
4536 if (j >= cst->n_row)
4537 return 1;
4540 return 0;
4543 /* Given that the last set variable of "bset" represents the minimum
4544 * of the bounds in "cst", check whether we need to split the domain
4545 * based on which bound attains the minimum.
4547 * We simply call need_split_basic_map here. This is safe because
4548 * the position of the minimum is computed from "cst" and not
4549 * from "bmap".
4551 static int need_split_basic_set(__isl_keep isl_basic_set *bset,
4552 __isl_keep isl_mat *cst)
4554 return need_split_basic_map(bset_to_bmap(bset), cst);
4557 /* Given that the last set variable of "set" represents the minimum
4558 * of the bounds in "cst", check whether we need to split the domain
4559 * based on which bound attains the minimum.
4561 static int need_split_set(__isl_keep isl_set *set, __isl_keep isl_mat *cst)
4563 int i;
4565 for (i = 0; i < set->n; ++i)
4566 if (need_split_basic_set(set->p[i], cst))
4567 return 1;
4569 return 0;
4572 /* Given a set of which the last set variable is the minimum
4573 * of the bounds in "cst", split each basic set in the set
4574 * in pieces where one of the bounds is (strictly) smaller than the others.
4575 * This subdivision is given in "min_expr".
4576 * The variable is subsequently projected out.
4578 * We only do the split when it is needed.
4579 * For example if the last input variable m = min(a,b) and the only
4580 * constraints in the given basic set are lower bounds on m,
4581 * i.e., l <= m = min(a,b), then we can simply project out m
4582 * to obtain l <= a and l <= b, without having to split on whether
4583 * m is equal to a or b.
4585 static __isl_give isl_set *split(__isl_take isl_set *empty,
4586 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
4588 int n_in;
4589 int i;
4590 isl_space *dim;
4591 isl_set *res;
4593 if (!empty || !min_expr || !cst)
4594 goto error;
4596 n_in = isl_set_dim(empty, isl_dim_set);
4597 dim = isl_set_get_space(empty);
4598 dim = isl_space_drop_dims(dim, isl_dim_set, n_in - 1, 1);
4599 res = isl_set_empty(dim);
4601 for (i = 0; i < empty->n; ++i) {
4602 isl_set *set;
4604 set = isl_set_from_basic_set(isl_basic_set_copy(empty->p[i]));
4605 if (need_split_basic_set(empty->p[i], cst))
4606 set = isl_set_intersect(set, isl_set_copy(min_expr));
4607 set = isl_set_remove_dims(set, isl_dim_set, n_in - 1, 1);
4609 res = isl_set_union_disjoint(res, set);
4612 isl_set_free(empty);
4613 isl_set_free(min_expr);
4614 isl_mat_free(cst);
4615 return res;
4616 error:
4617 isl_set_free(empty);
4618 isl_set_free(min_expr);
4619 isl_mat_free(cst);
4620 return NULL;
4623 /* Given a map of which the last input variable is the minimum
4624 * of the bounds in "cst", split each basic set in the set
4625 * in pieces where one of the bounds is (strictly) smaller than the others.
4626 * This subdivision is given in "min_expr".
4627 * The variable is subsequently projected out.
4629 * The implementation is essentially the same as that of "split".
4631 static __isl_give isl_map *split_domain(__isl_take isl_map *opt,
4632 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
4634 int n_in;
4635 int i;
4636 isl_space *dim;
4637 isl_map *res;
4639 if (!opt || !min_expr || !cst)
4640 goto error;
4642 n_in = isl_map_dim(opt, isl_dim_in);
4643 dim = isl_map_get_space(opt);
4644 dim = isl_space_drop_dims(dim, isl_dim_in, n_in - 1, 1);
4645 res = isl_map_empty(dim);
4647 for (i = 0; i < opt->n; ++i) {
4648 isl_map *map;
4650 map = isl_map_from_basic_map(isl_basic_map_copy(opt->p[i]));
4651 if (need_split_basic_map(opt->p[i], cst))
4652 map = isl_map_intersect_domain(map,
4653 isl_set_copy(min_expr));
4654 map = isl_map_remove_dims(map, isl_dim_in, n_in - 1, 1);
4656 res = isl_map_union_disjoint(res, map);
4659 isl_map_free(opt);
4660 isl_set_free(min_expr);
4661 isl_mat_free(cst);
4662 return res;
4663 error:
4664 isl_map_free(opt);
4665 isl_set_free(min_expr);
4666 isl_mat_free(cst);
4667 return NULL;
4670 static __isl_give isl_map *basic_map_partial_lexopt(
4671 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4672 __isl_give isl_set **empty, int max);
4674 /* This function is called from basic_map_partial_lexopt_symm.
4675 * The last variable of "bmap" and "dom" corresponds to the minimum
4676 * of the bounds in "cst". "map_space" is the space of the original
4677 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
4678 * is the space of the original domain.
4680 * We recursively call basic_map_partial_lexopt and then plug in
4681 * the definition of the minimum in the result.
4683 static __isl_give isl_map *basic_map_partial_lexopt_symm_core(
4684 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4685 __isl_give isl_set **empty, int max, __isl_take isl_mat *cst,
4686 __isl_take isl_space *map_space, __isl_take isl_space *set_space)
4688 isl_map *opt;
4689 isl_set *min_expr;
4691 min_expr = set_minimum(isl_basic_set_get_space(dom), isl_mat_copy(cst));
4693 opt = basic_map_partial_lexopt(bmap, dom, empty, max);
4695 if (empty) {
4696 *empty = split(*empty,
4697 isl_set_copy(min_expr), isl_mat_copy(cst));
4698 *empty = isl_set_reset_space(*empty, set_space);
4701 opt = split_domain(opt, min_expr, cst);
4702 opt = isl_map_reset_space(opt, map_space);
4704 return opt;
4707 /* Extract a domain from "bmap" for the purpose of computing
4708 * a lexicographic optimum.
4710 * This function is only called when the caller wants to compute a full
4711 * lexicographic optimum, i.e., without specifying a domain. In this case,
4712 * the caller is not interested in the part of the domain space where
4713 * there is no solution and the domain can be initialized to those constraints
4714 * of "bmap" that only involve the parameters and the input dimensions.
4715 * This relieves the parametric programming engine from detecting those
4716 * inequalities and transferring them to the context. More importantly,
4717 * it ensures that those inequalities are transferred first and not
4718 * intermixed with inequalities that actually split the domain.
4720 * If the caller does not require the absence of existentially quantified
4721 * variables in the result (i.e., if ISL_OPT_QE is not set in "flags"),
4722 * then the actual domain of "bmap" can be used. This ensures that
4723 * the domain does not need to be split at all just to separate out
4724 * pieces of the domain that do not have a solution from piece that do.
4725 * This domain cannot be used in general because it may involve
4726 * (unknown) existentially quantified variables which will then also
4727 * appear in the solution.
4729 static __isl_give isl_basic_set *extract_domain(__isl_keep isl_basic_map *bmap,
4730 unsigned flags)
4732 int n_div;
4733 int n_out;
4735 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4736 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4737 bmap = isl_basic_map_copy(bmap);
4738 if (ISL_FL_ISSET(flags, ISL_OPT_QE)) {
4739 bmap = isl_basic_map_drop_constraints_involving_dims(bmap,
4740 isl_dim_div, 0, n_div);
4741 bmap = isl_basic_map_drop_constraints_involving_dims(bmap,
4742 isl_dim_out, 0, n_out);
4744 return isl_basic_map_domain(bmap);
4747 #undef TYPE
4748 #define TYPE isl_map
4749 #undef SUFFIX
4750 #define SUFFIX
4751 #include "isl_tab_lexopt_templ.c"
4753 struct isl_sol_for {
4754 struct isl_sol sol;
4755 int (*fn)(__isl_take isl_basic_set *dom,
4756 __isl_take isl_aff_list *list, void *user);
4757 void *user;
4760 static void sol_for_free(struct isl_sol_for *sol_for)
4762 if (!sol_for)
4763 return;
4764 if (sol_for->sol.context)
4765 sol_for->sol.context->op->free(sol_for->sol.context);
4766 free(sol_for);
4769 static void sol_for_free_wrap(struct isl_sol *sol)
4771 sol_for_free((struct isl_sol_for *)sol);
4774 /* Add the solution identified by the tableau and the context tableau.
4776 * See documentation of sol_add for more details.
4778 * Instead of constructing a basic map, this function calls a user
4779 * defined function with the current context as a basic set and
4780 * a list of affine expressions representing the relation between
4781 * the input and output. The space over which the affine expressions
4782 * are defined is the same as that of the domain. The number of
4783 * affine expressions in the list is equal to the number of output variables.
4785 static void sol_for_add(struct isl_sol_for *sol,
4786 struct isl_basic_set *dom, struct isl_mat *M)
4788 int i;
4789 isl_ctx *ctx;
4790 isl_local_space *ls;
4791 isl_aff *aff;
4792 isl_aff_list *list;
4794 if (sol->sol.error || !dom || !M)
4795 goto error;
4797 ctx = isl_basic_set_get_ctx(dom);
4798 ls = isl_basic_set_get_local_space(dom);
4799 list = isl_aff_list_alloc(ctx, M->n_row - 1);
4800 for (i = 1; i < M->n_row; ++i) {
4801 aff = isl_aff_alloc(isl_local_space_copy(ls));
4802 if (aff) {
4803 isl_int_set(aff->v->el[0], M->row[0][0]);
4804 isl_seq_cpy(aff->v->el + 1, M->row[i], M->n_col);
4806 aff = isl_aff_normalize(aff);
4807 list = isl_aff_list_add(list, aff);
4809 isl_local_space_free(ls);
4811 dom = isl_basic_set_finalize(dom);
4813 if (sol->fn(isl_basic_set_copy(dom), list, sol->user) < 0)
4814 goto error;
4816 isl_basic_set_free(dom);
4817 isl_mat_free(M);
4818 return;
4819 error:
4820 isl_basic_set_free(dom);
4821 isl_mat_free(M);
4822 sol->sol.error = 1;
4825 static void sol_for_add_wrap(struct isl_sol *sol,
4826 struct isl_basic_set *dom, struct isl_mat *M)
4828 sol_for_add((struct isl_sol_for *)sol, dom, M);
4831 static struct isl_sol_for *sol_for_init(struct isl_basic_map *bmap, int max,
4832 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4833 void *user),
4834 void *user)
4836 struct isl_sol_for *sol_for = NULL;
4837 isl_space *dom_dim;
4838 struct isl_basic_set *dom = NULL;
4840 sol_for = isl_calloc_type(bmap->ctx, struct isl_sol_for);
4841 if (!sol_for)
4842 goto error;
4844 dom_dim = isl_space_domain(isl_space_copy(bmap->dim));
4845 dom = isl_basic_set_universe(dom_dim);
4847 sol_for->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
4848 sol_for->sol.dec_level.callback.run = &sol_dec_level_wrap;
4849 sol_for->sol.dec_level.sol = &sol_for->sol;
4850 sol_for->fn = fn;
4851 sol_for->user = user;
4852 sol_for->sol.max = max;
4853 sol_for->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
4854 sol_for->sol.add = &sol_for_add_wrap;
4855 sol_for->sol.add_empty = NULL;
4856 sol_for->sol.free = &sol_for_free_wrap;
4858 sol_for->sol.context = isl_context_alloc(dom);
4859 if (!sol_for->sol.context)
4860 goto error;
4862 isl_basic_set_free(dom);
4863 return sol_for;
4864 error:
4865 isl_basic_set_free(dom);
4866 sol_for_free(sol_for);
4867 return NULL;
4870 static void sol_for_find_solutions(struct isl_sol_for *sol_for,
4871 struct isl_tab *tab)
4873 find_solutions_main(&sol_for->sol, tab);
4876 int isl_basic_map_foreach_lexopt(__isl_keep isl_basic_map *bmap, int max,
4877 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4878 void *user),
4879 void *user)
4881 struct isl_sol_for *sol_for = NULL;
4883 bmap = isl_basic_map_copy(bmap);
4884 bmap = isl_basic_map_detect_equalities(bmap);
4885 if (!bmap)
4886 return -1;
4888 sol_for = sol_for_init(bmap, max, fn, user);
4889 if (!sol_for)
4890 goto error;
4892 if (isl_basic_map_plain_is_empty(bmap))
4893 /* nothing */;
4894 else {
4895 struct isl_tab *tab;
4896 struct isl_context *context = sol_for->sol.context;
4897 tab = tab_for_lexmin(bmap,
4898 context->op->peek_basic_set(context), 1, max);
4899 tab = context->op->detect_nonnegative_parameters(context, tab);
4900 sol_for_find_solutions(sol_for, tab);
4901 if (sol_for->sol.error)
4902 goto error;
4905 sol_free(&sol_for->sol);
4906 isl_basic_map_free(bmap);
4907 return 0;
4908 error:
4909 sol_free(&sol_for->sol);
4910 isl_basic_map_free(bmap);
4911 return -1;
4914 int isl_basic_set_foreach_lexopt(__isl_keep isl_basic_set *bset, int max,
4915 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4916 void *user),
4917 void *user)
4919 return isl_basic_map_foreach_lexopt(bset, max, fn, user);
4922 /* Check if the given sequence of len variables starting at pos
4923 * represents a trivial (i.e., zero) solution.
4924 * The variables are assumed to be non-negative and to come in pairs,
4925 * with each pair representing a variable of unrestricted sign.
4926 * The solution is trivial if each such pair in the sequence consists
4927 * of two identical values, meaning that the variable being represented
4928 * has value zero.
4930 static int region_is_trivial(struct isl_tab *tab, int pos, int len)
4932 int i;
4934 if (len == 0)
4935 return 0;
4937 for (i = 0; i < len; i += 2) {
4938 int neg_row;
4939 int pos_row;
4941 neg_row = tab->var[pos + i].is_row ?
4942 tab->var[pos + i].index : -1;
4943 pos_row = tab->var[pos + i + 1].is_row ?
4944 tab->var[pos + i + 1].index : -1;
4946 if ((neg_row < 0 ||
4947 isl_int_is_zero(tab->mat->row[neg_row][1])) &&
4948 (pos_row < 0 ||
4949 isl_int_is_zero(tab->mat->row[pos_row][1])))
4950 continue;
4952 if (neg_row < 0 || pos_row < 0)
4953 return 0;
4954 if (isl_int_ne(tab->mat->row[neg_row][1],
4955 tab->mat->row[pos_row][1]))
4956 return 0;
4959 return 1;
4962 /* Return the index of the first trivial region or -1 if all regions
4963 * are non-trivial.
4965 static int first_trivial_region(struct isl_tab *tab,
4966 int n_region, struct isl_region *region)
4968 int i;
4970 for (i = 0; i < n_region; ++i) {
4971 if (region_is_trivial(tab, region[i].pos, region[i].len))
4972 return i;
4975 return -1;
4978 /* Check if the solution is optimal, i.e., whether the first
4979 * n_op entries are zero.
4981 static int is_optimal(__isl_keep isl_vec *sol, int n_op)
4983 int i;
4985 for (i = 0; i < n_op; ++i)
4986 if (!isl_int_is_zero(sol->el[1 + i]))
4987 return 0;
4988 return 1;
4991 /* Add constraints to "tab" that ensure that any solution is significantly
4992 * better than that represented by "sol". That is, find the first
4993 * relevant (within first n_op) non-zero coefficient and force it (along
4994 * with all previous coefficients) to be zero.
4995 * If the solution is already optimal (all relevant coefficients are zero),
4996 * then just mark the table as empty.
4997 * "n_zero" is the number of coefficients that have been forced zero
4998 * by previous calls to this function at the same level.
4999 * Return the updated number of forced zero coefficients or -1 on error.
5001 * This function assumes that at least 2 * (n_op - n_zero) more rows and
5002 * at least 2 * (n_op - n_zero) more elements in the constraint array
5003 * are available in the tableau.
5005 static int force_better_solution(struct isl_tab *tab,
5006 __isl_keep isl_vec *sol, int n_op, int n_zero)
5008 int i, n;
5009 isl_ctx *ctx;
5010 isl_vec *v = NULL;
5012 if (!sol)
5013 return -1;
5015 for (i = n_zero; i < n_op; ++i)
5016 if (!isl_int_is_zero(sol->el[1 + i]))
5017 break;
5019 if (i == n_op) {
5020 if (isl_tab_mark_empty(tab) < 0)
5021 return -1;
5022 return n_op;
5025 ctx = isl_vec_get_ctx(sol);
5026 v = isl_vec_alloc(ctx, 1 + tab->n_var);
5027 if (!v)
5028 return -1;
5030 n = i + 1;
5031 for (; i >= n_zero; --i) {
5032 v = isl_vec_clr(v);
5033 isl_int_set_si(v->el[1 + i], -1);
5034 if (add_lexmin_eq(tab, v->el) < 0)
5035 goto error;
5038 isl_vec_free(v);
5039 return n;
5040 error:
5041 isl_vec_free(v);
5042 return -1;
5045 /* Local data at each level of the backtracking procedure of
5046 * isl_tab_basic_set_non_trivial_lexmin.
5048 * "n_zero" is the number of initial coordinates that have already
5049 * been forced to be zero at this level.
5051 struct isl_trivial {
5052 int update;
5053 int n_zero;
5054 int region;
5055 int side;
5056 struct isl_tab_undo *snap;
5059 /* Return the lexicographically smallest non-trivial solution of the
5060 * given ILP problem.
5062 * All variables are assumed to be non-negative.
5064 * n_op is the number of initial coordinates to optimize.
5065 * That is, once a solution has been found, we will only continue looking
5066 * for solution that result in significantly better values for those
5067 * initial coordinates. That is, we only continue looking for solutions
5068 * that increase the number of initial zeros in this sequence.
5070 * A solution is non-trivial, if it is non-trivial on each of the
5071 * specified regions. Each region represents a sequence of pairs
5072 * of variables. A solution is non-trivial on such a region if
5073 * at least one of these pairs consists of different values, i.e.,
5074 * such that the non-negative variable represented by the pair is non-zero.
5076 * Whenever a conflict is encountered, all constraints involved are
5077 * reported to the caller through a call to "conflict".
5079 * We perform a simple branch-and-bound backtracking search.
5080 * Each level in the search represents initially trivial region that is forced
5081 * to be non-trivial.
5082 * At each level we consider n cases, where n is the length of the region.
5083 * In terms of the n/2 variables of unrestricted signs being encoded by
5084 * the region, we consider the cases
5085 * x_0 >= 1
5086 * x_0 <= -1
5087 * x_0 = 0 and x_1 >= 1
5088 * x_0 = 0 and x_1 <= -1
5089 * x_0 = 0 and x_1 = 0 and x_2 >= 1
5090 * x_0 = 0 and x_1 = 0 and x_2 <= -1
5091 * ...
5092 * The cases are considered in this order, assuming that each pair
5093 * x_i_a x_i_b represents the value x_i_b - x_i_a.
5094 * That is, x_0 >= 1 is enforced by adding the constraint
5095 * x_0_b - x_0_a >= 1
5097 __isl_give isl_vec *isl_tab_basic_set_non_trivial_lexmin(
5098 __isl_take isl_basic_set *bset, int n_op, int n_region,
5099 struct isl_region *region,
5100 int (*conflict)(int con, void *user), void *user)
5102 int i, j;
5103 int r;
5104 isl_ctx *ctx;
5105 isl_vec *v = NULL;
5106 isl_vec *sol = NULL;
5107 struct isl_tab *tab;
5108 struct isl_trivial *triv = NULL;
5109 int level, init;
5111 if (!bset)
5112 return NULL;
5114 ctx = isl_basic_set_get_ctx(bset);
5115 sol = isl_vec_alloc(ctx, 0);
5117 tab = tab_for_lexmin(bset, NULL, 0, 0);
5118 if (!tab)
5119 goto error;
5120 tab->conflict = conflict;
5121 tab->conflict_user = user;
5123 v = isl_vec_alloc(ctx, 1 + tab->n_var);
5124 triv = isl_calloc_array(ctx, struct isl_trivial, n_region);
5125 if (!v || (n_region && !triv))
5126 goto error;
5128 level = 0;
5129 init = 1;
5131 while (level >= 0) {
5132 int side, base;
5134 if (init) {
5135 tab = cut_to_integer_lexmin(tab, CUT_ONE);
5136 if (!tab)
5137 goto error;
5138 if (tab->empty)
5139 goto backtrack;
5140 r = first_trivial_region(tab, n_region, region);
5141 if (r < 0) {
5142 for (i = 0; i < level; ++i)
5143 triv[i].update = 1;
5144 isl_vec_free(sol);
5145 sol = isl_tab_get_sample_value(tab);
5146 if (!sol)
5147 goto error;
5148 if (is_optimal(sol, n_op))
5149 break;
5150 goto backtrack;
5152 if (level >= n_region)
5153 isl_die(ctx, isl_error_internal,
5154 "nesting level too deep", goto error);
5155 if (isl_tab_extend_cons(tab,
5156 2 * region[r].len + 2 * n_op) < 0)
5157 goto error;
5158 triv[level].region = r;
5159 triv[level].side = 0;
5160 triv[level].update = 0;
5161 triv[level].n_zero = 0;
5164 r = triv[level].region;
5165 side = triv[level].side;
5166 base = 2 * (side/2);
5168 if (side >= region[r].len) {
5169 backtrack:
5170 level--;
5171 init = 0;
5172 if (level >= 0)
5173 if (isl_tab_rollback(tab, triv[level].snap) < 0)
5174 goto error;
5175 continue;
5178 if (triv[level].update) {
5179 triv[level].n_zero = force_better_solution(tab, sol,
5180 n_op, triv[level].n_zero);
5181 if (triv[level].n_zero < 0)
5182 goto error;
5183 triv[level].update = 0;
5186 if (side == base && base >= 2) {
5187 for (j = base - 2; j < base; ++j) {
5188 v = isl_vec_clr(v);
5189 isl_int_set_si(v->el[1 + region[r].pos + j], 1);
5190 if (add_lexmin_eq(tab, v->el) < 0)
5191 goto error;
5195 triv[level].snap = isl_tab_snap(tab);
5196 if (isl_tab_push_basis(tab) < 0)
5197 goto error;
5199 v = isl_vec_clr(v);
5200 isl_int_set_si(v->el[0], -1);
5201 isl_int_set_si(v->el[1 + region[r].pos + side], -1);
5202 isl_int_set_si(v->el[1 + region[r].pos + (side ^ 1)], 1);
5203 tab = add_lexmin_ineq(tab, v->el);
5205 triv[level].side++;
5206 level++;
5207 init = 1;
5210 free(triv);
5211 isl_vec_free(v);
5212 isl_tab_free(tab);
5213 isl_basic_set_free(bset);
5215 return sol;
5216 error:
5217 free(triv);
5218 isl_vec_free(v);
5219 isl_tab_free(tab);
5220 isl_basic_set_free(bset);
5221 isl_vec_free(sol);
5222 return NULL;
5225 /* Wrapper for a tableau that is used for computing
5226 * the lexicographically smallest rational point of a non-negative set.
5227 * This point is represented by the sample value of "tab",
5228 * unless "tab" is empty.
5230 struct isl_tab_lexmin {
5231 isl_ctx *ctx;
5232 struct isl_tab *tab;
5235 /* Free "tl" and return NULL.
5237 __isl_null isl_tab_lexmin *isl_tab_lexmin_free(__isl_take isl_tab_lexmin *tl)
5239 if (!tl)
5240 return NULL;
5241 isl_ctx_deref(tl->ctx);
5242 isl_tab_free(tl->tab);
5243 free(tl);
5245 return NULL;
5248 /* Construct an isl_tab_lexmin for computing
5249 * the lexicographically smallest rational point in "bset",
5250 * assuming that all variables are non-negative.
5252 __isl_give isl_tab_lexmin *isl_tab_lexmin_from_basic_set(
5253 __isl_take isl_basic_set *bset)
5255 isl_ctx *ctx;
5256 isl_tab_lexmin *tl;
5258 if (!bset)
5259 return NULL;
5261 ctx = isl_basic_set_get_ctx(bset);
5262 tl = isl_calloc_type(ctx, struct isl_tab_lexmin);
5263 if (!tl)
5264 goto error;
5265 tl->ctx = ctx;
5266 isl_ctx_ref(ctx);
5267 tl->tab = tab_for_lexmin(bset, NULL, 0, 0);
5268 isl_basic_set_free(bset);
5269 if (!tl->tab)
5270 return isl_tab_lexmin_free(tl);
5271 return tl;
5272 error:
5273 isl_basic_set_free(bset);
5274 isl_tab_lexmin_free(tl);
5275 return NULL;
5278 /* Return the dimension of the set represented by "tl".
5280 int isl_tab_lexmin_dim(__isl_keep isl_tab_lexmin *tl)
5282 return tl ? tl->tab->n_var : -1;
5285 /* Add the equality with coefficients "eq" to "tl", updating the optimal
5286 * solution if needed.
5287 * The equality is added as two opposite inequality constraints.
5289 __isl_give isl_tab_lexmin *isl_tab_lexmin_add_eq(__isl_take isl_tab_lexmin *tl,
5290 isl_int *eq)
5292 unsigned n_var;
5294 if (!tl || !eq)
5295 return isl_tab_lexmin_free(tl);
5297 if (isl_tab_extend_cons(tl->tab, 2) < 0)
5298 return isl_tab_lexmin_free(tl);
5299 n_var = tl->tab->n_var;
5300 isl_seq_neg(eq, eq, 1 + n_var);
5301 tl->tab = add_lexmin_ineq(tl->tab, eq);
5302 isl_seq_neg(eq, eq, 1 + n_var);
5303 tl->tab = add_lexmin_ineq(tl->tab, eq);
5305 if (!tl->tab)
5306 return isl_tab_lexmin_free(tl);
5308 return tl;
5311 /* Return the lexicographically smallest rational point in the basic set
5312 * from which "tl" was constructed.
5313 * If the original input was empty, then return a zero-length vector.
5315 __isl_give isl_vec *isl_tab_lexmin_get_solution(__isl_keep isl_tab_lexmin *tl)
5317 if (!tl)
5318 return NULL;
5319 if (tl->tab->empty)
5320 return isl_vec_alloc(tl->ctx, 0);
5321 else
5322 return isl_tab_get_sample_value(tl->tab);
5325 /* Return the lexicographically smallest rational point in "bset",
5326 * assuming that all variables are non-negative.
5327 * If "bset" is empty, then return a zero-length vector.
5329 __isl_give isl_vec *isl_tab_basic_set_non_neg_lexmin(
5330 __isl_take isl_basic_set *bset)
5332 isl_tab_lexmin *tl;
5333 isl_vec *sol;
5335 tl = isl_tab_lexmin_from_basic_set(bset);
5336 sol = isl_tab_lexmin_get_solution(tl);
5337 isl_tab_lexmin_free(tl);
5338 return sol;
5341 struct isl_sol_pma {
5342 struct isl_sol sol;
5343 isl_pw_multi_aff *pma;
5344 isl_set *empty;
5347 static void sol_pma_free(struct isl_sol_pma *sol_pma)
5349 if (!sol_pma)
5350 return;
5351 if (sol_pma->sol.context)
5352 sol_pma->sol.context->op->free(sol_pma->sol.context);
5353 isl_pw_multi_aff_free(sol_pma->pma);
5354 isl_set_free(sol_pma->empty);
5355 free(sol_pma);
5358 /* This function is called for parts of the context where there is
5359 * no solution, with "bset" corresponding to the context tableau.
5360 * Simply add the basic set to the set "empty".
5362 static void sol_pma_add_empty(struct isl_sol_pma *sol,
5363 __isl_take isl_basic_set *bset)
5365 if (!bset || !sol->empty)
5366 goto error;
5368 sol->empty = isl_set_grow(sol->empty, 1);
5369 bset = isl_basic_set_simplify(bset);
5370 bset = isl_basic_set_finalize(bset);
5371 sol->empty = isl_set_add_basic_set(sol->empty, bset);
5372 if (!sol->empty)
5373 sol->sol.error = 1;
5374 return;
5375 error:
5376 isl_basic_set_free(bset);
5377 sol->sol.error = 1;
5380 /* Check that the final columns of "M", starting at "first", are zero.
5382 static isl_stat check_final_columns_are_zero(__isl_keep isl_mat *M,
5383 unsigned first)
5385 int i;
5386 unsigned rows, cols, n;
5388 if (!M)
5389 return isl_stat_error;
5390 rows = isl_mat_rows(M);
5391 cols = isl_mat_cols(M);
5392 n = cols - first;
5393 for (i = 0; i < rows; ++i)
5394 if (isl_seq_first_non_zero(M->row[i] + first, n) != -1)
5395 isl_die(isl_mat_get_ctx(M), isl_error_internal,
5396 "final columns should be zero",
5397 return isl_stat_error);
5398 return isl_stat_ok;
5401 /* Set the affine expressions in "ma" according to the rows in "M", which
5402 * are defined over the local space "ls".
5403 * The matrix "M" may have extra (zero) columns beyond the number
5404 * of variables in "ls".
5406 static __isl_give isl_multi_aff *set_from_affine_matrix(
5407 __isl_take isl_multi_aff *ma, __isl_take isl_local_space *ls,
5408 __isl_take isl_mat *M)
5410 int i, dim;
5411 isl_aff *aff;
5413 if (!ma || !ls || !M)
5414 goto error;
5416 dim = isl_local_space_dim(ls, isl_dim_all);
5417 if (check_final_columns_are_zero(M, 1 + dim) < 0)
5418 goto error;
5419 for (i = 1; i < M->n_row; ++i) {
5420 aff = isl_aff_alloc(isl_local_space_copy(ls));
5421 if (aff) {
5422 isl_int_set(aff->v->el[0], M->row[0][0]);
5423 isl_seq_cpy(aff->v->el + 1, M->row[i], 1 + dim);
5425 aff = isl_aff_normalize(aff);
5426 ma = isl_multi_aff_set_aff(ma, i - 1, aff);
5428 isl_local_space_free(ls);
5429 isl_mat_free(M);
5431 return ma;
5432 error:
5433 isl_local_space_free(ls);
5434 isl_mat_free(M);
5435 isl_multi_aff_free(ma);
5436 return NULL;
5439 /* Given a basic set "dom" that represents the context and an affine
5440 * matrix "M" that maps the dimensions of the context to the
5441 * output variables, construct an isl_pw_multi_aff with a single
5442 * cell corresponding to "dom" and affine expressions copied from "M".
5444 * Note that the description of the initial context may have involved
5445 * existentially quantified variables, in which case they also appear
5446 * in "dom". These need to be removed before creating the affine
5447 * expression because an affine expression cannot be defined in terms
5448 * of existentially quantified variables without a known representation.
5449 * Since newly added integer divisions are inserted before these
5450 * existentially quantified variables, they are still in the final
5451 * positions and the corresponding final columns of "M" are zero
5452 * because align_context_divs adds the existentially quantified
5453 * variables of the context to the main tableau without any constraints and
5454 * any equality constraints that are added later on can only serve
5455 * to eliminate these existentially quantified variables.
5457 static void sol_pma_add(struct isl_sol_pma *sol,
5458 __isl_take isl_basic_set *dom, __isl_take isl_mat *M)
5460 isl_local_space *ls;
5461 isl_multi_aff *maff;
5462 isl_pw_multi_aff *pma;
5463 int n_div, n_known;
5465 n_div = isl_basic_set_dim(dom, isl_dim_div);
5466 n_known = n_div - sol->sol.context->n_unknown;
5468 maff = isl_multi_aff_alloc(isl_pw_multi_aff_get_space(sol->pma));
5469 ls = isl_basic_set_get_local_space(dom);
5470 ls = isl_local_space_drop_dims(ls, isl_dim_div,
5471 n_known, n_div - n_known);
5472 maff = set_from_affine_matrix(maff, ls, M);
5473 dom = isl_basic_set_simplify(dom);
5474 dom = isl_basic_set_finalize(dom);
5475 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(dom), maff);
5476 sol->pma = isl_pw_multi_aff_add_disjoint(sol->pma, pma);
5477 if (!sol->pma)
5478 sol->sol.error = 1;
5481 static void sol_pma_free_wrap(struct isl_sol *sol)
5483 sol_pma_free((struct isl_sol_pma *)sol);
5486 static void sol_pma_add_empty_wrap(struct isl_sol *sol,
5487 __isl_take isl_basic_set *bset)
5489 sol_pma_add_empty((struct isl_sol_pma *)sol, bset);
5492 static void sol_pma_add_wrap(struct isl_sol *sol,
5493 __isl_take isl_basic_set *dom, __isl_take isl_mat *M)
5495 sol_pma_add((struct isl_sol_pma *)sol, dom, M);
5498 /* Construct an isl_sol_pma structure for accumulating the solution.
5499 * If track_empty is set, then we also keep track of the parts
5500 * of the context where there is no solution.
5501 * If max is set, then we are solving a maximization, rather than
5502 * a minimization problem, which means that the variables in the
5503 * tableau have value "M - x" rather than "M + x".
5505 static struct isl_sol *sol_pma_init(__isl_keep isl_basic_map *bmap,
5506 __isl_take isl_basic_set *dom, int track_empty, int max)
5508 struct isl_sol_pma *sol_pma = NULL;
5510 if (!bmap)
5511 goto error;
5513 sol_pma = isl_calloc_type(bmap->ctx, struct isl_sol_pma);
5514 if (!sol_pma)
5515 goto error;
5517 sol_pma->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
5518 sol_pma->sol.dec_level.callback.run = &sol_dec_level_wrap;
5519 sol_pma->sol.dec_level.sol = &sol_pma->sol;
5520 sol_pma->sol.max = max;
5521 sol_pma->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
5522 sol_pma->sol.add = &sol_pma_add_wrap;
5523 sol_pma->sol.add_empty = track_empty ? &sol_pma_add_empty_wrap : NULL;
5524 sol_pma->sol.free = &sol_pma_free_wrap;
5525 sol_pma->pma = isl_pw_multi_aff_empty(isl_basic_map_get_space(bmap));
5526 if (!sol_pma->pma)
5527 goto error;
5529 sol_pma->sol.context = isl_context_alloc(dom);
5530 if (!sol_pma->sol.context)
5531 goto error;
5533 if (track_empty) {
5534 sol_pma->empty = isl_set_alloc_space(isl_basic_set_get_space(dom),
5535 1, ISL_SET_DISJOINT);
5536 if (!sol_pma->empty)
5537 goto error;
5540 isl_basic_set_free(dom);
5541 return &sol_pma->sol;
5542 error:
5543 isl_basic_set_free(dom);
5544 sol_pma_free(sol_pma);
5545 return NULL;
5548 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
5549 * some obvious symmetries.
5551 * We call basic_map_partial_lexopt_base_sol and extract the results.
5553 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_base_pw_multi_aff(
5554 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5555 __isl_give isl_set **empty, int max)
5557 isl_pw_multi_aff *result = NULL;
5558 struct isl_sol *sol;
5559 struct isl_sol_pma *sol_pma;
5561 sol = basic_map_partial_lexopt_base_sol(bmap, dom, empty, max,
5562 &sol_pma_init);
5563 if (!sol)
5564 return NULL;
5565 sol_pma = (struct isl_sol_pma *) sol;
5567 result = isl_pw_multi_aff_copy(sol_pma->pma);
5568 if (empty)
5569 *empty = isl_set_copy(sol_pma->empty);
5570 sol_free(&sol_pma->sol);
5571 return result;
5574 /* Given that the last input variable of "maff" represents the minimum
5575 * of some bounds, check whether we need to plug in the expression
5576 * of the minimum.
5578 * In particular, check if the last input variable appears in any
5579 * of the expressions in "maff".
5581 static int need_substitution(__isl_keep isl_multi_aff *maff)
5583 int i;
5584 unsigned pos;
5586 pos = isl_multi_aff_dim(maff, isl_dim_in) - 1;
5588 for (i = 0; i < maff->n; ++i)
5589 if (isl_aff_involves_dims(maff->p[i], isl_dim_in, pos, 1))
5590 return 1;
5592 return 0;
5595 /* Given a set of upper bounds on the last "input" variable m,
5596 * construct a piecewise affine expression that selects
5597 * the minimal upper bound to m, i.e.,
5598 * divide the space into cells where one
5599 * of the upper bounds is smaller than all the others and select
5600 * this upper bound on that cell.
5602 * In particular, if there are n bounds b_i, then the result
5603 * consists of n cell, each one of the form
5605 * b_i <= b_j for j > i
5606 * b_i < b_j for j < i
5608 * The affine expression on this cell is
5610 * b_i
5612 static __isl_give isl_pw_aff *set_minimum_pa(__isl_take isl_space *space,
5613 __isl_take isl_mat *var)
5615 int i;
5616 isl_aff *aff = NULL;
5617 isl_basic_set *bset = NULL;
5618 isl_pw_aff *paff = NULL;
5619 isl_space *pw_space;
5620 isl_local_space *ls = NULL;
5622 if (!space || !var)
5623 goto error;
5625 ls = isl_local_space_from_space(isl_space_copy(space));
5626 pw_space = isl_space_copy(space);
5627 pw_space = isl_space_from_domain(pw_space);
5628 pw_space = isl_space_add_dims(pw_space, isl_dim_out, 1);
5629 paff = isl_pw_aff_alloc_size(pw_space, var->n_row);
5631 for (i = 0; i < var->n_row; ++i) {
5632 isl_pw_aff *paff_i;
5634 aff = isl_aff_alloc(isl_local_space_copy(ls));
5635 bset = isl_basic_set_alloc_space(isl_space_copy(space), 0,
5636 0, var->n_row - 1);
5637 if (!aff || !bset)
5638 goto error;
5639 isl_int_set_si(aff->v->el[0], 1);
5640 isl_seq_cpy(aff->v->el + 1, var->row[i], var->n_col);
5641 isl_int_set_si(aff->v->el[1 + var->n_col], 0);
5642 bset = select_minimum(bset, var, i);
5643 paff_i = isl_pw_aff_alloc(isl_set_from_basic_set(bset), aff);
5644 paff = isl_pw_aff_add_disjoint(paff, paff_i);
5647 isl_local_space_free(ls);
5648 isl_space_free(space);
5649 isl_mat_free(var);
5650 return paff;
5651 error:
5652 isl_aff_free(aff);
5653 isl_basic_set_free(bset);
5654 isl_pw_aff_free(paff);
5655 isl_local_space_free(ls);
5656 isl_space_free(space);
5657 isl_mat_free(var);
5658 return NULL;
5661 /* Given a piecewise multi-affine expression of which the last input variable
5662 * is the minimum of the bounds in "cst", plug in the value of the minimum.
5663 * This minimum expression is given in "min_expr_pa".
5664 * The set "min_expr" contains the same information, but in the form of a set.
5665 * The variable is subsequently projected out.
5667 * The implementation is similar to those of "split" and "split_domain".
5668 * If the variable appears in a given expression, then minimum expression
5669 * is plugged in. Otherwise, if the variable appears in the constraints
5670 * and a split is required, then the domain is split. Otherwise, no split
5671 * is performed.
5673 static __isl_give isl_pw_multi_aff *split_domain_pma(
5674 __isl_take isl_pw_multi_aff *opt, __isl_take isl_pw_aff *min_expr_pa,
5675 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
5677 int n_in;
5678 int i;
5679 isl_space *space;
5680 isl_pw_multi_aff *res;
5682 if (!opt || !min_expr || !cst)
5683 goto error;
5685 n_in = isl_pw_multi_aff_dim(opt, isl_dim_in);
5686 space = isl_pw_multi_aff_get_space(opt);
5687 space = isl_space_drop_dims(space, isl_dim_in, n_in - 1, 1);
5688 res = isl_pw_multi_aff_empty(space);
5690 for (i = 0; i < opt->n; ++i) {
5691 isl_pw_multi_aff *pma;
5693 pma = isl_pw_multi_aff_alloc(isl_set_copy(opt->p[i].set),
5694 isl_multi_aff_copy(opt->p[i].maff));
5695 if (need_substitution(opt->p[i].maff))
5696 pma = isl_pw_multi_aff_substitute(pma,
5697 isl_dim_in, n_in - 1, min_expr_pa);
5698 else if (need_split_set(opt->p[i].set, cst))
5699 pma = isl_pw_multi_aff_intersect_domain(pma,
5700 isl_set_copy(min_expr));
5701 pma = isl_pw_multi_aff_project_out(pma,
5702 isl_dim_in, n_in - 1, 1);
5704 res = isl_pw_multi_aff_add_disjoint(res, pma);
5707 isl_pw_multi_aff_free(opt);
5708 isl_pw_aff_free(min_expr_pa);
5709 isl_set_free(min_expr);
5710 isl_mat_free(cst);
5711 return res;
5712 error:
5713 isl_pw_multi_aff_free(opt);
5714 isl_pw_aff_free(min_expr_pa);
5715 isl_set_free(min_expr);
5716 isl_mat_free(cst);
5717 return NULL;
5720 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_pw_multi_aff(
5721 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5722 __isl_give isl_set **empty, int max);
5724 /* This function is called from basic_map_partial_lexopt_symm.
5725 * The last variable of "bmap" and "dom" corresponds to the minimum
5726 * of the bounds in "cst". "map_space" is the space of the original
5727 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
5728 * is the space of the original domain.
5730 * We recursively call basic_map_partial_lexopt and then plug in
5731 * the definition of the minimum in the result.
5733 static __isl_give isl_pw_multi_aff *
5734 basic_map_partial_lexopt_symm_core_pw_multi_aff(
5735 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5736 __isl_give isl_set **empty, int max, __isl_take isl_mat *cst,
5737 __isl_take isl_space *map_space, __isl_take isl_space *set_space)
5739 isl_pw_multi_aff *opt;
5740 isl_pw_aff *min_expr_pa;
5741 isl_set *min_expr;
5743 min_expr = set_minimum(isl_basic_set_get_space(dom), isl_mat_copy(cst));
5744 min_expr_pa = set_minimum_pa(isl_basic_set_get_space(dom),
5745 isl_mat_copy(cst));
5747 opt = basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, max);
5749 if (empty) {
5750 *empty = split(*empty,
5751 isl_set_copy(min_expr), isl_mat_copy(cst));
5752 *empty = isl_set_reset_space(*empty, set_space);
5755 opt = split_domain_pma(opt, min_expr_pa, min_expr, cst);
5756 opt = isl_pw_multi_aff_reset_space(opt, map_space);
5758 return opt;
5761 #undef TYPE
5762 #define TYPE isl_pw_multi_aff
5763 #undef SUFFIX
5764 #define SUFFIX _pw_multi_aff
5765 #include "isl_tab_lexopt_templ.c"