isl_tab_cone_is_bounded: return isl_bool
[isl.git] / isl_tab_pip.c
blobcdc1b20323e7716a67937890d1287868eb717d1d
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 * "ma" describes the solution as a function of "dom".
137 * In particular, the domain space of "ma" is equal to the space of "dom".
139 * If "ma" is NULL, then there is no solution on "dom".
141 struct isl_partial_sol {
142 int level;
143 struct isl_basic_set *dom;
144 isl_multi_aff *ma;
146 struct isl_partial_sol *next;
149 struct isl_sol;
150 struct isl_sol_callback {
151 struct isl_tab_callback callback;
152 struct isl_sol *sol;
155 /* isl_sol is an interface for constructing a solution to
156 * a parametric integer linear programming problem.
157 * Every time the algorithm reaches a state where a solution
158 * can be read off from the tableau, the function "add" is called
159 * on the isl_sol passed to find_solutions_main. In a state where
160 * the tableau is empty, "add_empty" is called instead.
161 * "free" is called to free the implementation specific fields, if any.
163 * "error" is set if some error has occurred. This flag invalidates
164 * the remainder of the data structure.
165 * If "rational" is set, then a rational optimization is being performed.
166 * "level" is the current level in the tree with nodes for each
167 * split in the context.
168 * If "max" is set, then a maximization problem is being solved, rather than
169 * a minimization problem, which means that the variables in the
170 * tableau have value "M - x" rather than "M + x".
171 * "n_out" is the number of output dimensions in the input.
172 * "space" is the space in which the solution (and also the input) lives.
174 * The context tableau is owned by isl_sol and is updated incrementally.
176 * There are currently three implementations of this interface,
177 * isl_sol_map, which simply collects the solutions in an isl_map
178 * and (optionally) the parts of the context where there is no solution
179 * in an isl_set,
180 * isl_sol_pma, which collects an isl_pw_multi_aff instead, and
181 * isl_sol_for, which calls a user-defined function for each part of
182 * the solution.
184 struct isl_sol {
185 int error;
186 int rational;
187 int level;
188 int max;
189 int n_out;
190 isl_space *space;
191 struct isl_context *context;
192 struct isl_partial_sol *partial;
193 void (*add)(struct isl_sol *sol,
194 __isl_take isl_basic_set *dom, __isl_take isl_multi_aff *ma);
195 void (*add_empty)(struct isl_sol *sol, struct isl_basic_set *bset);
196 void (*free)(struct isl_sol *sol);
197 struct isl_sol_callback dec_level;
200 static void sol_free(struct isl_sol *sol)
202 struct isl_partial_sol *partial, *next;
203 if (!sol)
204 return;
205 for (partial = sol->partial; partial; partial = next) {
206 next = partial->next;
207 isl_basic_set_free(partial->dom);
208 isl_multi_aff_free(partial->ma);
209 free(partial);
211 isl_space_free(sol->space);
212 if (sol->context)
213 sol->context->op->free(sol->context);
214 sol->free(sol);
215 free(sol);
218 /* Push a partial solution represented by a domain and function "ma"
219 * onto the stack of partial solutions.
220 * If "ma" is NULL, then "dom" represents a part of the domain
221 * with no solution.
223 static void sol_push_sol(struct isl_sol *sol,
224 __isl_take isl_basic_set *dom, __isl_take isl_multi_aff *ma)
226 struct isl_partial_sol *partial;
228 if (sol->error || !dom)
229 goto error;
231 partial = isl_alloc_type(dom->ctx, struct isl_partial_sol);
232 if (!partial)
233 goto error;
235 partial->level = sol->level;
236 partial->dom = dom;
237 partial->ma = ma;
238 partial->next = sol->partial;
240 sol->partial = partial;
242 return;
243 error:
244 isl_basic_set_free(dom);
245 isl_multi_aff_free(ma);
246 sol->error = 1;
249 /* Check that the final columns of "M", starting at "first", are zero.
251 static isl_stat check_final_columns_are_zero(__isl_keep isl_mat *M,
252 unsigned first)
254 int i;
255 unsigned rows, cols, n;
257 if (!M)
258 return isl_stat_error;
259 rows = isl_mat_rows(M);
260 cols = isl_mat_cols(M);
261 n = cols - first;
262 for (i = 0; i < rows; ++i)
263 if (isl_seq_first_non_zero(M->row[i] + first, n) != -1)
264 isl_die(isl_mat_get_ctx(M), isl_error_internal,
265 "final columns should be zero",
266 return isl_stat_error);
267 return isl_stat_ok;
270 /* Set the affine expressions in "ma" according to the rows in "M", which
271 * are defined over the local space "ls".
272 * The matrix "M" may have extra (zero) columns beyond the number
273 * of variables in "ls".
275 static __isl_give isl_multi_aff *set_from_affine_matrix(
276 __isl_take isl_multi_aff *ma, __isl_take isl_local_space *ls,
277 __isl_take isl_mat *M)
279 int i, dim;
280 isl_aff *aff;
282 if (!ma || !ls || !M)
283 goto error;
285 dim = isl_local_space_dim(ls, isl_dim_all);
286 if (check_final_columns_are_zero(M, 1 + dim) < 0)
287 goto error;
288 for (i = 1; i < M->n_row; ++i) {
289 aff = isl_aff_alloc(isl_local_space_copy(ls));
290 if (aff) {
291 isl_int_set(aff->v->el[0], M->row[0][0]);
292 isl_seq_cpy(aff->v->el + 1, M->row[i], 1 + dim);
294 aff = isl_aff_normalize(aff);
295 ma = isl_multi_aff_set_aff(ma, i - 1, aff);
297 isl_local_space_free(ls);
298 isl_mat_free(M);
300 return ma;
301 error:
302 isl_local_space_free(ls);
303 isl_mat_free(M);
304 isl_multi_aff_free(ma);
305 return NULL;
308 /* Push a partial solution represented by a domain and mapping M
309 * onto the stack of partial solutions.
311 * The affine matrix "M" maps the dimensions of the context
312 * to the output variables. Convert it into an isl_multi_aff and
313 * then call sol_push_sol.
315 * Note that the description of the initial context may have involved
316 * existentially quantified variables, in which case they also appear
317 * in "dom". These need to be removed before creating the affine
318 * expression because an affine expression cannot be defined in terms
319 * of existentially quantified variables without a known representation.
320 * Since newly added integer divisions are inserted before these
321 * existentially quantified variables, they are still in the final
322 * positions and the corresponding final columns of "M" are zero
323 * because align_context_divs adds the existentially quantified
324 * variables of the context to the main tableau without any constraints and
325 * any equality constraints that are added later on can only serve
326 * to eliminate these existentially quantified variables.
328 static void sol_push_sol_mat(struct isl_sol *sol,
329 __isl_take isl_basic_set *dom, __isl_take isl_mat *M)
331 isl_local_space *ls;
332 isl_multi_aff *ma;
333 int n_div, n_known;
335 n_div = isl_basic_set_dim(dom, isl_dim_div);
336 n_known = n_div - sol->context->n_unknown;
338 ma = isl_multi_aff_alloc(isl_space_copy(sol->space));
339 ls = isl_basic_set_get_local_space(dom);
340 ls = isl_local_space_drop_dims(ls, isl_dim_div,
341 n_known, n_div - n_known);
342 ma = set_from_affine_matrix(ma, ls, M);
344 if (!ma)
345 dom = isl_basic_set_free(dom);
346 sol_push_sol(sol, dom, ma);
349 /* Pop one partial solution from the partial solution stack and
350 * pass it on to sol->add or sol->add_empty.
352 static void sol_pop_one(struct isl_sol *sol)
354 struct isl_partial_sol *partial;
356 partial = sol->partial;
357 sol->partial = partial->next;
359 if (partial->ma)
360 sol->add(sol, partial->dom, partial->ma);
361 else
362 sol->add_empty(sol, partial->dom);
363 free(partial);
366 /* Return a fresh copy of the domain represented by the context tableau.
368 static struct isl_basic_set *sol_domain(struct isl_sol *sol)
370 struct isl_basic_set *bset;
372 if (sol->error)
373 return NULL;
375 bset = isl_basic_set_dup(sol->context->op->peek_basic_set(sol->context));
376 bset = isl_basic_set_update_from_tab(bset,
377 sol->context->op->peek_tab(sol->context));
379 return bset;
382 /* Check whether two partial solutions have the same affine expressions.
384 static isl_bool same_solution(struct isl_partial_sol *s1,
385 struct isl_partial_sol *s2)
387 if (!s1->ma != !s2->ma)
388 return isl_bool_false;
389 if (!s1->ma)
390 return isl_bool_true;
392 return isl_multi_aff_plain_is_equal(s1->ma, s2->ma);
395 /* Swap the initial two partial solutions in "sol".
397 * That is, go from
399 * sol->partial = p1; p1->next = p2; p2->next = p3
401 * to
403 * sol->partial = p2; p2->next = p1; p1->next = p3
405 static void swap_initial(struct isl_sol *sol)
407 struct isl_partial_sol *partial;
409 partial = sol->partial;
410 sol->partial = partial->next;
411 partial->next = partial->next->next;
412 sol->partial->next = partial;
415 /* Combine the initial two partial solution of "sol" into
416 * a partial solution with the current context domain of "sol" and
417 * the function description of the second partial solution in the list.
418 * The level of the new partial solution is set to the current level.
420 * That is, the first two partial solutions (D1,M1) and (D2,M2) are
421 * replaced by (D,M2), where D is the domain of "sol", which is assumed
422 * to be the union of D1 and D2, while M1 is assumed to be equal to M2
423 * (at least on D1).
425 static isl_stat combine_initial_into_second(struct isl_sol *sol)
427 struct isl_partial_sol *partial;
428 isl_basic_set *bset;
430 partial = sol->partial;
432 bset = sol_domain(sol);
433 isl_basic_set_free(partial->next->dom);
434 partial->next->dom = bset;
435 partial->next->level = sol->level;
437 if (!bset)
438 return isl_stat_error;
440 sol->partial = partial->next;
441 isl_basic_set_free(partial->dom);
442 isl_multi_aff_free(partial->ma);
443 free(partial);
445 return isl_stat_ok;
448 /* Are "ma1" and "ma2" equal to each other on "dom"?
450 * Combine "ma1" and "ma2" with "dom" and check if the results are the same.
451 * "dom" may have existentially quantified variables. Eliminate them first
452 * as otherwise they would have to be eliminated twice, in a more complicated
453 * context.
455 static isl_bool equal_on_domain(__isl_keep isl_multi_aff *ma1,
456 __isl_keep isl_multi_aff *ma2, __isl_keep isl_basic_set *dom)
458 isl_set *set;
459 isl_pw_multi_aff *pma1, *pma2;
460 isl_bool equal;
462 set = isl_basic_set_compute_divs(isl_basic_set_copy(dom));
463 pma1 = isl_pw_multi_aff_alloc(isl_set_copy(set),
464 isl_multi_aff_copy(ma1));
465 pma2 = isl_pw_multi_aff_alloc(set, isl_multi_aff_copy(ma2));
466 equal = isl_pw_multi_aff_is_equal(pma1, pma2);
467 isl_pw_multi_aff_free(pma1);
468 isl_pw_multi_aff_free(pma2);
470 return equal;
473 /* The initial two partial solutions of "sol" are known to be at
474 * the same level.
475 * If they represent the same solution (on different parts of the domain),
476 * then combine them into a single solution at the current level.
477 * Otherwise, pop them both.
479 * Even if the two partial solution are not obviously the same,
480 * one may still be a simplification of the other over its own domain.
481 * Also check if the two sets of affine functions are equal when
482 * restricted to one of the domains. If so, combine the two
483 * using the set of affine functions on the other domain.
484 * That is, for two partial solutions (D1,M1) and (D2,M2),
485 * if M1 = M2 on D1, then the pair of partial solutions can
486 * be replaced by (D1+D2,M2) and similarly when M1 = M2 on D2.
488 static isl_stat combine_initial_if_equal(struct isl_sol *sol)
490 struct isl_partial_sol *partial;
491 isl_bool same;
493 partial = sol->partial;
495 same = same_solution(partial, partial->next);
496 if (same < 0)
497 return isl_stat_error;
498 if (same)
499 return combine_initial_into_second(sol);
500 if (partial->ma && partial->next->ma) {
501 same = equal_on_domain(partial->ma, partial->next->ma,
502 partial->dom);
503 if (same < 0)
504 return isl_stat_error;
505 if (same)
506 return combine_initial_into_second(sol);
507 same = equal_on_domain(partial->ma, partial->next->ma,
508 partial->next->dom);
509 if (same) {
510 swap_initial(sol);
511 return combine_initial_into_second(sol);
515 sol_pop_one(sol);
516 sol_pop_one(sol);
518 return isl_stat_ok;
521 /* Pop all solutions from the partial solution stack that were pushed onto
522 * the stack at levels that are deeper than the current level.
523 * If the two topmost elements on the stack have the same level
524 * and represent the same solution, then their domains are combined.
525 * This combined domain is the same as the current context domain
526 * as sol_pop is called each time we move back to a higher level.
527 * If the outer level (0) has been reached, then all partial solutions
528 * at the current level are also popped off.
530 static void sol_pop(struct isl_sol *sol)
532 struct isl_partial_sol *partial;
534 if (sol->error)
535 return;
537 partial = sol->partial;
538 if (!partial)
539 return;
541 if (partial->level == 0 && sol->level == 0) {
542 for (partial = sol->partial; partial; partial = sol->partial)
543 sol_pop_one(sol);
544 return;
547 if (partial->level <= sol->level)
548 return;
550 if (partial->next && partial->next->level == partial->level) {
551 if (combine_initial_if_equal(sol) < 0)
552 goto error;
553 } else
554 sol_pop_one(sol);
556 if (sol->level == 0) {
557 for (partial = sol->partial; partial; partial = sol->partial)
558 sol_pop_one(sol);
559 return;
562 if (0)
563 error: sol->error = 1;
566 static void sol_dec_level(struct isl_sol *sol)
568 if (sol->error)
569 return;
571 sol->level--;
573 sol_pop(sol);
576 static int sol_dec_level_wrap(struct isl_tab_callback *cb)
578 struct isl_sol_callback *callback = (struct isl_sol_callback *)cb;
580 sol_dec_level(callback->sol);
582 return callback->sol->error ? -1 : 0;
585 /* Move down to next level and push callback onto context tableau
586 * to decrease the level again when it gets rolled back across
587 * the current state. That is, dec_level will be called with
588 * the context tableau in the same state as it is when inc_level
589 * is called.
591 static void sol_inc_level(struct isl_sol *sol)
593 struct isl_tab *tab;
595 if (sol->error)
596 return;
598 sol->level++;
599 tab = sol->context->op->peek_tab(sol->context);
600 if (isl_tab_push_callback(tab, &sol->dec_level.callback) < 0)
601 sol->error = 1;
604 static void scale_rows(struct isl_mat *mat, isl_int m, int n_row)
606 int i;
608 if (isl_int_is_one(m))
609 return;
611 for (i = 0; i < n_row; ++i)
612 isl_seq_scale(mat->row[i], mat->row[i], m, mat->n_col);
615 /* Add the solution identified by the tableau and the context tableau.
617 * The layout of the variables is as follows.
618 * tab->n_var is equal to the total number of variables in the input
619 * map (including divs that were copied from the context)
620 * + the number of extra divs constructed
621 * Of these, the first tab->n_param and the last tab->n_div variables
622 * correspond to the variables in the context, i.e.,
623 * tab->n_param + tab->n_div = context_tab->n_var
624 * tab->n_param is equal to the number of parameters and input
625 * dimensions in the input map
626 * tab->n_div is equal to the number of divs in the context
628 * If there is no solution, then call add_empty with a basic set
629 * that corresponds to the context tableau. (If add_empty is NULL,
630 * then do nothing).
632 * If there is a solution, then first construct a matrix that maps
633 * all dimensions of the context to the output variables, i.e.,
634 * the output dimensions in the input map.
635 * The divs in the input map (if any) that do not correspond to any
636 * div in the context do not appear in the solution.
637 * The algorithm will make sure that they have an integer value,
638 * but these values themselves are of no interest.
639 * We have to be careful not to drop or rearrange any divs in the
640 * context because that would change the meaning of the matrix.
642 * To extract the value of the output variables, it should be noted
643 * that we always use a big parameter M in the main tableau and so
644 * the variable stored in this tableau is not an output variable x itself, but
645 * x' = M + x (in case of minimization)
646 * or
647 * x' = M - x (in case of maximization)
648 * If x' appears in a column, then its optimal value is zero,
649 * which means that the optimal value of x is an unbounded number
650 * (-M for minimization and M for maximization).
651 * We currently assume that the output dimensions in the original map
652 * are bounded, so this cannot occur.
653 * Similarly, when x' appears in a row, then the coefficient of M in that
654 * row is necessarily 1.
655 * If the row in the tableau represents
656 * d x' = c + d M + e(y)
657 * then, in case of minimization, the corresponding row in the matrix
658 * will be
659 * a c + a e(y)
660 * with a d = m, the (updated) common denominator of the matrix.
661 * In case of maximization, the row will be
662 * -a c - a e(y)
664 static void sol_add(struct isl_sol *sol, struct isl_tab *tab)
666 struct isl_basic_set *bset = NULL;
667 struct isl_mat *mat = NULL;
668 unsigned off;
669 int row;
670 isl_int m;
672 if (sol->error || !tab)
673 goto error;
675 if (tab->empty && !sol->add_empty)
676 return;
677 if (sol->context->op->is_empty(sol->context))
678 return;
680 bset = sol_domain(sol);
682 if (tab->empty) {
683 sol_push_sol(sol, bset, NULL);
684 return;
687 off = 2 + tab->M;
689 mat = isl_mat_alloc(tab->mat->ctx, 1 + sol->n_out,
690 1 + tab->n_param + tab->n_div);
691 if (!mat)
692 goto error;
694 isl_int_init(m);
696 isl_seq_clr(mat->row[0] + 1, mat->n_col - 1);
697 isl_int_set_si(mat->row[0][0], 1);
698 for (row = 0; row < sol->n_out; ++row) {
699 int i = tab->n_param + row;
700 int r, j;
702 isl_seq_clr(mat->row[1 + row], mat->n_col);
703 if (!tab->var[i].is_row) {
704 if (tab->M)
705 isl_die(mat->ctx, isl_error_invalid,
706 "unbounded optimum", goto error2);
707 continue;
710 r = tab->var[i].index;
711 if (tab->M &&
712 isl_int_ne(tab->mat->row[r][2], tab->mat->row[r][0]))
713 isl_die(mat->ctx, isl_error_invalid,
714 "unbounded optimum", goto error2);
715 isl_int_gcd(m, mat->row[0][0], tab->mat->row[r][0]);
716 isl_int_divexact(m, tab->mat->row[r][0], m);
717 scale_rows(mat, m, 1 + row);
718 isl_int_divexact(m, mat->row[0][0], tab->mat->row[r][0]);
719 isl_int_mul(mat->row[1 + row][0], m, tab->mat->row[r][1]);
720 for (j = 0; j < tab->n_param; ++j) {
721 int col;
722 if (tab->var[j].is_row)
723 continue;
724 col = tab->var[j].index;
725 isl_int_mul(mat->row[1 + row][1 + j], m,
726 tab->mat->row[r][off + col]);
728 for (j = 0; j < tab->n_div; ++j) {
729 int col;
730 if (tab->var[tab->n_var - tab->n_div+j].is_row)
731 continue;
732 col = tab->var[tab->n_var - tab->n_div+j].index;
733 isl_int_mul(mat->row[1 + row][1 + tab->n_param + j], m,
734 tab->mat->row[r][off + col]);
736 if (sol->max)
737 isl_seq_neg(mat->row[1 + row], mat->row[1 + row],
738 mat->n_col);
741 isl_int_clear(m);
743 sol_push_sol_mat(sol, bset, mat);
744 return;
745 error2:
746 isl_int_clear(m);
747 error:
748 isl_basic_set_free(bset);
749 isl_mat_free(mat);
750 sol->error = 1;
753 struct isl_sol_map {
754 struct isl_sol sol;
755 struct isl_map *map;
756 struct isl_set *empty;
759 static void sol_map_free(struct isl_sol *sol)
761 struct isl_sol_map *sol_map = (struct isl_sol_map *) sol;
762 isl_map_free(sol_map->map);
763 isl_set_free(sol_map->empty);
766 /* This function is called for parts of the context where there is
767 * no solution, with "bset" corresponding to the context tableau.
768 * Simply add the basic set to the set "empty".
770 static void sol_map_add_empty(struct isl_sol_map *sol,
771 struct isl_basic_set *bset)
773 if (!bset || !sol->empty)
774 goto error;
776 sol->empty = isl_set_grow(sol->empty, 1);
777 bset = isl_basic_set_simplify(bset);
778 bset = isl_basic_set_finalize(bset);
779 sol->empty = isl_set_add_basic_set(sol->empty, isl_basic_set_copy(bset));
780 if (!sol->empty)
781 goto error;
782 isl_basic_set_free(bset);
783 return;
784 error:
785 isl_basic_set_free(bset);
786 sol->sol.error = 1;
789 static void sol_map_add_empty_wrap(struct isl_sol *sol,
790 struct isl_basic_set *bset)
792 sol_map_add_empty((struct isl_sol_map *)sol, bset);
795 /* Given a basic set "dom" that represents the context and a tuple of
796 * affine expressions "ma" defined over this domain, construct a basic map
797 * that expresses this function on the domain.
799 static void sol_map_add(struct isl_sol_map *sol,
800 __isl_take isl_basic_set *dom, __isl_take isl_multi_aff *ma)
802 isl_basic_map *bmap;
804 if (sol->sol.error || !dom || !ma)
805 goto error;
807 bmap = isl_basic_map_from_multi_aff2(ma, sol->sol.rational);
808 bmap = isl_basic_map_intersect_domain(bmap, dom);
809 sol->map = isl_map_grow(sol->map, 1);
810 sol->map = isl_map_add_basic_map(sol->map, bmap);
811 if (!sol->map)
812 sol->sol.error = 1;
813 return;
814 error:
815 isl_basic_set_free(dom);
816 isl_multi_aff_free(ma);
817 sol->sol.error = 1;
820 static void sol_map_add_wrap(struct isl_sol *sol,
821 __isl_take isl_basic_set *dom, __isl_take isl_multi_aff *ma)
823 sol_map_add((struct isl_sol_map *)sol, dom, ma);
827 /* Store the "parametric constant" of row "row" of tableau "tab" in "line",
828 * i.e., the constant term and the coefficients of all variables that
829 * appear in the context tableau.
830 * Note that the coefficient of the big parameter M is NOT copied.
831 * The context tableau may not have a big parameter and even when it
832 * does, it is a different big parameter.
834 static void get_row_parameter_line(struct isl_tab *tab, int row, isl_int *line)
836 int i;
837 unsigned off = 2 + tab->M;
839 isl_int_set(line[0], tab->mat->row[row][1]);
840 for (i = 0; i < tab->n_param; ++i) {
841 if (tab->var[i].is_row)
842 isl_int_set_si(line[1 + i], 0);
843 else {
844 int col = tab->var[i].index;
845 isl_int_set(line[1 + i], tab->mat->row[row][off + col]);
848 for (i = 0; i < tab->n_div; ++i) {
849 if (tab->var[tab->n_var - tab->n_div + i].is_row)
850 isl_int_set_si(line[1 + tab->n_param + i], 0);
851 else {
852 int col = tab->var[tab->n_var - tab->n_div + i].index;
853 isl_int_set(line[1 + tab->n_param + i],
854 tab->mat->row[row][off + col]);
859 /* Check if rows "row1" and "row2" have identical "parametric constants",
860 * as explained above.
861 * In this case, we also insist that the coefficients of the big parameter
862 * be the same as the values of the constants will only be the same
863 * if these coefficients are also the same.
865 static int identical_parameter_line(struct isl_tab *tab, int row1, int row2)
867 int i;
868 unsigned off = 2 + tab->M;
870 if (isl_int_ne(tab->mat->row[row1][1], tab->mat->row[row2][1]))
871 return 0;
873 if (tab->M && isl_int_ne(tab->mat->row[row1][2],
874 tab->mat->row[row2][2]))
875 return 0;
877 for (i = 0; i < tab->n_param + tab->n_div; ++i) {
878 int pos = i < tab->n_param ? i :
879 tab->n_var - tab->n_div + i - tab->n_param;
880 int col;
882 if (tab->var[pos].is_row)
883 continue;
884 col = tab->var[pos].index;
885 if (isl_int_ne(tab->mat->row[row1][off + col],
886 tab->mat->row[row2][off + col]))
887 return 0;
889 return 1;
892 /* Return an inequality that expresses that the "parametric constant"
893 * should be non-negative.
894 * This function is only called when the coefficient of the big parameter
895 * is equal to zero.
897 static struct isl_vec *get_row_parameter_ineq(struct isl_tab *tab, int row)
899 struct isl_vec *ineq;
901 ineq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_param + tab->n_div);
902 if (!ineq)
903 return NULL;
905 get_row_parameter_line(tab, row, ineq->el);
906 if (ineq)
907 ineq = isl_vec_normalize(ineq);
909 return ineq;
912 /* Normalize a div expression of the form
914 * [(g*f(x) + c)/(g * m)]
916 * with c the constant term and f(x) the remaining coefficients, to
918 * [(f(x) + [c/g])/m]
920 static void normalize_div(__isl_keep isl_vec *div)
922 isl_ctx *ctx = isl_vec_get_ctx(div);
923 int len = div->size - 2;
925 isl_seq_gcd(div->el + 2, len, &ctx->normalize_gcd);
926 isl_int_gcd(ctx->normalize_gcd, ctx->normalize_gcd, div->el[0]);
928 if (isl_int_is_one(ctx->normalize_gcd))
929 return;
931 isl_int_divexact(div->el[0], div->el[0], ctx->normalize_gcd);
932 isl_int_fdiv_q(div->el[1], div->el[1], ctx->normalize_gcd);
933 isl_seq_scale_down(div->el + 2, div->el + 2, ctx->normalize_gcd, len);
936 /* Return an integer division for use in a parametric cut based
937 * on the given row.
938 * In particular, let the parametric constant of the row be
940 * \sum_i a_i y_i
942 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
943 * The div returned is equal to
945 * floor(\sum_i {-a_i} y_i) = floor((\sum_i (-a_i mod d) y_i)/d)
947 static struct isl_vec *get_row_parameter_div(struct isl_tab *tab, int row)
949 struct isl_vec *div;
951 div = isl_vec_alloc(tab->mat->ctx, 1 + 1 + tab->n_param + tab->n_div);
952 if (!div)
953 return NULL;
955 isl_int_set(div->el[0], tab->mat->row[row][0]);
956 get_row_parameter_line(tab, row, div->el + 1);
957 isl_seq_neg(div->el + 1, div->el + 1, div->size - 1);
958 normalize_div(div);
959 isl_seq_fdiv_r(div->el + 1, div->el + 1, div->el[0], div->size - 1);
961 return div;
964 /* Return an integer division for use in transferring an integrality constraint
965 * to the context.
966 * In particular, let the parametric constant of the row be
968 * \sum_i a_i y_i
970 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
971 * The the returned div is equal to
973 * floor(\sum_i {a_i} y_i) = floor((\sum_i (a_i mod d) y_i)/d)
975 static struct isl_vec *get_row_split_div(struct isl_tab *tab, int row)
977 struct isl_vec *div;
979 div = isl_vec_alloc(tab->mat->ctx, 1 + 1 + tab->n_param + tab->n_div);
980 if (!div)
981 return NULL;
983 isl_int_set(div->el[0], tab->mat->row[row][0]);
984 get_row_parameter_line(tab, row, div->el + 1);
985 normalize_div(div);
986 isl_seq_fdiv_r(div->el + 1, div->el + 1, div->el[0], div->size - 1);
988 return div;
991 /* Construct and return an inequality that expresses an upper bound
992 * on the given div.
993 * In particular, if the div is given by
995 * d = floor(e/m)
997 * then the inequality expresses
999 * m d <= e
1001 static struct isl_vec *ineq_for_div(struct isl_basic_set *bset, unsigned div)
1003 unsigned total;
1004 unsigned div_pos;
1005 struct isl_vec *ineq;
1007 if (!bset)
1008 return NULL;
1010 total = isl_basic_set_total_dim(bset);
1011 div_pos = 1 + total - bset->n_div + div;
1013 ineq = isl_vec_alloc(bset->ctx, 1 + total);
1014 if (!ineq)
1015 return NULL;
1017 isl_seq_cpy(ineq->el, bset->div[div] + 1, 1 + total);
1018 isl_int_neg(ineq->el[div_pos], bset->div[div][0]);
1019 return ineq;
1022 /* Given a row in the tableau and a div that was created
1023 * using get_row_split_div and that has been constrained to equality, i.e.,
1025 * d = floor(\sum_i {a_i} y_i) = \sum_i {a_i} y_i
1027 * replace the expression "\sum_i {a_i} y_i" in the row by d,
1028 * i.e., we subtract "\sum_i {a_i} y_i" and add 1 d.
1029 * The coefficients of the non-parameters in the tableau have been
1030 * verified to be integral. We can therefore simply replace coefficient b
1031 * by floor(b). For the coefficients of the parameters we have
1032 * floor(a_i) = a_i - {a_i}, while for the other coefficients, we have
1033 * floor(b) = b.
1035 static struct isl_tab *set_row_cst_to_div(struct isl_tab *tab, int row, int div)
1037 isl_seq_fdiv_q(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
1038 tab->mat->row[row][0], 1 + tab->M + tab->n_col);
1040 isl_int_set_si(tab->mat->row[row][0], 1);
1042 if (tab->var[tab->n_var - tab->n_div + div].is_row) {
1043 int drow = tab->var[tab->n_var - tab->n_div + div].index;
1045 isl_assert(tab->mat->ctx,
1046 isl_int_is_one(tab->mat->row[drow][0]), goto error);
1047 isl_seq_combine(tab->mat->row[row] + 1,
1048 tab->mat->ctx->one, tab->mat->row[row] + 1,
1049 tab->mat->ctx->one, tab->mat->row[drow] + 1,
1050 1 + tab->M + tab->n_col);
1051 } else {
1052 int dcol = tab->var[tab->n_var - tab->n_div + div].index;
1054 isl_int_add_ui(tab->mat->row[row][2 + tab->M + dcol],
1055 tab->mat->row[row][2 + tab->M + dcol], 1);
1058 return tab;
1059 error:
1060 isl_tab_free(tab);
1061 return NULL;
1064 /* Check if the (parametric) constant of the given row is obviously
1065 * negative, meaning that we don't need to consult the context tableau.
1066 * If there is a big parameter and its coefficient is non-zero,
1067 * then this coefficient determines the outcome.
1068 * Otherwise, we check whether the constant is negative and
1069 * all non-zero coefficients of parameters are negative and
1070 * belong to non-negative parameters.
1072 static int is_obviously_neg(struct isl_tab *tab, int row)
1074 int i;
1075 int col;
1076 unsigned off = 2 + tab->M;
1078 if (tab->M) {
1079 if (isl_int_is_pos(tab->mat->row[row][2]))
1080 return 0;
1081 if (isl_int_is_neg(tab->mat->row[row][2]))
1082 return 1;
1085 if (isl_int_is_nonneg(tab->mat->row[row][1]))
1086 return 0;
1087 for (i = 0; i < tab->n_param; ++i) {
1088 /* Eliminated parameter */
1089 if (tab->var[i].is_row)
1090 continue;
1091 col = tab->var[i].index;
1092 if (isl_int_is_zero(tab->mat->row[row][off + col]))
1093 continue;
1094 if (!tab->var[i].is_nonneg)
1095 return 0;
1096 if (isl_int_is_pos(tab->mat->row[row][off + col]))
1097 return 0;
1099 for (i = 0; i < tab->n_div; ++i) {
1100 if (tab->var[tab->n_var - tab->n_div + i].is_row)
1101 continue;
1102 col = tab->var[tab->n_var - tab->n_div + i].index;
1103 if (isl_int_is_zero(tab->mat->row[row][off + col]))
1104 continue;
1105 if (!tab->var[tab->n_var - tab->n_div + i].is_nonneg)
1106 return 0;
1107 if (isl_int_is_pos(tab->mat->row[row][off + col]))
1108 return 0;
1110 return 1;
1113 /* Check if the (parametric) constant of the given row is obviously
1114 * non-negative, meaning that we don't need to consult the context tableau.
1115 * If there is a big parameter and its coefficient is non-zero,
1116 * then this coefficient determines the outcome.
1117 * Otherwise, we check whether the constant is non-negative and
1118 * all non-zero coefficients of parameters are positive and
1119 * belong to non-negative parameters.
1121 static int is_obviously_nonneg(struct isl_tab *tab, int row)
1123 int i;
1124 int col;
1125 unsigned off = 2 + tab->M;
1127 if (tab->M) {
1128 if (isl_int_is_pos(tab->mat->row[row][2]))
1129 return 1;
1130 if (isl_int_is_neg(tab->mat->row[row][2]))
1131 return 0;
1134 if (isl_int_is_neg(tab->mat->row[row][1]))
1135 return 0;
1136 for (i = 0; i < tab->n_param; ++i) {
1137 /* Eliminated parameter */
1138 if (tab->var[i].is_row)
1139 continue;
1140 col = tab->var[i].index;
1141 if (isl_int_is_zero(tab->mat->row[row][off + col]))
1142 continue;
1143 if (!tab->var[i].is_nonneg)
1144 return 0;
1145 if (isl_int_is_neg(tab->mat->row[row][off + col]))
1146 return 0;
1148 for (i = 0; i < tab->n_div; ++i) {
1149 if (tab->var[tab->n_var - tab->n_div + i].is_row)
1150 continue;
1151 col = tab->var[tab->n_var - tab->n_div + i].index;
1152 if (isl_int_is_zero(tab->mat->row[row][off + col]))
1153 continue;
1154 if (!tab->var[tab->n_var - tab->n_div + i].is_nonneg)
1155 return 0;
1156 if (isl_int_is_neg(tab->mat->row[row][off + col]))
1157 return 0;
1159 return 1;
1162 /* Given a row r and two columns, return the column that would
1163 * lead to the lexicographically smallest increment in the sample
1164 * solution when leaving the basis in favor of the row.
1165 * Pivoting with column c will increment the sample value by a non-negative
1166 * constant times a_{V,c}/a_{r,c}, with a_{V,c} the elements of column c
1167 * corresponding to the non-parametric variables.
1168 * If variable v appears in a column c_v, the a_{v,c} = 1 iff c = c_v,
1169 * with all other entries in this virtual row equal to zero.
1170 * If variable v appears in a row, then a_{v,c} is the element in column c
1171 * of that row.
1173 * Let v be the first variable with a_{v,c1}/a_{r,c1} != a_{v,c2}/a_{r,c2}.
1174 * Then if a_{v,c1}/a_{r,c1} < a_{v,c2}/a_{r,c2}, i.e.,
1175 * a_{v,c2} a_{r,c1} - a_{v,c1} a_{r,c2} > 0, c1 results in the minimal
1176 * increment. Otherwise, it's c2.
1178 static int lexmin_col_pair(struct isl_tab *tab,
1179 int row, int col1, int col2, isl_int tmp)
1181 int i;
1182 isl_int *tr;
1184 tr = tab->mat->row[row] + 2 + tab->M;
1186 for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
1187 int s1, s2;
1188 isl_int *r;
1190 if (!tab->var[i].is_row) {
1191 if (tab->var[i].index == col1)
1192 return col2;
1193 if (tab->var[i].index == col2)
1194 return col1;
1195 continue;
1198 if (tab->var[i].index == row)
1199 continue;
1201 r = tab->mat->row[tab->var[i].index] + 2 + tab->M;
1202 s1 = isl_int_sgn(r[col1]);
1203 s2 = isl_int_sgn(r[col2]);
1204 if (s1 == 0 && s2 == 0)
1205 continue;
1206 if (s1 < s2)
1207 return col1;
1208 if (s2 < s1)
1209 return col2;
1211 isl_int_mul(tmp, r[col2], tr[col1]);
1212 isl_int_submul(tmp, r[col1], tr[col2]);
1213 if (isl_int_is_pos(tmp))
1214 return col1;
1215 if (isl_int_is_neg(tmp))
1216 return col2;
1218 return -1;
1221 /* Given a row in the tableau, find and return the column that would
1222 * result in the lexicographically smallest, but positive, increment
1223 * in the sample point.
1224 * If there is no such column, then return tab->n_col.
1225 * If anything goes wrong, return -1.
1227 static int lexmin_pivot_col(struct isl_tab *tab, int row)
1229 int j;
1230 int col = tab->n_col;
1231 isl_int *tr;
1232 isl_int tmp;
1234 tr = tab->mat->row[row] + 2 + tab->M;
1236 isl_int_init(tmp);
1238 for (j = tab->n_dead; j < tab->n_col; ++j) {
1239 if (tab->col_var[j] >= 0 &&
1240 (tab->col_var[j] < tab->n_param ||
1241 tab->col_var[j] >= tab->n_var - tab->n_div))
1242 continue;
1244 if (!isl_int_is_pos(tr[j]))
1245 continue;
1247 if (col == tab->n_col)
1248 col = j;
1249 else
1250 col = lexmin_col_pair(tab, row, col, j, tmp);
1251 isl_assert(tab->mat->ctx, col >= 0, goto error);
1254 isl_int_clear(tmp);
1255 return col;
1256 error:
1257 isl_int_clear(tmp);
1258 return -1;
1261 /* Return the first known violated constraint, i.e., a non-negative
1262 * constraint that currently has an either obviously negative value
1263 * or a previously determined to be negative value.
1265 * If any constraint has a negative coefficient for the big parameter,
1266 * if any, then we return one of these first.
1268 static int first_neg(struct isl_tab *tab)
1270 int row;
1272 if (tab->M)
1273 for (row = tab->n_redundant; row < tab->n_row; ++row) {
1274 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
1275 continue;
1276 if (!isl_int_is_neg(tab->mat->row[row][2]))
1277 continue;
1278 if (tab->row_sign)
1279 tab->row_sign[row] = isl_tab_row_neg;
1280 return row;
1282 for (row = tab->n_redundant; row < tab->n_row; ++row) {
1283 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
1284 continue;
1285 if (tab->row_sign) {
1286 if (tab->row_sign[row] == 0 &&
1287 is_obviously_neg(tab, row))
1288 tab->row_sign[row] = isl_tab_row_neg;
1289 if (tab->row_sign[row] != isl_tab_row_neg)
1290 continue;
1291 } else if (!is_obviously_neg(tab, row))
1292 continue;
1293 return row;
1295 return -1;
1298 /* Check whether the invariant that all columns are lexico-positive
1299 * is satisfied. This function is not called from the current code
1300 * but is useful during debugging.
1302 static void check_lexpos(struct isl_tab *tab) __attribute__ ((unused));
1303 static void check_lexpos(struct isl_tab *tab)
1305 unsigned off = 2 + tab->M;
1306 int col;
1307 int var;
1308 int row;
1310 for (col = tab->n_dead; col < tab->n_col; ++col) {
1311 if (tab->col_var[col] >= 0 &&
1312 (tab->col_var[col] < tab->n_param ||
1313 tab->col_var[col] >= tab->n_var - tab->n_div))
1314 continue;
1315 for (var = tab->n_param; var < tab->n_var - tab->n_div; ++var) {
1316 if (!tab->var[var].is_row) {
1317 if (tab->var[var].index == col)
1318 break;
1319 else
1320 continue;
1322 row = tab->var[var].index;
1323 if (isl_int_is_zero(tab->mat->row[row][off + col]))
1324 continue;
1325 if (isl_int_is_pos(tab->mat->row[row][off + col]))
1326 break;
1327 fprintf(stderr, "lexneg column %d (row %d)\n",
1328 col, row);
1330 if (var >= tab->n_var - tab->n_div)
1331 fprintf(stderr, "zero column %d\n", col);
1335 /* Report to the caller that the given constraint is part of an encountered
1336 * conflict.
1338 static int report_conflicting_constraint(struct isl_tab *tab, int con)
1340 return tab->conflict(con, tab->conflict_user);
1343 /* Given a conflicting row in the tableau, report all constraints
1344 * involved in the row to the caller. That is, the row itself
1345 * (if it represents a constraint) and all constraint columns with
1346 * non-zero (and therefore negative) coefficients.
1348 static int report_conflict(struct isl_tab *tab, int row)
1350 int j;
1351 isl_int *tr;
1353 if (!tab->conflict)
1354 return 0;
1356 if (tab->row_var[row] < 0 &&
1357 report_conflicting_constraint(tab, ~tab->row_var[row]) < 0)
1358 return -1;
1360 tr = tab->mat->row[row] + 2 + tab->M;
1362 for (j = tab->n_dead; j < tab->n_col; ++j) {
1363 if (tab->col_var[j] >= 0 &&
1364 (tab->col_var[j] < tab->n_param ||
1365 tab->col_var[j] >= tab->n_var - tab->n_div))
1366 continue;
1368 if (!isl_int_is_neg(tr[j]))
1369 continue;
1371 if (tab->col_var[j] < 0 &&
1372 report_conflicting_constraint(tab, ~tab->col_var[j]) < 0)
1373 return -1;
1376 return 0;
1379 /* Resolve all known or obviously violated constraints through pivoting.
1380 * In particular, as long as we can find any violated constraint, we
1381 * look for a pivoting column that would result in the lexicographically
1382 * smallest increment in the sample point. If there is no such column
1383 * then the tableau is infeasible.
1385 static int restore_lexmin(struct isl_tab *tab) WARN_UNUSED;
1386 static int restore_lexmin(struct isl_tab *tab)
1388 int row, col;
1390 if (!tab)
1391 return -1;
1392 if (tab->empty)
1393 return 0;
1394 while ((row = first_neg(tab)) != -1) {
1395 col = lexmin_pivot_col(tab, row);
1396 if (col >= tab->n_col) {
1397 if (report_conflict(tab, row) < 0)
1398 return -1;
1399 if (isl_tab_mark_empty(tab) < 0)
1400 return -1;
1401 return 0;
1403 if (col < 0)
1404 return -1;
1405 if (isl_tab_pivot(tab, row, col) < 0)
1406 return -1;
1408 return 0;
1411 /* Given a row that represents an equality, look for an appropriate
1412 * pivoting column.
1413 * In particular, if there are any non-zero coefficients among
1414 * the non-parameter variables, then we take the last of these
1415 * variables. Eliminating this variable in terms of the other
1416 * variables and/or parameters does not influence the property
1417 * that all column in the initial tableau are lexicographically
1418 * positive. The row corresponding to the eliminated variable
1419 * will only have non-zero entries below the diagonal of the
1420 * initial tableau. That is, we transform
1422 * I I
1423 * 1 into a
1424 * I I
1426 * If there is no such non-parameter variable, then we are dealing with
1427 * pure parameter equality and we pick any parameter with coefficient 1 or -1
1428 * for elimination. This will ensure that the eliminated parameter
1429 * always has an integer value whenever all the other parameters are integral.
1430 * If there is no such parameter then we return -1.
1432 static int last_var_col_or_int_par_col(struct isl_tab *tab, int row)
1434 unsigned off = 2 + tab->M;
1435 int i;
1437 for (i = tab->n_var - tab->n_div - 1; i >= 0 && i >= tab->n_param; --i) {
1438 int col;
1439 if (tab->var[i].is_row)
1440 continue;
1441 col = tab->var[i].index;
1442 if (col <= tab->n_dead)
1443 continue;
1444 if (!isl_int_is_zero(tab->mat->row[row][off + col]))
1445 return col;
1447 for (i = tab->n_dead; i < tab->n_col; ++i) {
1448 if (isl_int_is_one(tab->mat->row[row][off + i]))
1449 return i;
1450 if (isl_int_is_negone(tab->mat->row[row][off + i]))
1451 return i;
1453 return -1;
1456 /* Add an equality that is known to be valid to the tableau.
1457 * We first check if we can eliminate a variable or a parameter.
1458 * If not, we add the equality as two inequalities.
1459 * In this case, the equality was a pure parameter equality and there
1460 * is no need to resolve any constraint violations.
1462 * This function assumes that at least two more rows and at least
1463 * two more elements in the constraint array are available in the tableau.
1465 static struct isl_tab *add_lexmin_valid_eq(struct isl_tab *tab, isl_int *eq)
1467 int i;
1468 int r;
1470 if (!tab)
1471 return NULL;
1472 r = isl_tab_add_row(tab, eq);
1473 if (r < 0)
1474 goto error;
1476 r = tab->con[r].index;
1477 i = last_var_col_or_int_par_col(tab, r);
1478 if (i < 0) {
1479 tab->con[r].is_nonneg = 1;
1480 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1481 goto error;
1482 isl_seq_neg(eq, eq, 1 + tab->n_var);
1483 r = isl_tab_add_row(tab, eq);
1484 if (r < 0)
1485 goto error;
1486 tab->con[r].is_nonneg = 1;
1487 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1488 goto error;
1489 } else {
1490 if (isl_tab_pivot(tab, r, i) < 0)
1491 goto error;
1492 if (isl_tab_kill_col(tab, i) < 0)
1493 goto error;
1494 tab->n_eq++;
1497 return tab;
1498 error:
1499 isl_tab_free(tab);
1500 return NULL;
1503 /* Check if the given row is a pure constant.
1505 static int is_constant(struct isl_tab *tab, int row)
1507 unsigned off = 2 + tab->M;
1509 return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1510 tab->n_col - tab->n_dead) == -1;
1513 /* Add an equality that may or may not be valid to the tableau.
1514 * If the resulting row is a pure constant, then it must be zero.
1515 * Otherwise, the resulting tableau is empty.
1517 * If the row is not a pure constant, then we add two inequalities,
1518 * each time checking that they can be satisfied.
1519 * In the end we try to use one of the two constraints to eliminate
1520 * a column.
1522 * This function assumes that at least two more rows and at least
1523 * two more elements in the constraint array are available in the tableau.
1525 static int add_lexmin_eq(struct isl_tab *tab, isl_int *eq) WARN_UNUSED;
1526 static int add_lexmin_eq(struct isl_tab *tab, isl_int *eq)
1528 int r1, r2;
1529 int row;
1530 struct isl_tab_undo *snap;
1532 if (!tab)
1533 return -1;
1534 snap = isl_tab_snap(tab);
1535 r1 = isl_tab_add_row(tab, eq);
1536 if (r1 < 0)
1537 return -1;
1538 tab->con[r1].is_nonneg = 1;
1539 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r1]) < 0)
1540 return -1;
1542 row = tab->con[r1].index;
1543 if (is_constant(tab, row)) {
1544 if (!isl_int_is_zero(tab->mat->row[row][1]) ||
1545 (tab->M && !isl_int_is_zero(tab->mat->row[row][2]))) {
1546 if (isl_tab_mark_empty(tab) < 0)
1547 return -1;
1548 return 0;
1550 if (isl_tab_rollback(tab, snap) < 0)
1551 return -1;
1552 return 0;
1555 if (restore_lexmin(tab) < 0)
1556 return -1;
1557 if (tab->empty)
1558 return 0;
1560 isl_seq_neg(eq, eq, 1 + tab->n_var);
1562 r2 = isl_tab_add_row(tab, eq);
1563 if (r2 < 0)
1564 return -1;
1565 tab->con[r2].is_nonneg = 1;
1566 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r2]) < 0)
1567 return -1;
1569 if (restore_lexmin(tab) < 0)
1570 return -1;
1571 if (tab->empty)
1572 return 0;
1574 if (!tab->con[r1].is_row) {
1575 if (isl_tab_kill_col(tab, tab->con[r1].index) < 0)
1576 return -1;
1577 } else if (!tab->con[r2].is_row) {
1578 if (isl_tab_kill_col(tab, tab->con[r2].index) < 0)
1579 return -1;
1582 if (tab->bmap) {
1583 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1584 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1585 return -1;
1586 isl_seq_neg(eq, eq, 1 + tab->n_var);
1587 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1588 isl_seq_neg(eq, eq, 1 + tab->n_var);
1589 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1590 return -1;
1591 if (!tab->bmap)
1592 return -1;
1595 return 0;
1598 /* Add an inequality to the tableau, resolving violations using
1599 * restore_lexmin.
1601 * This function assumes that at least one more row and at least
1602 * one more element in the constraint array are available in the tableau.
1604 static struct isl_tab *add_lexmin_ineq(struct isl_tab *tab, isl_int *ineq)
1606 int r;
1608 if (!tab)
1609 return NULL;
1610 if (tab->bmap) {
1611 tab->bmap = isl_basic_map_add_ineq(tab->bmap, ineq);
1612 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1613 goto error;
1614 if (!tab->bmap)
1615 goto error;
1617 r = isl_tab_add_row(tab, ineq);
1618 if (r < 0)
1619 goto error;
1620 tab->con[r].is_nonneg = 1;
1621 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1622 goto error;
1623 if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1624 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1625 goto error;
1626 return tab;
1629 if (restore_lexmin(tab) < 0)
1630 goto error;
1631 if (!tab->empty && tab->con[r].is_row &&
1632 isl_tab_row_is_redundant(tab, tab->con[r].index))
1633 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1634 goto error;
1635 return tab;
1636 error:
1637 isl_tab_free(tab);
1638 return NULL;
1641 /* Check if the coefficients of the parameters are all integral.
1643 static int integer_parameter(struct isl_tab *tab, int row)
1645 int i;
1646 int col;
1647 unsigned off = 2 + tab->M;
1649 for (i = 0; i < tab->n_param; ++i) {
1650 /* Eliminated parameter */
1651 if (tab->var[i].is_row)
1652 continue;
1653 col = tab->var[i].index;
1654 if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
1655 tab->mat->row[row][0]))
1656 return 0;
1658 for (i = 0; i < tab->n_div; ++i) {
1659 if (tab->var[tab->n_var - tab->n_div + i].is_row)
1660 continue;
1661 col = tab->var[tab->n_var - tab->n_div + i].index;
1662 if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
1663 tab->mat->row[row][0]))
1664 return 0;
1666 return 1;
1669 /* Check if the coefficients of the non-parameter variables are all integral.
1671 static int integer_variable(struct isl_tab *tab, int row)
1673 int i;
1674 unsigned off = 2 + tab->M;
1676 for (i = tab->n_dead; i < tab->n_col; ++i) {
1677 if (tab->col_var[i] >= 0 &&
1678 (tab->col_var[i] < tab->n_param ||
1679 tab->col_var[i] >= tab->n_var - tab->n_div))
1680 continue;
1681 if (!isl_int_is_divisible_by(tab->mat->row[row][off + i],
1682 tab->mat->row[row][0]))
1683 return 0;
1685 return 1;
1688 /* Check if the constant term is integral.
1690 static int integer_constant(struct isl_tab *tab, int row)
1692 return isl_int_is_divisible_by(tab->mat->row[row][1],
1693 tab->mat->row[row][0]);
1696 #define I_CST 1 << 0
1697 #define I_PAR 1 << 1
1698 #define I_VAR 1 << 2
1700 /* Check for next (non-parameter) variable after "var" (first if var == -1)
1701 * that is non-integer and therefore requires a cut and return
1702 * the index of the variable.
1703 * For parametric tableaus, there are three parts in a row,
1704 * the constant, the coefficients of the parameters and the rest.
1705 * For each part, we check whether the coefficients in that part
1706 * are all integral and if so, set the corresponding flag in *f.
1707 * If the constant and the parameter part are integral, then the
1708 * current sample value is integral and no cut is required
1709 * (irrespective of whether the variable part is integral).
1711 static int next_non_integer_var(struct isl_tab *tab, int var, int *f)
1713 var = var < 0 ? tab->n_param : var + 1;
1715 for (; var < tab->n_var - tab->n_div; ++var) {
1716 int flags = 0;
1717 int row;
1718 if (!tab->var[var].is_row)
1719 continue;
1720 row = tab->var[var].index;
1721 if (integer_constant(tab, row))
1722 ISL_FL_SET(flags, I_CST);
1723 if (integer_parameter(tab, row))
1724 ISL_FL_SET(flags, I_PAR);
1725 if (ISL_FL_ISSET(flags, I_CST) && ISL_FL_ISSET(flags, I_PAR))
1726 continue;
1727 if (integer_variable(tab, row))
1728 ISL_FL_SET(flags, I_VAR);
1729 *f = flags;
1730 return var;
1732 return -1;
1735 /* Check for first (non-parameter) variable that is non-integer and
1736 * therefore requires a cut and return the corresponding row.
1737 * For parametric tableaus, there are three parts in a row,
1738 * the constant, the coefficients of the parameters and the rest.
1739 * For each part, we check whether the coefficients in that part
1740 * are all integral and if so, set the corresponding flag in *f.
1741 * If the constant and the parameter part are integral, then the
1742 * current sample value is integral and no cut is required
1743 * (irrespective of whether the variable part is integral).
1745 static int first_non_integer_row(struct isl_tab *tab, int *f)
1747 int var = next_non_integer_var(tab, -1, f);
1749 return var < 0 ? -1 : tab->var[var].index;
1752 /* Add a (non-parametric) cut to cut away the non-integral sample
1753 * value of the given row.
1755 * If the row is given by
1757 * m r = f + \sum_i a_i y_i
1759 * then the cut is
1761 * c = - {-f/m} + \sum_i {a_i/m} y_i >= 0
1763 * The big parameter, if any, is ignored, since it is assumed to be big
1764 * enough to be divisible by any integer.
1765 * If the tableau is actually a parametric tableau, then this function
1766 * is only called when all coefficients of the parameters are integral.
1767 * The cut therefore has zero coefficients for the parameters.
1769 * The current value is known to be negative, so row_sign, if it
1770 * exists, is set accordingly.
1772 * Return the row of the cut or -1.
1774 static int add_cut(struct isl_tab *tab, int row)
1776 int i;
1777 int r;
1778 isl_int *r_row;
1779 unsigned off = 2 + tab->M;
1781 if (isl_tab_extend_cons(tab, 1) < 0)
1782 return -1;
1783 r = isl_tab_allocate_con(tab);
1784 if (r < 0)
1785 return -1;
1787 r_row = tab->mat->row[tab->con[r].index];
1788 isl_int_set(r_row[0], tab->mat->row[row][0]);
1789 isl_int_neg(r_row[1], tab->mat->row[row][1]);
1790 isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
1791 isl_int_neg(r_row[1], r_row[1]);
1792 if (tab->M)
1793 isl_int_set_si(r_row[2], 0);
1794 for (i = 0; i < tab->n_col; ++i)
1795 isl_int_fdiv_r(r_row[off + i],
1796 tab->mat->row[row][off + i], tab->mat->row[row][0]);
1798 tab->con[r].is_nonneg = 1;
1799 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1800 return -1;
1801 if (tab->row_sign)
1802 tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
1804 return tab->con[r].index;
1807 #define CUT_ALL 1
1808 #define CUT_ONE 0
1810 /* Given a non-parametric tableau, add cuts until an integer
1811 * sample point is obtained or until the tableau is determined
1812 * to be integer infeasible.
1813 * As long as there is any non-integer value in the sample point,
1814 * we add appropriate cuts, if possible, for each of these
1815 * non-integer values and then resolve the violated
1816 * cut constraints using restore_lexmin.
1817 * If one of the corresponding rows is equal to an integral
1818 * combination of variables/constraints plus a non-integral constant,
1819 * then there is no way to obtain an integer point and we return
1820 * a tableau that is marked empty.
1821 * The parameter cutting_strategy controls the strategy used when adding cuts
1822 * to remove non-integer points. CUT_ALL adds all possible cuts
1823 * before continuing the search. CUT_ONE adds only one cut at a time.
1825 static struct isl_tab *cut_to_integer_lexmin(struct isl_tab *tab,
1826 int cutting_strategy)
1828 int var;
1829 int row;
1830 int flags;
1832 if (!tab)
1833 return NULL;
1834 if (tab->empty)
1835 return tab;
1837 while ((var = next_non_integer_var(tab, -1, &flags)) != -1) {
1838 do {
1839 if (ISL_FL_ISSET(flags, I_VAR)) {
1840 if (isl_tab_mark_empty(tab) < 0)
1841 goto error;
1842 return tab;
1844 row = tab->var[var].index;
1845 row = add_cut(tab, row);
1846 if (row < 0)
1847 goto error;
1848 if (cutting_strategy == CUT_ONE)
1849 break;
1850 } while ((var = next_non_integer_var(tab, var, &flags)) != -1);
1851 if (restore_lexmin(tab) < 0)
1852 goto error;
1853 if (tab->empty)
1854 break;
1856 return tab;
1857 error:
1858 isl_tab_free(tab);
1859 return NULL;
1862 /* Check whether all the currently active samples also satisfy the inequality
1863 * "ineq" (treated as an equality if eq is set).
1864 * Remove those samples that do not.
1866 static struct isl_tab *check_samples(struct isl_tab *tab, isl_int *ineq, int eq)
1868 int i;
1869 isl_int v;
1871 if (!tab)
1872 return NULL;
1874 isl_assert(tab->mat->ctx, tab->bmap, goto error);
1875 isl_assert(tab->mat->ctx, tab->samples, goto error);
1876 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, goto error);
1878 isl_int_init(v);
1879 for (i = tab->n_outside; i < tab->n_sample; ++i) {
1880 int sgn;
1881 isl_seq_inner_product(ineq, tab->samples->row[i],
1882 1 + tab->n_var, &v);
1883 sgn = isl_int_sgn(v);
1884 if (eq ? (sgn == 0) : (sgn >= 0))
1885 continue;
1886 tab = isl_tab_drop_sample(tab, i);
1887 if (!tab)
1888 break;
1890 isl_int_clear(v);
1892 return tab;
1893 error:
1894 isl_tab_free(tab);
1895 return NULL;
1898 /* Check whether the sample value of the tableau is finite,
1899 * i.e., either the tableau does not use a big parameter, or
1900 * all values of the variables are equal to the big parameter plus
1901 * some constant. This constant is the actual sample value.
1903 static int sample_is_finite(struct isl_tab *tab)
1905 int i;
1907 if (!tab->M)
1908 return 1;
1910 for (i = 0; i < tab->n_var; ++i) {
1911 int row;
1912 if (!tab->var[i].is_row)
1913 return 0;
1914 row = tab->var[i].index;
1915 if (isl_int_ne(tab->mat->row[row][0], tab->mat->row[row][2]))
1916 return 0;
1918 return 1;
1921 /* Check if the context tableau of sol has any integer points.
1922 * Leave tab in empty state if no integer point can be found.
1923 * If an integer point can be found and if moreover it is finite,
1924 * then it is added to the list of sample values.
1926 * This function is only called when none of the currently active sample
1927 * values satisfies the most recently added constraint.
1929 static struct isl_tab *check_integer_feasible(struct isl_tab *tab)
1931 struct isl_tab_undo *snap;
1933 if (!tab)
1934 return NULL;
1936 snap = isl_tab_snap(tab);
1937 if (isl_tab_push_basis(tab) < 0)
1938 goto error;
1940 tab = cut_to_integer_lexmin(tab, CUT_ALL);
1941 if (!tab)
1942 goto error;
1944 if (!tab->empty && sample_is_finite(tab)) {
1945 struct isl_vec *sample;
1947 sample = isl_tab_get_sample_value(tab);
1949 if (isl_tab_add_sample(tab, sample) < 0)
1950 goto error;
1953 if (!tab->empty && isl_tab_rollback(tab, snap) < 0)
1954 goto error;
1956 return tab;
1957 error:
1958 isl_tab_free(tab);
1959 return NULL;
1962 /* Check if any of the currently active sample values satisfies
1963 * the inequality "ineq" (an equality if eq is set).
1965 static int tab_has_valid_sample(struct isl_tab *tab, isl_int *ineq, int eq)
1967 int i;
1968 isl_int v;
1970 if (!tab)
1971 return -1;
1973 isl_assert(tab->mat->ctx, tab->bmap, return -1);
1974 isl_assert(tab->mat->ctx, tab->samples, return -1);
1975 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, return -1);
1977 isl_int_init(v);
1978 for (i = tab->n_outside; i < tab->n_sample; ++i) {
1979 int sgn;
1980 isl_seq_inner_product(ineq, tab->samples->row[i],
1981 1 + tab->n_var, &v);
1982 sgn = isl_int_sgn(v);
1983 if (eq ? (sgn == 0) : (sgn >= 0))
1984 break;
1986 isl_int_clear(v);
1988 return i < tab->n_sample;
1991 /* Insert a div specified by "div" to the tableau "tab" at position "pos" and
1992 * return isl_bool_true if the div is obviously non-negative.
1994 static isl_bool context_tab_insert_div(struct isl_tab *tab, int pos,
1995 __isl_keep isl_vec *div,
1996 int (*add_ineq)(void *user, isl_int *), void *user)
1998 int i;
1999 int r;
2000 struct isl_mat *samples;
2001 int nonneg;
2003 r = isl_tab_insert_div(tab, pos, div, add_ineq, user);
2004 if (r < 0)
2005 return isl_bool_error;
2006 nonneg = tab->var[r].is_nonneg;
2007 tab->var[r].frozen = 1;
2009 samples = isl_mat_extend(tab->samples,
2010 tab->n_sample, 1 + tab->n_var);
2011 tab->samples = samples;
2012 if (!samples)
2013 return isl_bool_error;
2014 for (i = tab->n_outside; i < samples->n_row; ++i) {
2015 isl_seq_inner_product(div->el + 1, samples->row[i],
2016 div->size - 1, &samples->row[i][samples->n_col - 1]);
2017 isl_int_fdiv_q(samples->row[i][samples->n_col - 1],
2018 samples->row[i][samples->n_col - 1], div->el[0]);
2020 tab->samples = isl_mat_move_cols(tab->samples, 1 + pos,
2021 1 + tab->n_var - 1, 1);
2022 if (!tab->samples)
2023 return isl_bool_error;
2025 return nonneg;
2028 /* Add a div specified by "div" to both the main tableau and
2029 * the context tableau. In case of the main tableau, we only
2030 * need to add an extra div. In the context tableau, we also
2031 * need to express the meaning of the div.
2032 * Return the index of the div or -1 if anything went wrong.
2034 * The new integer division is added before any unknown integer
2035 * divisions in the context to ensure that it does not get
2036 * equated to some linear combination involving unknown integer
2037 * divisions.
2039 static int add_div(struct isl_tab *tab, struct isl_context *context,
2040 __isl_keep isl_vec *div)
2042 int r;
2043 int pos;
2044 isl_bool nonneg;
2045 struct isl_tab *context_tab = context->op->peek_tab(context);
2047 if (!tab || !context_tab)
2048 goto error;
2050 pos = context_tab->n_var - context->n_unknown;
2051 if ((nonneg = context->op->insert_div(context, pos, div)) < 0)
2052 goto error;
2054 if (!context->op->is_ok(context))
2055 goto error;
2057 pos = tab->n_var - context->n_unknown;
2058 if (isl_tab_extend_vars(tab, 1) < 0)
2059 goto error;
2060 r = isl_tab_insert_var(tab, pos);
2061 if (r < 0)
2062 goto error;
2063 if (nonneg)
2064 tab->var[r].is_nonneg = 1;
2065 tab->var[r].frozen = 1;
2066 tab->n_div++;
2068 return tab->n_div - 1 - context->n_unknown;
2069 error:
2070 context->op->invalidate(context);
2071 return -1;
2074 static int find_div(struct isl_tab *tab, isl_int *div, isl_int denom)
2076 int i;
2077 unsigned total = isl_basic_map_total_dim(tab->bmap);
2079 for (i = 0; i < tab->bmap->n_div; ++i) {
2080 if (isl_int_ne(tab->bmap->div[i][0], denom))
2081 continue;
2082 if (!isl_seq_eq(tab->bmap->div[i] + 1, div, 1 + total))
2083 continue;
2084 return i;
2086 return -1;
2089 /* Return the index of a div that corresponds to "div".
2090 * We first check if we already have such a div and if not, we create one.
2092 static int get_div(struct isl_tab *tab, struct isl_context *context,
2093 struct isl_vec *div)
2095 int d;
2096 struct isl_tab *context_tab = context->op->peek_tab(context);
2098 if (!context_tab)
2099 return -1;
2101 d = find_div(context_tab, div->el + 1, div->el[0]);
2102 if (d != -1)
2103 return d;
2105 return add_div(tab, context, div);
2108 /* Add a parametric cut to cut away the non-integral sample value
2109 * of the give row.
2110 * Let a_i be the coefficients of the constant term and the parameters
2111 * and let b_i be the coefficients of the variables or constraints
2112 * in basis of the tableau.
2113 * Let q be the div q = floor(\sum_i {-a_i} y_i).
2115 * The cut is expressed as
2117 * c = \sum_i -{-a_i} y_i + \sum_i {b_i} x_i + q >= 0
2119 * If q did not already exist in the context tableau, then it is added first.
2120 * If q is in a column of the main tableau then the "+ q" can be accomplished
2121 * by setting the corresponding entry to the denominator of the constraint.
2122 * If q happens to be in a row of the main tableau, then the corresponding
2123 * row needs to be added instead (taking care of the denominators).
2124 * Note that this is very unlikely, but perhaps not entirely impossible.
2126 * The current value of the cut is known to be negative (or at least
2127 * non-positive), so row_sign is set accordingly.
2129 * Return the row of the cut or -1.
2131 static int add_parametric_cut(struct isl_tab *tab, int row,
2132 struct isl_context *context)
2134 struct isl_vec *div;
2135 int d;
2136 int i;
2137 int r;
2138 isl_int *r_row;
2139 int col;
2140 int n;
2141 unsigned off = 2 + tab->M;
2143 if (!context)
2144 return -1;
2146 div = get_row_parameter_div(tab, row);
2147 if (!div)
2148 return -1;
2150 n = tab->n_div - context->n_unknown;
2151 d = context->op->get_div(context, tab, div);
2152 isl_vec_free(div);
2153 if (d < 0)
2154 return -1;
2156 if (isl_tab_extend_cons(tab, 1) < 0)
2157 return -1;
2158 r = isl_tab_allocate_con(tab);
2159 if (r < 0)
2160 return -1;
2162 r_row = tab->mat->row[tab->con[r].index];
2163 isl_int_set(r_row[0], tab->mat->row[row][0]);
2164 isl_int_neg(r_row[1], tab->mat->row[row][1]);
2165 isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
2166 isl_int_neg(r_row[1], r_row[1]);
2167 if (tab->M)
2168 isl_int_set_si(r_row[2], 0);
2169 for (i = 0; i < tab->n_param; ++i) {
2170 if (tab->var[i].is_row)
2171 continue;
2172 col = tab->var[i].index;
2173 isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
2174 isl_int_fdiv_r(r_row[off + col], r_row[off + col],
2175 tab->mat->row[row][0]);
2176 isl_int_neg(r_row[off + col], r_row[off + col]);
2178 for (i = 0; i < tab->n_div; ++i) {
2179 if (tab->var[tab->n_var - tab->n_div + i].is_row)
2180 continue;
2181 col = tab->var[tab->n_var - tab->n_div + i].index;
2182 isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
2183 isl_int_fdiv_r(r_row[off + col], r_row[off + col],
2184 tab->mat->row[row][0]);
2185 isl_int_neg(r_row[off + col], r_row[off + col]);
2187 for (i = 0; i < tab->n_col; ++i) {
2188 if (tab->col_var[i] >= 0 &&
2189 (tab->col_var[i] < tab->n_param ||
2190 tab->col_var[i] >= tab->n_var - tab->n_div))
2191 continue;
2192 isl_int_fdiv_r(r_row[off + i],
2193 tab->mat->row[row][off + i], tab->mat->row[row][0]);
2195 if (tab->var[tab->n_var - tab->n_div + d].is_row) {
2196 isl_int gcd;
2197 int d_row = tab->var[tab->n_var - tab->n_div + d].index;
2198 isl_int_init(gcd);
2199 isl_int_gcd(gcd, tab->mat->row[d_row][0], r_row[0]);
2200 isl_int_divexact(r_row[0], r_row[0], gcd);
2201 isl_int_divexact(gcd, tab->mat->row[d_row][0], gcd);
2202 isl_seq_combine(r_row + 1, gcd, r_row + 1,
2203 r_row[0], tab->mat->row[d_row] + 1,
2204 off - 1 + tab->n_col);
2205 isl_int_mul(r_row[0], r_row[0], tab->mat->row[d_row][0]);
2206 isl_int_clear(gcd);
2207 } else {
2208 col = tab->var[tab->n_var - tab->n_div + d].index;
2209 isl_int_set(r_row[off + col], tab->mat->row[row][0]);
2212 tab->con[r].is_nonneg = 1;
2213 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
2214 return -1;
2215 if (tab->row_sign)
2216 tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
2218 row = tab->con[r].index;
2220 if (d >= n && context->op->detect_equalities(context, tab) < 0)
2221 return -1;
2223 return row;
2226 /* Construct a tableau for bmap that can be used for computing
2227 * the lexicographic minimum (or maximum) of bmap.
2228 * If not NULL, then dom is the domain where the minimum
2229 * should be computed. In this case, we set up a parametric
2230 * tableau with row signs (initialized to "unknown").
2231 * If M is set, then the tableau will use a big parameter.
2232 * If max is set, then a maximum should be computed instead of a minimum.
2233 * This means that for each variable x, the tableau will contain the variable
2234 * x' = M - x, rather than x' = M + x. This in turn means that the coefficient
2235 * of the variables in all constraints are negated prior to adding them
2236 * to the tableau.
2238 static struct isl_tab *tab_for_lexmin(struct isl_basic_map *bmap,
2239 struct isl_basic_set *dom, unsigned M, int max)
2241 int i;
2242 struct isl_tab *tab;
2243 unsigned n_var;
2244 unsigned o_var;
2246 tab = isl_tab_alloc(bmap->ctx, 2 * bmap->n_eq + bmap->n_ineq + 1,
2247 isl_basic_map_total_dim(bmap), M);
2248 if (!tab)
2249 return NULL;
2251 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
2252 if (dom) {
2253 tab->n_param = isl_basic_set_total_dim(dom) - dom->n_div;
2254 tab->n_div = dom->n_div;
2255 tab->row_sign = isl_calloc_array(bmap->ctx,
2256 enum isl_tab_row_sign, tab->mat->n_row);
2257 if (tab->mat->n_row && !tab->row_sign)
2258 goto error;
2260 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
2261 if (isl_tab_mark_empty(tab) < 0)
2262 goto error;
2263 return tab;
2266 for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
2267 tab->var[i].is_nonneg = 1;
2268 tab->var[i].frozen = 1;
2270 o_var = 1 + tab->n_param;
2271 n_var = tab->n_var - tab->n_param - tab->n_div;
2272 for (i = 0; i < bmap->n_eq; ++i) {
2273 if (max)
2274 isl_seq_neg(bmap->eq[i] + o_var,
2275 bmap->eq[i] + o_var, n_var);
2276 tab = add_lexmin_valid_eq(tab, bmap->eq[i]);
2277 if (max)
2278 isl_seq_neg(bmap->eq[i] + o_var,
2279 bmap->eq[i] + o_var, n_var);
2280 if (!tab || tab->empty)
2281 return tab;
2283 if (bmap->n_eq && restore_lexmin(tab) < 0)
2284 goto error;
2285 for (i = 0; i < bmap->n_ineq; ++i) {
2286 if (max)
2287 isl_seq_neg(bmap->ineq[i] + o_var,
2288 bmap->ineq[i] + o_var, n_var);
2289 tab = add_lexmin_ineq(tab, bmap->ineq[i]);
2290 if (max)
2291 isl_seq_neg(bmap->ineq[i] + o_var,
2292 bmap->ineq[i] + o_var, n_var);
2293 if (!tab || tab->empty)
2294 return tab;
2296 return tab;
2297 error:
2298 isl_tab_free(tab);
2299 return NULL;
2302 /* Given a main tableau where more than one row requires a split,
2303 * determine and return the "best" row to split on.
2305 * Given two rows in the main tableau, if the inequality corresponding
2306 * to the first row is redundant with respect to that of the second row
2307 * in the current tableau, then it is better to split on the second row,
2308 * since in the positive part, both rows will be positive.
2309 * (In the negative part a pivot will have to be performed and just about
2310 * anything can happen to the sign of the other row.)
2312 * As a simple heuristic, we therefore select the row that makes the most
2313 * of the other rows redundant.
2315 * Perhaps it would also be useful to look at the number of constraints
2316 * that conflict with any given constraint.
2318 * best is the best row so far (-1 when we have not found any row yet).
2319 * best_r is the number of other rows made redundant by row best.
2320 * When best is still -1, bset_r is meaningless, but it is initialized
2321 * to some arbitrary value (0) anyway. Without this redundant initialization
2322 * valgrind may warn about uninitialized memory accesses when isl
2323 * is compiled with some versions of gcc.
2325 static int best_split(struct isl_tab *tab, struct isl_tab *context_tab)
2327 struct isl_tab_undo *snap;
2328 int split;
2329 int row;
2330 int best = -1;
2331 int best_r = 0;
2333 if (isl_tab_extend_cons(context_tab, 2) < 0)
2334 return -1;
2336 snap = isl_tab_snap(context_tab);
2338 for (split = tab->n_redundant; split < tab->n_row; ++split) {
2339 struct isl_tab_undo *snap2;
2340 struct isl_vec *ineq = NULL;
2341 int r = 0;
2342 int ok;
2344 if (!isl_tab_var_from_row(tab, split)->is_nonneg)
2345 continue;
2346 if (tab->row_sign[split] != isl_tab_row_any)
2347 continue;
2349 ineq = get_row_parameter_ineq(tab, split);
2350 if (!ineq)
2351 return -1;
2352 ok = isl_tab_add_ineq(context_tab, ineq->el) >= 0;
2353 isl_vec_free(ineq);
2354 if (!ok)
2355 return -1;
2357 snap2 = isl_tab_snap(context_tab);
2359 for (row = tab->n_redundant; row < tab->n_row; ++row) {
2360 struct isl_tab_var *var;
2362 if (row == split)
2363 continue;
2364 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
2365 continue;
2366 if (tab->row_sign[row] != isl_tab_row_any)
2367 continue;
2369 ineq = get_row_parameter_ineq(tab, row);
2370 if (!ineq)
2371 return -1;
2372 ok = isl_tab_add_ineq(context_tab, ineq->el) >= 0;
2373 isl_vec_free(ineq);
2374 if (!ok)
2375 return -1;
2376 var = &context_tab->con[context_tab->n_con - 1];
2377 if (!context_tab->empty &&
2378 !isl_tab_min_at_most_neg_one(context_tab, var))
2379 r++;
2380 if (isl_tab_rollback(context_tab, snap2) < 0)
2381 return -1;
2383 if (best == -1 || r > best_r) {
2384 best = split;
2385 best_r = r;
2387 if (isl_tab_rollback(context_tab, snap) < 0)
2388 return -1;
2391 return best;
2394 static struct isl_basic_set *context_lex_peek_basic_set(
2395 struct isl_context *context)
2397 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2398 if (!clex->tab)
2399 return NULL;
2400 return isl_tab_peek_bset(clex->tab);
2403 static struct isl_tab *context_lex_peek_tab(struct isl_context *context)
2405 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2406 return clex->tab;
2409 static void context_lex_add_eq(struct isl_context *context, isl_int *eq,
2410 int check, int update)
2412 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2413 if (isl_tab_extend_cons(clex->tab, 2) < 0)
2414 goto error;
2415 if (add_lexmin_eq(clex->tab, eq) < 0)
2416 goto error;
2417 if (check) {
2418 int v = tab_has_valid_sample(clex->tab, eq, 1);
2419 if (v < 0)
2420 goto error;
2421 if (!v)
2422 clex->tab = check_integer_feasible(clex->tab);
2424 if (update)
2425 clex->tab = check_samples(clex->tab, eq, 1);
2426 return;
2427 error:
2428 isl_tab_free(clex->tab);
2429 clex->tab = NULL;
2432 static void context_lex_add_ineq(struct isl_context *context, isl_int *ineq,
2433 int check, int update)
2435 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2436 if (isl_tab_extend_cons(clex->tab, 1) < 0)
2437 goto error;
2438 clex->tab = add_lexmin_ineq(clex->tab, ineq);
2439 if (check) {
2440 int v = tab_has_valid_sample(clex->tab, ineq, 0);
2441 if (v < 0)
2442 goto error;
2443 if (!v)
2444 clex->tab = check_integer_feasible(clex->tab);
2446 if (update)
2447 clex->tab = check_samples(clex->tab, ineq, 0);
2448 return;
2449 error:
2450 isl_tab_free(clex->tab);
2451 clex->tab = NULL;
2454 static int context_lex_add_ineq_wrap(void *user, isl_int *ineq)
2456 struct isl_context *context = (struct isl_context *)user;
2457 context_lex_add_ineq(context, ineq, 0, 0);
2458 return context->op->is_ok(context) ? 0 : -1;
2461 /* Check which signs can be obtained by "ineq" on all the currently
2462 * active sample values. See row_sign for more information.
2464 static enum isl_tab_row_sign tab_ineq_sign(struct isl_tab *tab, isl_int *ineq,
2465 int strict)
2467 int i;
2468 int sgn;
2469 isl_int tmp;
2470 enum isl_tab_row_sign res = isl_tab_row_unknown;
2472 isl_assert(tab->mat->ctx, tab->samples, return isl_tab_row_unknown);
2473 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var,
2474 return isl_tab_row_unknown);
2476 isl_int_init(tmp);
2477 for (i = tab->n_outside; i < tab->n_sample; ++i) {
2478 isl_seq_inner_product(tab->samples->row[i], ineq,
2479 1 + tab->n_var, &tmp);
2480 sgn = isl_int_sgn(tmp);
2481 if (sgn > 0 || (sgn == 0 && strict)) {
2482 if (res == isl_tab_row_unknown)
2483 res = isl_tab_row_pos;
2484 if (res == isl_tab_row_neg)
2485 res = isl_tab_row_any;
2487 if (sgn < 0) {
2488 if (res == isl_tab_row_unknown)
2489 res = isl_tab_row_neg;
2490 if (res == isl_tab_row_pos)
2491 res = isl_tab_row_any;
2493 if (res == isl_tab_row_any)
2494 break;
2496 isl_int_clear(tmp);
2498 return res;
2501 static enum isl_tab_row_sign context_lex_ineq_sign(struct isl_context *context,
2502 isl_int *ineq, int strict)
2504 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2505 return tab_ineq_sign(clex->tab, ineq, strict);
2508 /* Check whether "ineq" can be added to the tableau without rendering
2509 * it infeasible.
2511 static int context_lex_test_ineq(struct isl_context *context, isl_int *ineq)
2513 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2514 struct isl_tab_undo *snap;
2515 int feasible;
2517 if (!clex->tab)
2518 return -1;
2520 if (isl_tab_extend_cons(clex->tab, 1) < 0)
2521 return -1;
2523 snap = isl_tab_snap(clex->tab);
2524 if (isl_tab_push_basis(clex->tab) < 0)
2525 return -1;
2526 clex->tab = add_lexmin_ineq(clex->tab, ineq);
2527 clex->tab = check_integer_feasible(clex->tab);
2528 if (!clex->tab)
2529 return -1;
2530 feasible = !clex->tab->empty;
2531 if (isl_tab_rollback(clex->tab, snap) < 0)
2532 return -1;
2534 return feasible;
2537 static int context_lex_get_div(struct isl_context *context, struct isl_tab *tab,
2538 struct isl_vec *div)
2540 return get_div(tab, context, div);
2543 /* Insert a div specified by "div" to the context tableau at position "pos" and
2544 * return isl_bool_true if the div is obviously non-negative.
2545 * context_tab_add_div will always return isl_bool_true, because all variables
2546 * in a isl_context_lex tableau are non-negative.
2547 * However, if we are using a big parameter in the context, then this only
2548 * reflects the non-negativity of the variable used to _encode_ the
2549 * div, i.e., div' = M + div, so we can't draw any conclusions.
2551 static isl_bool context_lex_insert_div(struct isl_context *context, int pos,
2552 __isl_keep isl_vec *div)
2554 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2555 isl_bool nonneg;
2556 nonneg = context_tab_insert_div(clex->tab, pos, div,
2557 context_lex_add_ineq_wrap, context);
2558 if (nonneg < 0)
2559 return isl_bool_error;
2560 if (clex->tab->M)
2561 return isl_bool_false;
2562 return nonneg;
2565 static int context_lex_detect_equalities(struct isl_context *context,
2566 struct isl_tab *tab)
2568 return 0;
2571 static int context_lex_best_split(struct isl_context *context,
2572 struct isl_tab *tab)
2574 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2575 struct isl_tab_undo *snap;
2576 int r;
2578 snap = isl_tab_snap(clex->tab);
2579 if (isl_tab_push_basis(clex->tab) < 0)
2580 return -1;
2581 r = best_split(tab, clex->tab);
2583 if (r >= 0 && isl_tab_rollback(clex->tab, snap) < 0)
2584 return -1;
2586 return r;
2589 static int context_lex_is_empty(struct isl_context *context)
2591 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2592 if (!clex->tab)
2593 return -1;
2594 return clex->tab->empty;
2597 static void *context_lex_save(struct isl_context *context)
2599 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2600 struct isl_tab_undo *snap;
2602 snap = isl_tab_snap(clex->tab);
2603 if (isl_tab_push_basis(clex->tab) < 0)
2604 return NULL;
2605 if (isl_tab_save_samples(clex->tab) < 0)
2606 return NULL;
2608 return snap;
2611 static void context_lex_restore(struct isl_context *context, void *save)
2613 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2614 if (isl_tab_rollback(clex->tab, (struct isl_tab_undo *)save) < 0) {
2615 isl_tab_free(clex->tab);
2616 clex->tab = NULL;
2620 static void context_lex_discard(void *save)
2624 static int context_lex_is_ok(struct isl_context *context)
2626 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2627 return !!clex->tab;
2630 /* For each variable in the context tableau, check if the variable can
2631 * only attain non-negative values. If so, mark the parameter as non-negative
2632 * in the main tableau. This allows for a more direct identification of some
2633 * cases of violated constraints.
2635 static struct isl_tab *tab_detect_nonnegative_parameters(struct isl_tab *tab,
2636 struct isl_tab *context_tab)
2638 int i;
2639 struct isl_tab_undo *snap;
2640 struct isl_vec *ineq = NULL;
2641 struct isl_tab_var *var;
2642 int n;
2644 if (context_tab->n_var == 0)
2645 return tab;
2647 ineq = isl_vec_alloc(tab->mat->ctx, 1 + context_tab->n_var);
2648 if (!ineq)
2649 goto error;
2651 if (isl_tab_extend_cons(context_tab, 1) < 0)
2652 goto error;
2654 snap = isl_tab_snap(context_tab);
2656 n = 0;
2657 isl_seq_clr(ineq->el, ineq->size);
2658 for (i = 0; i < context_tab->n_var; ++i) {
2659 isl_int_set_si(ineq->el[1 + i], 1);
2660 if (isl_tab_add_ineq(context_tab, ineq->el) < 0)
2661 goto error;
2662 var = &context_tab->con[context_tab->n_con - 1];
2663 if (!context_tab->empty &&
2664 !isl_tab_min_at_most_neg_one(context_tab, var)) {
2665 int j = i;
2666 if (i >= tab->n_param)
2667 j = i - tab->n_param + tab->n_var - tab->n_div;
2668 tab->var[j].is_nonneg = 1;
2669 n++;
2671 isl_int_set_si(ineq->el[1 + i], 0);
2672 if (isl_tab_rollback(context_tab, snap) < 0)
2673 goto error;
2676 if (context_tab->M && n == context_tab->n_var) {
2677 context_tab->mat = isl_mat_drop_cols(context_tab->mat, 2, 1);
2678 context_tab->M = 0;
2681 isl_vec_free(ineq);
2682 return tab;
2683 error:
2684 isl_vec_free(ineq);
2685 isl_tab_free(tab);
2686 return NULL;
2689 static struct isl_tab *context_lex_detect_nonnegative_parameters(
2690 struct isl_context *context, struct isl_tab *tab)
2692 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2693 struct isl_tab_undo *snap;
2695 if (!tab)
2696 return NULL;
2698 snap = isl_tab_snap(clex->tab);
2699 if (isl_tab_push_basis(clex->tab) < 0)
2700 goto error;
2702 tab = tab_detect_nonnegative_parameters(tab, clex->tab);
2704 if (isl_tab_rollback(clex->tab, snap) < 0)
2705 goto error;
2707 return tab;
2708 error:
2709 isl_tab_free(tab);
2710 return NULL;
2713 static void context_lex_invalidate(struct isl_context *context)
2715 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2716 isl_tab_free(clex->tab);
2717 clex->tab = NULL;
2720 static __isl_null struct isl_context *context_lex_free(
2721 struct isl_context *context)
2723 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2724 isl_tab_free(clex->tab);
2725 free(clex);
2727 return NULL;
2730 struct isl_context_op isl_context_lex_op = {
2731 context_lex_detect_nonnegative_parameters,
2732 context_lex_peek_basic_set,
2733 context_lex_peek_tab,
2734 context_lex_add_eq,
2735 context_lex_add_ineq,
2736 context_lex_ineq_sign,
2737 context_lex_test_ineq,
2738 context_lex_get_div,
2739 context_lex_insert_div,
2740 context_lex_detect_equalities,
2741 context_lex_best_split,
2742 context_lex_is_empty,
2743 context_lex_is_ok,
2744 context_lex_save,
2745 context_lex_restore,
2746 context_lex_discard,
2747 context_lex_invalidate,
2748 context_lex_free,
2751 static struct isl_tab *context_tab_for_lexmin(struct isl_basic_set *bset)
2753 struct isl_tab *tab;
2755 if (!bset)
2756 return NULL;
2757 tab = tab_for_lexmin(bset_to_bmap(bset), NULL, 1, 0);
2758 if (!tab)
2759 goto error;
2760 if (isl_tab_track_bset(tab, bset) < 0)
2761 goto error;
2762 tab = isl_tab_init_samples(tab);
2763 return tab;
2764 error:
2765 isl_basic_set_free(bset);
2766 return NULL;
2769 static struct isl_context *isl_context_lex_alloc(struct isl_basic_set *dom)
2771 struct isl_context_lex *clex;
2773 if (!dom)
2774 return NULL;
2776 clex = isl_alloc_type(dom->ctx, struct isl_context_lex);
2777 if (!clex)
2778 return NULL;
2780 clex->context.op = &isl_context_lex_op;
2782 clex->tab = context_tab_for_lexmin(isl_basic_set_copy(dom));
2783 if (restore_lexmin(clex->tab) < 0)
2784 goto error;
2785 clex->tab = check_integer_feasible(clex->tab);
2786 if (!clex->tab)
2787 goto error;
2789 return &clex->context;
2790 error:
2791 clex->context.op->free(&clex->context);
2792 return NULL;
2795 /* Representation of the context when using generalized basis reduction.
2797 * "shifted" contains the offsets of the unit hypercubes that lie inside the
2798 * context. Any rational point in "shifted" can therefore be rounded
2799 * up to an integer point in the context.
2800 * If the context is constrained by any equality, then "shifted" is not used
2801 * as it would be empty.
2803 struct isl_context_gbr {
2804 struct isl_context context;
2805 struct isl_tab *tab;
2806 struct isl_tab *shifted;
2807 struct isl_tab *cone;
2810 static struct isl_tab *context_gbr_detect_nonnegative_parameters(
2811 struct isl_context *context, struct isl_tab *tab)
2813 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2814 if (!tab)
2815 return NULL;
2816 return tab_detect_nonnegative_parameters(tab, cgbr->tab);
2819 static struct isl_basic_set *context_gbr_peek_basic_set(
2820 struct isl_context *context)
2822 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2823 if (!cgbr->tab)
2824 return NULL;
2825 return isl_tab_peek_bset(cgbr->tab);
2828 static struct isl_tab *context_gbr_peek_tab(struct isl_context *context)
2830 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2831 return cgbr->tab;
2834 /* Initialize the "shifted" tableau of the context, which
2835 * contains the constraints of the original tableau shifted
2836 * by the sum of all negative coefficients. This ensures
2837 * that any rational point in the shifted tableau can
2838 * be rounded up to yield an integer point in the original tableau.
2840 static void gbr_init_shifted(struct isl_context_gbr *cgbr)
2842 int i, j;
2843 struct isl_vec *cst;
2844 struct isl_basic_set *bset = isl_tab_peek_bset(cgbr->tab);
2845 unsigned dim = isl_basic_set_total_dim(bset);
2847 cst = isl_vec_alloc(cgbr->tab->mat->ctx, bset->n_ineq);
2848 if (!cst)
2849 return;
2851 for (i = 0; i < bset->n_ineq; ++i) {
2852 isl_int_set(cst->el[i], bset->ineq[i][0]);
2853 for (j = 0; j < dim; ++j) {
2854 if (!isl_int_is_neg(bset->ineq[i][1 + j]))
2855 continue;
2856 isl_int_add(bset->ineq[i][0], bset->ineq[i][0],
2857 bset->ineq[i][1 + j]);
2861 cgbr->shifted = isl_tab_from_basic_set(bset, 0);
2863 for (i = 0; i < bset->n_ineq; ++i)
2864 isl_int_set(bset->ineq[i][0], cst->el[i]);
2866 isl_vec_free(cst);
2869 /* Check if the shifted tableau is non-empty, and if so
2870 * use the sample point to construct an integer point
2871 * of the context tableau.
2873 static struct isl_vec *gbr_get_shifted_sample(struct isl_context_gbr *cgbr)
2875 struct isl_vec *sample;
2877 if (!cgbr->shifted)
2878 gbr_init_shifted(cgbr);
2879 if (!cgbr->shifted)
2880 return NULL;
2881 if (cgbr->shifted->empty)
2882 return isl_vec_alloc(cgbr->tab->mat->ctx, 0);
2884 sample = isl_tab_get_sample_value(cgbr->shifted);
2885 sample = isl_vec_ceil(sample);
2887 return sample;
2890 static struct isl_basic_set *drop_constant_terms(struct isl_basic_set *bset)
2892 int i;
2894 if (!bset)
2895 return NULL;
2897 for (i = 0; i < bset->n_eq; ++i)
2898 isl_int_set_si(bset->eq[i][0], 0);
2900 for (i = 0; i < bset->n_ineq; ++i)
2901 isl_int_set_si(bset->ineq[i][0], 0);
2903 return bset;
2906 static int use_shifted(struct isl_context_gbr *cgbr)
2908 if (!cgbr->tab)
2909 return 0;
2910 return cgbr->tab->bmap->n_eq == 0 && cgbr->tab->bmap->n_div == 0;
2913 static struct isl_vec *gbr_get_sample(struct isl_context_gbr *cgbr)
2915 struct isl_basic_set *bset;
2916 struct isl_basic_set *cone;
2918 if (isl_tab_sample_is_integer(cgbr->tab))
2919 return isl_tab_get_sample_value(cgbr->tab);
2921 if (use_shifted(cgbr)) {
2922 struct isl_vec *sample;
2924 sample = gbr_get_shifted_sample(cgbr);
2925 if (!sample || sample->size > 0)
2926 return sample;
2928 isl_vec_free(sample);
2931 if (!cgbr->cone) {
2932 bset = isl_tab_peek_bset(cgbr->tab);
2933 cgbr->cone = isl_tab_from_recession_cone(bset, 0);
2934 if (!cgbr->cone)
2935 return NULL;
2936 if (isl_tab_track_bset(cgbr->cone,
2937 isl_basic_set_copy(bset)) < 0)
2938 return NULL;
2940 if (isl_tab_detect_implicit_equalities(cgbr->cone) < 0)
2941 return NULL;
2943 if (cgbr->cone->n_dead == cgbr->cone->n_col) {
2944 struct isl_vec *sample;
2945 struct isl_tab_undo *snap;
2947 if (cgbr->tab->basis) {
2948 if (cgbr->tab->basis->n_col != 1 + cgbr->tab->n_var) {
2949 isl_mat_free(cgbr->tab->basis);
2950 cgbr->tab->basis = NULL;
2952 cgbr->tab->n_zero = 0;
2953 cgbr->tab->n_unbounded = 0;
2956 snap = isl_tab_snap(cgbr->tab);
2958 sample = isl_tab_sample(cgbr->tab);
2960 if (!sample || isl_tab_rollback(cgbr->tab, snap) < 0) {
2961 isl_vec_free(sample);
2962 return NULL;
2965 return sample;
2968 cone = isl_basic_set_dup(isl_tab_peek_bset(cgbr->cone));
2969 cone = drop_constant_terms(cone);
2970 cone = isl_basic_set_update_from_tab(cone, cgbr->cone);
2971 cone = isl_basic_set_underlying_set(cone);
2972 cone = isl_basic_set_gauss(cone, NULL);
2974 bset = isl_basic_set_dup(isl_tab_peek_bset(cgbr->tab));
2975 bset = isl_basic_set_update_from_tab(bset, cgbr->tab);
2976 bset = isl_basic_set_underlying_set(bset);
2977 bset = isl_basic_set_gauss(bset, NULL);
2979 return isl_basic_set_sample_with_cone(bset, cone);
2982 static void check_gbr_integer_feasible(struct isl_context_gbr *cgbr)
2984 struct isl_vec *sample;
2986 if (!cgbr->tab)
2987 return;
2989 if (cgbr->tab->empty)
2990 return;
2992 sample = gbr_get_sample(cgbr);
2993 if (!sample)
2994 goto error;
2996 if (sample->size == 0) {
2997 isl_vec_free(sample);
2998 if (isl_tab_mark_empty(cgbr->tab) < 0)
2999 goto error;
3000 return;
3003 if (isl_tab_add_sample(cgbr->tab, sample) < 0)
3004 goto error;
3006 return;
3007 error:
3008 isl_tab_free(cgbr->tab);
3009 cgbr->tab = NULL;
3012 static struct isl_tab *add_gbr_eq(struct isl_tab *tab, isl_int *eq)
3014 if (!tab)
3015 return NULL;
3017 if (isl_tab_extend_cons(tab, 2) < 0)
3018 goto error;
3020 if (isl_tab_add_eq(tab, eq) < 0)
3021 goto error;
3023 return tab;
3024 error:
3025 isl_tab_free(tab);
3026 return NULL;
3029 /* Add the equality described by "eq" to the context.
3030 * If "check" is set, then we check if the context is empty after
3031 * adding the equality.
3032 * If "update" is set, then we check if the samples are still valid.
3034 * We do not explicitly add shifted copies of the equality to
3035 * cgbr->shifted since they would conflict with each other.
3036 * Instead, we directly mark cgbr->shifted empty.
3038 static void context_gbr_add_eq(struct isl_context *context, isl_int *eq,
3039 int check, int update)
3041 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3043 cgbr->tab = add_gbr_eq(cgbr->tab, eq);
3045 if (cgbr->shifted && !cgbr->shifted->empty && use_shifted(cgbr)) {
3046 if (isl_tab_mark_empty(cgbr->shifted) < 0)
3047 goto error;
3050 if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
3051 if (isl_tab_extend_cons(cgbr->cone, 2) < 0)
3052 goto error;
3053 if (isl_tab_add_eq(cgbr->cone, eq) < 0)
3054 goto error;
3057 if (check) {
3058 int v = tab_has_valid_sample(cgbr->tab, eq, 1);
3059 if (v < 0)
3060 goto error;
3061 if (!v)
3062 check_gbr_integer_feasible(cgbr);
3064 if (update)
3065 cgbr->tab = check_samples(cgbr->tab, eq, 1);
3066 return;
3067 error:
3068 isl_tab_free(cgbr->tab);
3069 cgbr->tab = NULL;
3072 static void add_gbr_ineq(struct isl_context_gbr *cgbr, isl_int *ineq)
3074 if (!cgbr->tab)
3075 return;
3077 if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
3078 goto error;
3080 if (isl_tab_add_ineq(cgbr->tab, ineq) < 0)
3081 goto error;
3083 if (cgbr->shifted && !cgbr->shifted->empty && use_shifted(cgbr)) {
3084 int i;
3085 unsigned dim;
3086 dim = isl_basic_map_total_dim(cgbr->tab->bmap);
3088 if (isl_tab_extend_cons(cgbr->shifted, 1) < 0)
3089 goto error;
3091 for (i = 0; i < dim; ++i) {
3092 if (!isl_int_is_neg(ineq[1 + i]))
3093 continue;
3094 isl_int_add(ineq[0], ineq[0], ineq[1 + i]);
3097 if (isl_tab_add_ineq(cgbr->shifted, ineq) < 0)
3098 goto error;
3100 for (i = 0; i < dim; ++i) {
3101 if (!isl_int_is_neg(ineq[1 + i]))
3102 continue;
3103 isl_int_sub(ineq[0], ineq[0], ineq[1 + i]);
3107 if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
3108 if (isl_tab_extend_cons(cgbr->cone, 1) < 0)
3109 goto error;
3110 if (isl_tab_add_ineq(cgbr->cone, ineq) < 0)
3111 goto error;
3114 return;
3115 error:
3116 isl_tab_free(cgbr->tab);
3117 cgbr->tab = NULL;
3120 static void context_gbr_add_ineq(struct isl_context *context, isl_int *ineq,
3121 int check, int update)
3123 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3125 add_gbr_ineq(cgbr, ineq);
3126 if (!cgbr->tab)
3127 return;
3129 if (check) {
3130 int v = tab_has_valid_sample(cgbr->tab, ineq, 0);
3131 if (v < 0)
3132 goto error;
3133 if (!v)
3134 check_gbr_integer_feasible(cgbr);
3136 if (update)
3137 cgbr->tab = check_samples(cgbr->tab, ineq, 0);
3138 return;
3139 error:
3140 isl_tab_free(cgbr->tab);
3141 cgbr->tab = NULL;
3144 static int context_gbr_add_ineq_wrap(void *user, isl_int *ineq)
3146 struct isl_context *context = (struct isl_context *)user;
3147 context_gbr_add_ineq(context, ineq, 0, 0);
3148 return context->op->is_ok(context) ? 0 : -1;
3151 static enum isl_tab_row_sign context_gbr_ineq_sign(struct isl_context *context,
3152 isl_int *ineq, int strict)
3154 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3155 return tab_ineq_sign(cgbr->tab, ineq, strict);
3158 /* Check whether "ineq" can be added to the tableau without rendering
3159 * it infeasible.
3161 static int context_gbr_test_ineq(struct isl_context *context, isl_int *ineq)
3163 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3164 struct isl_tab_undo *snap;
3165 struct isl_tab_undo *shifted_snap = NULL;
3166 struct isl_tab_undo *cone_snap = NULL;
3167 int feasible;
3169 if (!cgbr->tab)
3170 return -1;
3172 if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
3173 return -1;
3175 snap = isl_tab_snap(cgbr->tab);
3176 if (cgbr->shifted)
3177 shifted_snap = isl_tab_snap(cgbr->shifted);
3178 if (cgbr->cone)
3179 cone_snap = isl_tab_snap(cgbr->cone);
3180 add_gbr_ineq(cgbr, ineq);
3181 check_gbr_integer_feasible(cgbr);
3182 if (!cgbr->tab)
3183 return -1;
3184 feasible = !cgbr->tab->empty;
3185 if (isl_tab_rollback(cgbr->tab, snap) < 0)
3186 return -1;
3187 if (shifted_snap) {
3188 if (isl_tab_rollback(cgbr->shifted, shifted_snap))
3189 return -1;
3190 } else if (cgbr->shifted) {
3191 isl_tab_free(cgbr->shifted);
3192 cgbr->shifted = NULL;
3194 if (cone_snap) {
3195 if (isl_tab_rollback(cgbr->cone, cone_snap))
3196 return -1;
3197 } else if (cgbr->cone) {
3198 isl_tab_free(cgbr->cone);
3199 cgbr->cone = NULL;
3202 return feasible;
3205 /* Return the column of the last of the variables associated to
3206 * a column that has a non-zero coefficient.
3207 * This function is called in a context where only coefficients
3208 * of parameters or divs can be non-zero.
3210 static int last_non_zero_var_col(struct isl_tab *tab, isl_int *p)
3212 int i;
3213 int col;
3215 if (tab->n_var == 0)
3216 return -1;
3218 for (i = tab->n_var - 1; i >= 0; --i) {
3219 if (i >= tab->n_param && i < tab->n_var - tab->n_div)
3220 continue;
3221 if (tab->var[i].is_row)
3222 continue;
3223 col = tab->var[i].index;
3224 if (!isl_int_is_zero(p[col]))
3225 return col;
3228 return -1;
3231 /* Look through all the recently added equalities in the context
3232 * to see if we can propagate any of them to the main tableau.
3234 * The newly added equalities in the context are encoded as pairs
3235 * of inequalities starting at inequality "first".
3237 * We tentatively add each of these equalities to the main tableau
3238 * and if this happens to result in a row with a final coefficient
3239 * that is one or negative one, we use it to kill a column
3240 * in the main tableau. Otherwise, we discard the tentatively
3241 * added row.
3242 * This tentative addition of equality constraints turns
3243 * on the undo facility of the tableau. Turn it off again
3244 * at the end, assuming it was turned off to begin with.
3246 * Return 0 on success and -1 on failure.
3248 static int propagate_equalities(struct isl_context_gbr *cgbr,
3249 struct isl_tab *tab, unsigned first)
3251 int i;
3252 struct isl_vec *eq = NULL;
3253 isl_bool needs_undo;
3255 needs_undo = isl_tab_need_undo(tab);
3256 if (needs_undo < 0)
3257 goto error;
3258 eq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
3259 if (!eq)
3260 goto error;
3262 if (isl_tab_extend_cons(tab, (cgbr->tab->bmap->n_ineq - first)/2) < 0)
3263 goto error;
3265 isl_seq_clr(eq->el + 1 + tab->n_param,
3266 tab->n_var - tab->n_param - tab->n_div);
3267 for (i = first; i < cgbr->tab->bmap->n_ineq; i += 2) {
3268 int j;
3269 int r;
3270 struct isl_tab_undo *snap;
3271 snap = isl_tab_snap(tab);
3273 isl_seq_cpy(eq->el, cgbr->tab->bmap->ineq[i], 1 + tab->n_param);
3274 isl_seq_cpy(eq->el + 1 + tab->n_var - tab->n_div,
3275 cgbr->tab->bmap->ineq[i] + 1 + tab->n_param,
3276 tab->n_div);
3278 r = isl_tab_add_row(tab, eq->el);
3279 if (r < 0)
3280 goto error;
3281 r = tab->con[r].index;
3282 j = last_non_zero_var_col(tab, tab->mat->row[r] + 2 + tab->M);
3283 if (j < 0 || j < tab->n_dead ||
3284 !isl_int_is_one(tab->mat->row[r][0]) ||
3285 (!isl_int_is_one(tab->mat->row[r][2 + tab->M + j]) &&
3286 !isl_int_is_negone(tab->mat->row[r][2 + tab->M + j]))) {
3287 if (isl_tab_rollback(tab, snap) < 0)
3288 goto error;
3289 continue;
3291 if (isl_tab_pivot(tab, r, j) < 0)
3292 goto error;
3293 if (isl_tab_kill_col(tab, j) < 0)
3294 goto error;
3296 if (restore_lexmin(tab) < 0)
3297 goto error;
3300 if (!needs_undo)
3301 isl_tab_clear_undo(tab);
3302 isl_vec_free(eq);
3304 return 0;
3305 error:
3306 isl_vec_free(eq);
3307 isl_tab_free(cgbr->tab);
3308 cgbr->tab = NULL;
3309 return -1;
3312 static int context_gbr_detect_equalities(struct isl_context *context,
3313 struct isl_tab *tab)
3315 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3316 unsigned n_ineq;
3318 if (!cgbr->cone) {
3319 struct isl_basic_set *bset = isl_tab_peek_bset(cgbr->tab);
3320 cgbr->cone = isl_tab_from_recession_cone(bset, 0);
3321 if (!cgbr->cone)
3322 goto error;
3323 if (isl_tab_track_bset(cgbr->cone,
3324 isl_basic_set_copy(bset)) < 0)
3325 goto error;
3327 if (isl_tab_detect_implicit_equalities(cgbr->cone) < 0)
3328 goto error;
3330 n_ineq = cgbr->tab->bmap->n_ineq;
3331 cgbr->tab = isl_tab_detect_equalities(cgbr->tab, cgbr->cone);
3332 if (!cgbr->tab)
3333 return -1;
3334 if (cgbr->tab->bmap->n_ineq > n_ineq &&
3335 propagate_equalities(cgbr, tab, n_ineq) < 0)
3336 return -1;
3338 return 0;
3339 error:
3340 isl_tab_free(cgbr->tab);
3341 cgbr->tab = NULL;
3342 return -1;
3345 static int context_gbr_get_div(struct isl_context *context, struct isl_tab *tab,
3346 struct isl_vec *div)
3348 return get_div(tab, context, div);
3351 static isl_bool context_gbr_insert_div(struct isl_context *context, int pos,
3352 __isl_keep isl_vec *div)
3354 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3355 if (cgbr->cone) {
3356 int r, n_div, o_div;
3358 n_div = isl_basic_map_dim(cgbr->cone->bmap, isl_dim_div);
3359 o_div = cgbr->cone->n_var - n_div;
3361 if (isl_tab_extend_cons(cgbr->cone, 3) < 0)
3362 return isl_bool_error;
3363 if (isl_tab_extend_vars(cgbr->cone, 1) < 0)
3364 return isl_bool_error;
3365 if ((r = isl_tab_insert_var(cgbr->cone, pos)) <0)
3366 return isl_bool_error;
3368 cgbr->cone->bmap = isl_basic_map_insert_div(cgbr->cone->bmap,
3369 r - o_div, div);
3370 if (!cgbr->cone->bmap)
3371 return isl_bool_error;
3372 if (isl_tab_push_var(cgbr->cone, isl_tab_undo_bmap_div,
3373 &cgbr->cone->var[r]) < 0)
3374 return isl_bool_error;
3376 return context_tab_insert_div(cgbr->tab, pos, div,
3377 context_gbr_add_ineq_wrap, context);
3380 static int context_gbr_best_split(struct isl_context *context,
3381 struct isl_tab *tab)
3383 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3384 struct isl_tab_undo *snap;
3385 int r;
3387 snap = isl_tab_snap(cgbr->tab);
3388 r = best_split(tab, cgbr->tab);
3390 if (r >= 0 && isl_tab_rollback(cgbr->tab, snap) < 0)
3391 return -1;
3393 return r;
3396 static int context_gbr_is_empty(struct isl_context *context)
3398 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3399 if (!cgbr->tab)
3400 return -1;
3401 return cgbr->tab->empty;
3404 struct isl_gbr_tab_undo {
3405 struct isl_tab_undo *tab_snap;
3406 struct isl_tab_undo *shifted_snap;
3407 struct isl_tab_undo *cone_snap;
3410 static void *context_gbr_save(struct isl_context *context)
3412 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3413 struct isl_gbr_tab_undo *snap;
3415 if (!cgbr->tab)
3416 return NULL;
3418 snap = isl_alloc_type(cgbr->tab->mat->ctx, struct isl_gbr_tab_undo);
3419 if (!snap)
3420 return NULL;
3422 snap->tab_snap = isl_tab_snap(cgbr->tab);
3423 if (isl_tab_save_samples(cgbr->tab) < 0)
3424 goto error;
3426 if (cgbr->shifted)
3427 snap->shifted_snap = isl_tab_snap(cgbr->shifted);
3428 else
3429 snap->shifted_snap = NULL;
3431 if (cgbr->cone)
3432 snap->cone_snap = isl_tab_snap(cgbr->cone);
3433 else
3434 snap->cone_snap = NULL;
3436 return snap;
3437 error:
3438 free(snap);
3439 return NULL;
3442 static void context_gbr_restore(struct isl_context *context, void *save)
3444 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3445 struct isl_gbr_tab_undo *snap = (struct isl_gbr_tab_undo *)save;
3446 if (!snap)
3447 goto error;
3448 if (isl_tab_rollback(cgbr->tab, snap->tab_snap) < 0)
3449 goto error;
3451 if (snap->shifted_snap) {
3452 if (isl_tab_rollback(cgbr->shifted, snap->shifted_snap) < 0)
3453 goto error;
3454 } else if (cgbr->shifted) {
3455 isl_tab_free(cgbr->shifted);
3456 cgbr->shifted = NULL;
3459 if (snap->cone_snap) {
3460 if (isl_tab_rollback(cgbr->cone, snap->cone_snap) < 0)
3461 goto error;
3462 } else if (cgbr->cone) {
3463 isl_tab_free(cgbr->cone);
3464 cgbr->cone = NULL;
3467 free(snap);
3469 return;
3470 error:
3471 free(snap);
3472 isl_tab_free(cgbr->tab);
3473 cgbr->tab = NULL;
3476 static void context_gbr_discard(void *save)
3478 struct isl_gbr_tab_undo *snap = (struct isl_gbr_tab_undo *)save;
3479 free(snap);
3482 static int context_gbr_is_ok(struct isl_context *context)
3484 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3485 return !!cgbr->tab;
3488 static void context_gbr_invalidate(struct isl_context *context)
3490 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3491 isl_tab_free(cgbr->tab);
3492 cgbr->tab = NULL;
3495 static __isl_null struct isl_context *context_gbr_free(
3496 struct isl_context *context)
3498 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3499 isl_tab_free(cgbr->tab);
3500 isl_tab_free(cgbr->shifted);
3501 isl_tab_free(cgbr->cone);
3502 free(cgbr);
3504 return NULL;
3507 struct isl_context_op isl_context_gbr_op = {
3508 context_gbr_detect_nonnegative_parameters,
3509 context_gbr_peek_basic_set,
3510 context_gbr_peek_tab,
3511 context_gbr_add_eq,
3512 context_gbr_add_ineq,
3513 context_gbr_ineq_sign,
3514 context_gbr_test_ineq,
3515 context_gbr_get_div,
3516 context_gbr_insert_div,
3517 context_gbr_detect_equalities,
3518 context_gbr_best_split,
3519 context_gbr_is_empty,
3520 context_gbr_is_ok,
3521 context_gbr_save,
3522 context_gbr_restore,
3523 context_gbr_discard,
3524 context_gbr_invalidate,
3525 context_gbr_free,
3528 static struct isl_context *isl_context_gbr_alloc(__isl_keep isl_basic_set *dom)
3530 struct isl_context_gbr *cgbr;
3532 if (!dom)
3533 return NULL;
3535 cgbr = isl_calloc_type(dom->ctx, struct isl_context_gbr);
3536 if (!cgbr)
3537 return NULL;
3539 cgbr->context.op = &isl_context_gbr_op;
3541 cgbr->shifted = NULL;
3542 cgbr->cone = NULL;
3543 cgbr->tab = isl_tab_from_basic_set(dom, 1);
3544 cgbr->tab = isl_tab_init_samples(cgbr->tab);
3545 if (!cgbr->tab)
3546 goto error;
3547 check_gbr_integer_feasible(cgbr);
3549 return &cgbr->context;
3550 error:
3551 cgbr->context.op->free(&cgbr->context);
3552 return NULL;
3555 /* Allocate a context corresponding to "dom".
3556 * The representation specific fields are initialized by
3557 * isl_context_lex_alloc or isl_context_gbr_alloc.
3558 * The shared "n_unknown" field is initialized to the number
3559 * of final unknown integer divisions in "dom".
3561 static struct isl_context *isl_context_alloc(__isl_keep isl_basic_set *dom)
3563 struct isl_context *context;
3564 int first;
3566 if (!dom)
3567 return NULL;
3569 if (dom->ctx->opt->context == ISL_CONTEXT_LEXMIN)
3570 context = isl_context_lex_alloc(dom);
3571 else
3572 context = isl_context_gbr_alloc(dom);
3574 if (!context)
3575 return NULL;
3577 first = isl_basic_set_first_unknown_div(dom);
3578 if (first < 0)
3579 return context->op->free(context);
3580 context->n_unknown = isl_basic_set_dim(dom, isl_dim_div) - first;
3582 return context;
3585 /* Initialize some common fields of "sol", which keeps track
3586 * of the solution of an optimization problem on "bmap" over
3587 * the domain "dom".
3588 * If "max" is set, then a maximization problem is being solved, rather than
3589 * a minimization problem, which means that the variables in the
3590 * tableau have value "M - x" rather than "M + x".
3592 static isl_stat sol_init(struct isl_sol *sol, __isl_keep isl_basic_map *bmap,
3593 __isl_keep isl_basic_set *dom, int max)
3595 sol->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
3596 sol->dec_level.callback.run = &sol_dec_level_wrap;
3597 sol->dec_level.sol = sol;
3598 sol->max = max;
3599 sol->n_out = isl_basic_map_dim(bmap, isl_dim_out);
3600 sol->space = isl_basic_map_get_space(bmap);
3602 sol->context = isl_context_alloc(dom);
3603 if (!sol->space || !sol->context)
3604 return isl_stat_error;
3606 return isl_stat_ok;
3609 /* Construct an isl_sol_map structure for accumulating the solution.
3610 * If track_empty is set, then we also keep track of the parts
3611 * of the context where there is no solution.
3612 * If max is set, then we are solving a maximization, rather than
3613 * a minimization problem, which means that the variables in the
3614 * tableau have value "M - x" rather than "M + x".
3616 static struct isl_sol *sol_map_init(__isl_keep isl_basic_map *bmap,
3617 __isl_take isl_basic_set *dom, int track_empty, int max)
3619 struct isl_sol_map *sol_map = NULL;
3620 isl_space *space;
3622 if (!bmap)
3623 goto error;
3625 sol_map = isl_calloc_type(bmap->ctx, struct isl_sol_map);
3626 if (!sol_map)
3627 goto error;
3629 sol_map->sol.free = &sol_map_free;
3630 if (sol_init(&sol_map->sol, bmap, dom, max) < 0)
3631 goto error;
3632 sol_map->sol.add = &sol_map_add_wrap;
3633 sol_map->sol.add_empty = track_empty ? &sol_map_add_empty_wrap : NULL;
3634 space = isl_space_copy(sol_map->sol.space);
3635 sol_map->map = isl_map_alloc_space(space, 1, ISL_MAP_DISJOINT);
3636 if (!sol_map->map)
3637 goto error;
3639 if (track_empty) {
3640 sol_map->empty = isl_set_alloc_space(isl_basic_set_get_space(dom),
3641 1, ISL_SET_DISJOINT);
3642 if (!sol_map->empty)
3643 goto error;
3646 isl_basic_set_free(dom);
3647 return &sol_map->sol;
3648 error:
3649 isl_basic_set_free(dom);
3650 sol_free(&sol_map->sol);
3651 return NULL;
3654 /* Check whether all coefficients of (non-parameter) variables
3655 * are non-positive, meaning that no pivots can be performed on the row.
3657 static int is_critical(struct isl_tab *tab, int row)
3659 int j;
3660 unsigned off = 2 + tab->M;
3662 for (j = tab->n_dead; j < tab->n_col; ++j) {
3663 if (tab->col_var[j] >= 0 &&
3664 (tab->col_var[j] < tab->n_param ||
3665 tab->col_var[j] >= tab->n_var - tab->n_div))
3666 continue;
3668 if (isl_int_is_pos(tab->mat->row[row][off + j]))
3669 return 0;
3672 return 1;
3675 /* Check whether the inequality represented by vec is strict over the integers,
3676 * i.e., there are no integer values satisfying the constraint with
3677 * equality. This happens if the gcd of the coefficients is not a divisor
3678 * of the constant term. If so, scale the constraint down by the gcd
3679 * of the coefficients.
3681 static int is_strict(struct isl_vec *vec)
3683 isl_int gcd;
3684 int strict = 0;
3686 isl_int_init(gcd);
3687 isl_seq_gcd(vec->el + 1, vec->size - 1, &gcd);
3688 if (!isl_int_is_one(gcd)) {
3689 strict = !isl_int_is_divisible_by(vec->el[0], gcd);
3690 isl_int_fdiv_q(vec->el[0], vec->el[0], gcd);
3691 isl_seq_scale_down(vec->el + 1, vec->el + 1, gcd, vec->size-1);
3693 isl_int_clear(gcd);
3695 return strict;
3698 /* Determine the sign of the given row of the main tableau.
3699 * The result is one of
3700 * isl_tab_row_pos: always non-negative; no pivot needed
3701 * isl_tab_row_neg: always non-positive; pivot
3702 * isl_tab_row_any: can be both positive and negative; split
3704 * We first handle some simple cases
3705 * - the row sign may be known already
3706 * - the row may be obviously non-negative
3707 * - the parametric constant may be equal to that of another row
3708 * for which we know the sign. This sign will be either "pos" or
3709 * "any". If it had been "neg" then we would have pivoted before.
3711 * If none of these cases hold, we check the value of the row for each
3712 * of the currently active samples. Based on the signs of these values
3713 * we make an initial determination of the sign of the row.
3715 * all zero -> unk(nown)
3716 * all non-negative -> pos
3717 * all non-positive -> neg
3718 * both negative and positive -> all
3720 * If we end up with "all", we are done.
3721 * Otherwise, we perform a check for positive and/or negative
3722 * values as follows.
3724 * samples neg unk pos
3725 * <0 ? Y N Y N
3726 * pos any pos
3727 * >0 ? Y N Y N
3728 * any neg any neg
3730 * There is no special sign for "zero", because we can usually treat zero
3731 * as either non-negative or non-positive, whatever works out best.
3732 * However, if the row is "critical", meaning that pivoting is impossible
3733 * then we don't want to limp zero with the non-positive case, because
3734 * then we we would lose the solution for those values of the parameters
3735 * where the value of the row is zero. Instead, we treat 0 as non-negative
3736 * ensuring a split if the row can attain both zero and negative values.
3737 * The same happens when the original constraint was one that could not
3738 * be satisfied with equality by any integer values of the parameters.
3739 * In this case, we normalize the constraint, but then a value of zero
3740 * for the normalized constraint is actually a positive value for the
3741 * original constraint, so again we need to treat zero as non-negative.
3742 * In both these cases, we have the following decision tree instead:
3744 * all non-negative -> pos
3745 * all negative -> neg
3746 * both negative and non-negative -> all
3748 * samples neg pos
3749 * <0 ? Y N
3750 * any pos
3751 * >=0 ? Y N
3752 * any neg
3754 static enum isl_tab_row_sign row_sign(struct isl_tab *tab,
3755 struct isl_sol *sol, int row)
3757 struct isl_vec *ineq = NULL;
3758 enum isl_tab_row_sign res = isl_tab_row_unknown;
3759 int critical;
3760 int strict;
3761 int row2;
3763 if (tab->row_sign[row] != isl_tab_row_unknown)
3764 return tab->row_sign[row];
3765 if (is_obviously_nonneg(tab, row))
3766 return isl_tab_row_pos;
3767 for (row2 = tab->n_redundant; row2 < tab->n_row; ++row2) {
3768 if (tab->row_sign[row2] == isl_tab_row_unknown)
3769 continue;
3770 if (identical_parameter_line(tab, row, row2))
3771 return tab->row_sign[row2];
3774 critical = is_critical(tab, row);
3776 ineq = get_row_parameter_ineq(tab, row);
3777 if (!ineq)
3778 goto error;
3780 strict = is_strict(ineq);
3782 res = sol->context->op->ineq_sign(sol->context, ineq->el,
3783 critical || strict);
3785 if (res == isl_tab_row_unknown || res == isl_tab_row_pos) {
3786 /* test for negative values */
3787 int feasible;
3788 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3789 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3791 feasible = sol->context->op->test_ineq(sol->context, ineq->el);
3792 if (feasible < 0)
3793 goto error;
3794 if (!feasible)
3795 res = isl_tab_row_pos;
3796 else
3797 res = (res == isl_tab_row_unknown) ? isl_tab_row_neg
3798 : isl_tab_row_any;
3799 if (res == isl_tab_row_neg) {
3800 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3801 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3805 if (res == isl_tab_row_neg) {
3806 /* test for positive values */
3807 int feasible;
3808 if (!critical && !strict)
3809 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3811 feasible = sol->context->op->test_ineq(sol->context, ineq->el);
3812 if (feasible < 0)
3813 goto error;
3814 if (feasible)
3815 res = isl_tab_row_any;
3818 isl_vec_free(ineq);
3819 return res;
3820 error:
3821 isl_vec_free(ineq);
3822 return isl_tab_row_unknown;
3825 static void find_solutions(struct isl_sol *sol, struct isl_tab *tab);
3827 /* Find solutions for values of the parameters that satisfy the given
3828 * inequality.
3830 * We currently take a snapshot of the context tableau that is reset
3831 * when we return from this function, while we make a copy of the main
3832 * tableau, leaving the original main tableau untouched.
3833 * These are fairly arbitrary choices. Making a copy also of the context
3834 * tableau would obviate the need to undo any changes made to it later,
3835 * while taking a snapshot of the main tableau could reduce memory usage.
3836 * If we were to switch to taking a snapshot of the main tableau,
3837 * we would have to keep in mind that we need to save the row signs
3838 * and that we need to do this before saving the current basis
3839 * such that the basis has been restore before we restore the row signs.
3841 static void find_in_pos(struct isl_sol *sol, struct isl_tab *tab, isl_int *ineq)
3843 void *saved;
3845 if (!sol->context)
3846 goto error;
3847 saved = sol->context->op->save(sol->context);
3849 tab = isl_tab_dup(tab);
3850 if (!tab)
3851 goto error;
3853 sol->context->op->add_ineq(sol->context, ineq, 0, 1);
3855 find_solutions(sol, tab);
3857 if (!sol->error)
3858 sol->context->op->restore(sol->context, saved);
3859 else
3860 sol->context->op->discard(saved);
3861 return;
3862 error:
3863 sol->error = 1;
3866 /* Record the absence of solutions for those values of the parameters
3867 * that do not satisfy the given inequality with equality.
3869 static void no_sol_in_strict(struct isl_sol *sol,
3870 struct isl_tab *tab, struct isl_vec *ineq)
3872 int empty;
3873 void *saved;
3875 if (!sol->context || sol->error)
3876 goto error;
3877 saved = sol->context->op->save(sol->context);
3879 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3881 sol->context->op->add_ineq(sol->context, ineq->el, 1, 0);
3882 if (!sol->context)
3883 goto error;
3885 empty = tab->empty;
3886 tab->empty = 1;
3887 sol_add(sol, tab);
3888 tab->empty = empty;
3890 isl_int_add_ui(ineq->el[0], ineq->el[0], 1);
3892 sol->context->op->restore(sol->context, saved);
3893 return;
3894 error:
3895 sol->error = 1;
3898 /* Reset all row variables that are marked to have a sign that may
3899 * be both positive and negative to have an unknown sign.
3901 static void reset_any_to_unknown(struct isl_tab *tab)
3903 int row;
3905 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3906 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
3907 continue;
3908 if (tab->row_sign[row] == isl_tab_row_any)
3909 tab->row_sign[row] = isl_tab_row_unknown;
3913 /* Compute the lexicographic minimum of the set represented by the main
3914 * tableau "tab" within the context "sol->context_tab".
3915 * On entry the sample value of the main tableau is lexicographically
3916 * less than or equal to this lexicographic minimum.
3917 * Pivots are performed until a feasible point is found, which is then
3918 * necessarily equal to the minimum, or until the tableau is found to
3919 * be infeasible. Some pivots may need to be performed for only some
3920 * feasible values of the context tableau. If so, the context tableau
3921 * is split into a part where the pivot is needed and a part where it is not.
3923 * Whenever we enter the main loop, the main tableau is such that no
3924 * "obvious" pivots need to be performed on it, where "obvious" means
3925 * that the given row can be seen to be negative without looking at
3926 * the context tableau. In particular, for non-parametric problems,
3927 * no pivots need to be performed on the main tableau.
3928 * The caller of find_solutions is responsible for making this property
3929 * hold prior to the first iteration of the loop, while restore_lexmin
3930 * is called before every other iteration.
3932 * Inside the main loop, we first examine the signs of the rows of
3933 * the main tableau within the context of the context tableau.
3934 * If we find a row that is always non-positive for all values of
3935 * the parameters satisfying the context tableau and negative for at
3936 * least one value of the parameters, we perform the appropriate pivot
3937 * and start over. An exception is the case where no pivot can be
3938 * performed on the row. In this case, we require that the sign of
3939 * the row is negative for all values of the parameters (rather than just
3940 * non-positive). This special case is handled inside row_sign, which
3941 * will say that the row can have any sign if it determines that it can
3942 * attain both negative and zero values.
3944 * If we can't find a row that always requires a pivot, but we can find
3945 * one or more rows that require a pivot for some values of the parameters
3946 * (i.e., the row can attain both positive and negative signs), then we split
3947 * the context tableau into two parts, one where we force the sign to be
3948 * non-negative and one where we force is to be negative.
3949 * The non-negative part is handled by a recursive call (through find_in_pos).
3950 * Upon returning from this call, we continue with the negative part and
3951 * perform the required pivot.
3953 * If no such rows can be found, all rows are non-negative and we have
3954 * found a (rational) feasible point. If we only wanted a rational point
3955 * then we are done.
3956 * Otherwise, we check if all values of the sample point of the tableau
3957 * are integral for the variables. If so, we have found the minimal
3958 * integral point and we are done.
3959 * If the sample point is not integral, then we need to make a distinction
3960 * based on whether the constant term is non-integral or the coefficients
3961 * of the parameters. Furthermore, in order to decide how to handle
3962 * the non-integrality, we also need to know whether the coefficients
3963 * of the other columns in the tableau are integral. This leads
3964 * to the following table. The first two rows do not correspond
3965 * to a non-integral sample point and are only mentioned for completeness.
3967 * constant parameters other
3969 * int int int |
3970 * int int rat | -> no problem
3972 * rat int int -> fail
3974 * rat int rat -> cut
3976 * int rat rat |
3977 * rat rat rat | -> parametric cut
3979 * int rat int |
3980 * rat rat int | -> split context
3982 * If the parametric constant is completely integral, then there is nothing
3983 * to be done. If the constant term is non-integral, but all the other
3984 * coefficient are integral, then there is nothing that can be done
3985 * and the tableau has no integral solution.
3986 * If, on the other hand, one or more of the other columns have rational
3987 * coefficients, but the parameter coefficients are all integral, then
3988 * we can perform a regular (non-parametric) cut.
3989 * Finally, if there is any parameter coefficient that is non-integral,
3990 * then we need to involve the context tableau. There are two cases here.
3991 * If at least one other column has a rational coefficient, then we
3992 * can perform a parametric cut in the main tableau by adding a new
3993 * integer division in the context tableau.
3994 * If all other columns have integral coefficients, then we need to
3995 * enforce that the rational combination of parameters (c + \sum a_i y_i)/m
3996 * is always integral. We do this by introducing an integer division
3997 * q = floor((c + \sum a_i y_i)/m) and stipulating that its argument should
3998 * always be integral in the context tableau, i.e., m q = c + \sum a_i y_i.
3999 * Since q is expressed in the tableau as
4000 * c + \sum a_i y_i - m q >= 0
4001 * -c - \sum a_i y_i + m q + m - 1 >= 0
4002 * it is sufficient to add the inequality
4003 * -c - \sum a_i y_i + m q >= 0
4004 * In the part of the context where this inequality does not hold, the
4005 * main tableau is marked as being empty.
4007 static void find_solutions(struct isl_sol *sol, struct isl_tab *tab)
4009 struct isl_context *context;
4010 int r;
4012 if (!tab || sol->error)
4013 goto error;
4015 context = sol->context;
4017 if (tab->empty)
4018 goto done;
4019 if (context->op->is_empty(context))
4020 goto done;
4022 for (r = 0; r >= 0 && tab && !tab->empty; r = restore_lexmin(tab)) {
4023 int flags;
4024 int row;
4025 enum isl_tab_row_sign sgn;
4026 int split = -1;
4027 int n_split = 0;
4029 for (row = tab->n_redundant; row < tab->n_row; ++row) {
4030 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
4031 continue;
4032 sgn = row_sign(tab, sol, row);
4033 if (!sgn)
4034 goto error;
4035 tab->row_sign[row] = sgn;
4036 if (sgn == isl_tab_row_any)
4037 n_split++;
4038 if (sgn == isl_tab_row_any && split == -1)
4039 split = row;
4040 if (sgn == isl_tab_row_neg)
4041 break;
4043 if (row < tab->n_row)
4044 continue;
4045 if (split != -1) {
4046 struct isl_vec *ineq;
4047 if (n_split != 1)
4048 split = context->op->best_split(context, tab);
4049 if (split < 0)
4050 goto error;
4051 ineq = get_row_parameter_ineq(tab, split);
4052 if (!ineq)
4053 goto error;
4054 is_strict(ineq);
4055 reset_any_to_unknown(tab);
4056 tab->row_sign[split] = isl_tab_row_pos;
4057 sol_inc_level(sol);
4058 find_in_pos(sol, tab, ineq->el);
4059 tab->row_sign[split] = isl_tab_row_neg;
4060 isl_seq_neg(ineq->el, ineq->el, ineq->size);
4061 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
4062 if (!sol->error)
4063 context->op->add_ineq(context, ineq->el, 0, 1);
4064 isl_vec_free(ineq);
4065 if (sol->error)
4066 goto error;
4067 continue;
4069 if (tab->rational)
4070 break;
4071 row = first_non_integer_row(tab, &flags);
4072 if (row < 0)
4073 break;
4074 if (ISL_FL_ISSET(flags, I_PAR)) {
4075 if (ISL_FL_ISSET(flags, I_VAR)) {
4076 if (isl_tab_mark_empty(tab) < 0)
4077 goto error;
4078 break;
4080 row = add_cut(tab, row);
4081 } else if (ISL_FL_ISSET(flags, I_VAR)) {
4082 struct isl_vec *div;
4083 struct isl_vec *ineq;
4084 int d;
4085 div = get_row_split_div(tab, row);
4086 if (!div)
4087 goto error;
4088 d = context->op->get_div(context, tab, div);
4089 isl_vec_free(div);
4090 if (d < 0)
4091 goto error;
4092 ineq = ineq_for_div(context->op->peek_basic_set(context), d);
4093 if (!ineq)
4094 goto error;
4095 sol_inc_level(sol);
4096 no_sol_in_strict(sol, tab, ineq);
4097 isl_seq_neg(ineq->el, ineq->el, ineq->size);
4098 context->op->add_ineq(context, ineq->el, 1, 1);
4099 isl_vec_free(ineq);
4100 if (sol->error || !context->op->is_ok(context))
4101 goto error;
4102 tab = set_row_cst_to_div(tab, row, d);
4103 if (context->op->is_empty(context))
4104 break;
4105 } else
4106 row = add_parametric_cut(tab, row, context);
4107 if (row < 0)
4108 goto error;
4110 if (r < 0)
4111 goto error;
4112 done:
4113 sol_add(sol, tab);
4114 isl_tab_free(tab);
4115 return;
4116 error:
4117 isl_tab_free(tab);
4118 sol->error = 1;
4121 /* Does "sol" contain a pair of partial solutions that could potentially
4122 * be merged?
4124 * We currently only check that "sol" is not in an error state
4125 * and that there are at least two partial solutions of which the final two
4126 * are defined at the same level.
4128 static int sol_has_mergeable_solutions(struct isl_sol *sol)
4130 if (sol->error)
4131 return 0;
4132 if (!sol->partial)
4133 return 0;
4134 if (!sol->partial->next)
4135 return 0;
4136 return sol->partial->level == sol->partial->next->level;
4139 /* Compute the lexicographic minimum of the set represented by the main
4140 * tableau "tab" within the context "sol->context_tab".
4142 * As a preprocessing step, we first transfer all the purely parametric
4143 * equalities from the main tableau to the context tableau, i.e.,
4144 * parameters that have been pivoted to a row.
4145 * These equalities are ignored by the main algorithm, because the
4146 * corresponding rows may not be marked as being non-negative.
4147 * In parts of the context where the added equality does not hold,
4148 * the main tableau is marked as being empty.
4150 * Before we embark on the actual computation, we save a copy
4151 * of the context. When we return, we check if there are any
4152 * partial solutions that can potentially be merged. If so,
4153 * we perform a rollback to the initial state of the context.
4154 * The merging of partial solutions happens inside calls to
4155 * sol_dec_level that are pushed onto the undo stack of the context.
4156 * If there are no partial solutions that can potentially be merged
4157 * then the rollback is skipped as it would just be wasted effort.
4159 static void find_solutions_main(struct isl_sol *sol, struct isl_tab *tab)
4161 int row;
4162 void *saved;
4164 if (!tab)
4165 goto error;
4167 sol->level = 0;
4169 for (row = tab->n_redundant; row < tab->n_row; ++row) {
4170 int p;
4171 struct isl_vec *eq;
4173 if (tab->row_var[row] < 0)
4174 continue;
4175 if (tab->row_var[row] >= tab->n_param &&
4176 tab->row_var[row] < tab->n_var - tab->n_div)
4177 continue;
4178 if (tab->row_var[row] < tab->n_param)
4179 p = tab->row_var[row];
4180 else
4181 p = tab->row_var[row]
4182 + tab->n_param - (tab->n_var - tab->n_div);
4184 eq = isl_vec_alloc(tab->mat->ctx, 1+tab->n_param+tab->n_div);
4185 if (!eq)
4186 goto error;
4187 get_row_parameter_line(tab, row, eq->el);
4188 isl_int_neg(eq->el[1 + p], tab->mat->row[row][0]);
4189 eq = isl_vec_normalize(eq);
4191 sol_inc_level(sol);
4192 no_sol_in_strict(sol, tab, eq);
4194 isl_seq_neg(eq->el, eq->el, eq->size);
4195 sol_inc_level(sol);
4196 no_sol_in_strict(sol, tab, eq);
4197 isl_seq_neg(eq->el, eq->el, eq->size);
4199 sol->context->op->add_eq(sol->context, eq->el, 1, 1);
4201 isl_vec_free(eq);
4203 if (isl_tab_mark_redundant(tab, row) < 0)
4204 goto error;
4206 if (sol->context->op->is_empty(sol->context))
4207 break;
4209 row = tab->n_redundant - 1;
4212 saved = sol->context->op->save(sol->context);
4214 find_solutions(sol, tab);
4216 if (sol_has_mergeable_solutions(sol))
4217 sol->context->op->restore(sol->context, saved);
4218 else
4219 sol->context->op->discard(saved);
4221 sol->level = 0;
4222 sol_pop(sol);
4224 return;
4225 error:
4226 isl_tab_free(tab);
4227 sol->error = 1;
4230 /* Check if integer division "div" of "dom" also occurs in "bmap".
4231 * If so, return its position within the divs.
4232 * If not, return -1.
4234 static int find_context_div(struct isl_basic_map *bmap,
4235 struct isl_basic_set *dom, unsigned div)
4237 int i;
4238 unsigned b_dim = isl_space_dim(bmap->dim, isl_dim_all);
4239 unsigned d_dim = isl_space_dim(dom->dim, isl_dim_all);
4241 if (isl_int_is_zero(dom->div[div][0]))
4242 return -1;
4243 if (isl_seq_first_non_zero(dom->div[div] + 2 + d_dim, dom->n_div) != -1)
4244 return -1;
4246 for (i = 0; i < bmap->n_div; ++i) {
4247 if (isl_int_is_zero(bmap->div[i][0]))
4248 continue;
4249 if (isl_seq_first_non_zero(bmap->div[i] + 2 + d_dim,
4250 (b_dim - d_dim) + bmap->n_div) != -1)
4251 continue;
4252 if (isl_seq_eq(bmap->div[i], dom->div[div], 2 + d_dim))
4253 return i;
4255 return -1;
4258 /* The correspondence between the variables in the main tableau,
4259 * the context tableau, and the input map and domain is as follows.
4260 * The first n_param and the last n_div variables of the main tableau
4261 * form the variables of the context tableau.
4262 * In the basic map, these n_param variables correspond to the
4263 * parameters and the input dimensions. In the domain, they correspond
4264 * to the parameters and the set dimensions.
4265 * The n_div variables correspond to the integer divisions in the domain.
4266 * To ensure that everything lines up, we may need to copy some of the
4267 * integer divisions of the domain to the map. These have to be placed
4268 * in the same order as those in the context and they have to be placed
4269 * after any other integer divisions that the map may have.
4270 * This function performs the required reordering.
4272 static struct isl_basic_map *align_context_divs(struct isl_basic_map *bmap,
4273 struct isl_basic_set *dom)
4275 int i;
4276 int common = 0;
4277 int other;
4279 for (i = 0; i < dom->n_div; ++i)
4280 if (find_context_div(bmap, dom, i) != -1)
4281 common++;
4282 other = bmap->n_div - common;
4283 if (dom->n_div - common > 0) {
4284 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
4285 dom->n_div - common, 0, 0);
4286 if (!bmap)
4287 return NULL;
4289 for (i = 0; i < dom->n_div; ++i) {
4290 int pos = find_context_div(bmap, dom, i);
4291 if (pos < 0) {
4292 pos = isl_basic_map_alloc_div(bmap);
4293 if (pos < 0)
4294 goto error;
4295 isl_int_set_si(bmap->div[pos][0], 0);
4297 if (pos != other + i)
4298 isl_basic_map_swap_div(bmap, pos, other + i);
4300 return bmap;
4301 error:
4302 isl_basic_map_free(bmap);
4303 return NULL;
4306 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4307 * some obvious symmetries.
4309 * We make sure the divs in the domain are properly ordered,
4310 * because they will be added one by one in the given order
4311 * during the construction of the solution map.
4312 * Furthermore, make sure that the known integer divisions
4313 * appear before any unknown integer division because the solution
4314 * may depend on the known integer divisions, while anything that
4315 * depends on any variable starting from the first unknown integer
4316 * division is ignored in sol_pma_add.
4318 static struct isl_sol *basic_map_partial_lexopt_base_sol(
4319 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4320 __isl_give isl_set **empty, int max,
4321 struct isl_sol *(*init)(__isl_keep isl_basic_map *bmap,
4322 __isl_take isl_basic_set *dom, int track_empty, int max))
4324 struct isl_tab *tab;
4325 struct isl_sol *sol = NULL;
4326 struct isl_context *context;
4328 if (dom->n_div) {
4329 dom = isl_basic_set_sort_divs(dom);
4330 bmap = align_context_divs(bmap, dom);
4332 sol = init(bmap, dom, !!empty, max);
4333 if (!sol)
4334 goto error;
4336 context = sol->context;
4337 if (isl_basic_set_plain_is_empty(context->op->peek_basic_set(context)))
4338 /* nothing */;
4339 else if (isl_basic_map_plain_is_empty(bmap)) {
4340 if (sol->add_empty)
4341 sol->add_empty(sol,
4342 isl_basic_set_copy(context->op->peek_basic_set(context)));
4343 } else {
4344 tab = tab_for_lexmin(bmap,
4345 context->op->peek_basic_set(context), 1, max);
4346 tab = context->op->detect_nonnegative_parameters(context, tab);
4347 find_solutions_main(sol, tab);
4349 if (sol->error)
4350 goto error;
4352 isl_basic_map_free(bmap);
4353 return sol;
4354 error:
4355 sol_free(sol);
4356 isl_basic_map_free(bmap);
4357 return NULL;
4360 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4361 * some obvious symmetries.
4363 * We call basic_map_partial_lexopt_base_sol and extract the results.
4365 static __isl_give isl_map *basic_map_partial_lexopt_base(
4366 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4367 __isl_give isl_set **empty, int max)
4369 isl_map *result = NULL;
4370 struct isl_sol *sol;
4371 struct isl_sol_map *sol_map;
4373 sol = basic_map_partial_lexopt_base_sol(bmap, dom, empty, max,
4374 &sol_map_init);
4375 if (!sol)
4376 return NULL;
4377 sol_map = (struct isl_sol_map *) sol;
4379 result = isl_map_copy(sol_map->map);
4380 if (empty)
4381 *empty = isl_set_copy(sol_map->empty);
4382 sol_free(&sol_map->sol);
4383 return result;
4386 /* Return a count of the number of occurrences of the "n" first
4387 * variables in the inequality constraints of "bmap".
4389 static __isl_give int *count_occurrences(__isl_keep isl_basic_map *bmap,
4390 int n)
4392 int i, j;
4393 isl_ctx *ctx;
4394 int *occurrences;
4396 if (!bmap)
4397 return NULL;
4398 ctx = isl_basic_map_get_ctx(bmap);
4399 occurrences = isl_calloc_array(ctx, int, n);
4400 if (!occurrences)
4401 return NULL;
4403 for (i = 0; i < bmap->n_ineq; ++i) {
4404 for (j = 0; j < n; ++j) {
4405 if (!isl_int_is_zero(bmap->ineq[i][1 + j]))
4406 occurrences[j]++;
4410 return occurrences;
4413 /* Do all of the "n" variables with non-zero coefficients in "c"
4414 * occur in exactly a single constraint.
4415 * "occurrences" is an array of length "n" containing the number
4416 * of occurrences of each of the variables in the inequality constraints.
4418 static int single_occurrence(int n, isl_int *c, int *occurrences)
4420 int i;
4422 for (i = 0; i < n; ++i) {
4423 if (isl_int_is_zero(c[i]))
4424 continue;
4425 if (occurrences[i] != 1)
4426 return 0;
4429 return 1;
4432 /* Do all of the "n" initial variables that occur in inequality constraint
4433 * "ineq" of "bmap" only occur in that constraint?
4435 static int all_single_occurrence(__isl_keep isl_basic_map *bmap, int ineq,
4436 int n)
4438 int i, j;
4440 for (i = 0; i < n; ++i) {
4441 if (isl_int_is_zero(bmap->ineq[ineq][1 + i]))
4442 continue;
4443 for (j = 0; j < bmap->n_ineq; ++j) {
4444 if (j == ineq)
4445 continue;
4446 if (!isl_int_is_zero(bmap->ineq[j][1 + i]))
4447 return 0;
4451 return 1;
4454 /* Structure used during detection of parallel constraints.
4455 * n_in: number of "input" variables: isl_dim_param + isl_dim_in
4456 * n_out: number of "output" variables: isl_dim_out + isl_dim_div
4457 * val: the coefficients of the output variables
4459 struct isl_constraint_equal_info {
4460 isl_basic_map *bmap;
4461 unsigned n_in;
4462 unsigned n_out;
4463 isl_int *val;
4466 /* Check whether the coefficients of the output variables
4467 * of the constraint in "entry" are equal to info->val.
4469 static int constraint_equal(const void *entry, const void *val)
4471 isl_int **row = (isl_int **)entry;
4472 const struct isl_constraint_equal_info *info = val;
4474 return isl_seq_eq((*row) + 1 + info->n_in, info->val, info->n_out);
4477 /* Check whether "bmap" has a pair of constraints that have
4478 * the same coefficients for the output variables.
4479 * Note that the coefficients of the existentially quantified
4480 * variables need to be zero since the existentially quantified
4481 * of the result are usually not the same as those of the input.
4482 * Furthermore, check that each of the input variables that occur
4483 * in those constraints does not occur in any other constraint.
4484 * If so, return 1 and return the row indices of the two constraints
4485 * in *first and *second.
4487 static int parallel_constraints(__isl_keep isl_basic_map *bmap,
4488 int *first, int *second)
4490 int i;
4491 isl_ctx *ctx;
4492 int *occurrences = NULL;
4493 struct isl_hash_table *table = NULL;
4494 struct isl_hash_table_entry *entry;
4495 struct isl_constraint_equal_info info;
4496 unsigned n_out;
4497 unsigned n_div;
4499 ctx = isl_basic_map_get_ctx(bmap);
4500 table = isl_hash_table_alloc(ctx, bmap->n_ineq);
4501 if (!table)
4502 goto error;
4504 info.n_in = isl_basic_map_dim(bmap, isl_dim_param) +
4505 isl_basic_map_dim(bmap, isl_dim_in);
4506 occurrences = count_occurrences(bmap, info.n_in);
4507 if (info.n_in && !occurrences)
4508 goto error;
4509 info.bmap = bmap;
4510 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4511 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4512 info.n_out = n_out + n_div;
4513 for (i = 0; i < bmap->n_ineq; ++i) {
4514 uint32_t hash;
4516 info.val = bmap->ineq[i] + 1 + info.n_in;
4517 if (isl_seq_first_non_zero(info.val, n_out) < 0)
4518 continue;
4519 if (isl_seq_first_non_zero(info.val + n_out, n_div) >= 0)
4520 continue;
4521 if (!single_occurrence(info.n_in, bmap->ineq[i] + 1,
4522 occurrences))
4523 continue;
4524 hash = isl_seq_get_hash(info.val, info.n_out);
4525 entry = isl_hash_table_find(ctx, table, hash,
4526 constraint_equal, &info, 1);
4527 if (!entry)
4528 goto error;
4529 if (entry->data)
4530 break;
4531 entry->data = &bmap->ineq[i];
4534 if (i < bmap->n_ineq) {
4535 *first = ((isl_int **)entry->data) - bmap->ineq;
4536 *second = i;
4539 isl_hash_table_free(ctx, table);
4540 free(occurrences);
4542 return i < bmap->n_ineq;
4543 error:
4544 isl_hash_table_free(ctx, table);
4545 free(occurrences);
4546 return -1;
4549 /* Given a set of upper bounds in "var", add constraints to "bset"
4550 * that make the i-th bound smallest.
4552 * In particular, if there are n bounds b_i, then add the constraints
4554 * b_i <= b_j for j > i
4555 * b_i < b_j for j < i
4557 static __isl_give isl_basic_set *select_minimum(__isl_take isl_basic_set *bset,
4558 __isl_keep isl_mat *var, int i)
4560 isl_ctx *ctx;
4561 int j, k;
4563 ctx = isl_mat_get_ctx(var);
4565 for (j = 0; j < var->n_row; ++j) {
4566 if (j == i)
4567 continue;
4568 k = isl_basic_set_alloc_inequality(bset);
4569 if (k < 0)
4570 goto error;
4571 isl_seq_combine(bset->ineq[k], ctx->one, var->row[j],
4572 ctx->negone, var->row[i], var->n_col);
4573 isl_int_set_si(bset->ineq[k][var->n_col], 0);
4574 if (j < i)
4575 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
4578 bset = isl_basic_set_finalize(bset);
4580 return bset;
4581 error:
4582 isl_basic_set_free(bset);
4583 return NULL;
4586 /* Given a set of upper bounds on the last "input" variable m,
4587 * construct a set that assigns the minimal upper bound to m, i.e.,
4588 * construct a set that divides the space into cells where one
4589 * of the upper bounds is smaller than all the others and assign
4590 * this upper bound to m.
4592 * In particular, if there are n bounds b_i, then the result
4593 * consists of n basic sets, each one of the form
4595 * m = b_i
4596 * b_i <= b_j for j > i
4597 * b_i < b_j for j < i
4599 static __isl_give isl_set *set_minimum(__isl_take isl_space *dim,
4600 __isl_take isl_mat *var)
4602 int i, k;
4603 isl_basic_set *bset = NULL;
4604 isl_set *set = NULL;
4606 if (!dim || !var)
4607 goto error;
4609 set = isl_set_alloc_space(isl_space_copy(dim),
4610 var->n_row, ISL_SET_DISJOINT);
4612 for (i = 0; i < var->n_row; ++i) {
4613 bset = isl_basic_set_alloc_space(isl_space_copy(dim), 0,
4614 1, var->n_row - 1);
4615 k = isl_basic_set_alloc_equality(bset);
4616 if (k < 0)
4617 goto error;
4618 isl_seq_cpy(bset->eq[k], var->row[i], var->n_col);
4619 isl_int_set_si(bset->eq[k][var->n_col], -1);
4620 bset = select_minimum(bset, var, i);
4621 set = isl_set_add_basic_set(set, bset);
4624 isl_space_free(dim);
4625 isl_mat_free(var);
4626 return set;
4627 error:
4628 isl_basic_set_free(bset);
4629 isl_set_free(set);
4630 isl_space_free(dim);
4631 isl_mat_free(var);
4632 return NULL;
4635 /* Given that the last input variable of "bmap" represents the minimum
4636 * of the bounds in "cst", check whether we need to split the domain
4637 * based on which bound attains the minimum.
4639 * A split is needed when the minimum appears in an integer division
4640 * or in an equality. Otherwise, it is only needed if it appears in
4641 * an upper bound that is different from the upper bounds on which it
4642 * is defined.
4644 static int need_split_basic_map(__isl_keep isl_basic_map *bmap,
4645 __isl_keep isl_mat *cst)
4647 int i, j;
4648 unsigned total;
4649 unsigned pos;
4651 pos = cst->n_col - 1;
4652 total = isl_basic_map_dim(bmap, isl_dim_all);
4654 for (i = 0; i < bmap->n_div; ++i)
4655 if (!isl_int_is_zero(bmap->div[i][2 + pos]))
4656 return 1;
4658 for (i = 0; i < bmap->n_eq; ++i)
4659 if (!isl_int_is_zero(bmap->eq[i][1 + pos]))
4660 return 1;
4662 for (i = 0; i < bmap->n_ineq; ++i) {
4663 if (isl_int_is_nonneg(bmap->ineq[i][1 + pos]))
4664 continue;
4665 if (!isl_int_is_negone(bmap->ineq[i][1 + pos]))
4666 return 1;
4667 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + pos + 1,
4668 total - pos - 1) >= 0)
4669 return 1;
4671 for (j = 0; j < cst->n_row; ++j)
4672 if (isl_seq_eq(bmap->ineq[i], cst->row[j], cst->n_col))
4673 break;
4674 if (j >= cst->n_row)
4675 return 1;
4678 return 0;
4681 /* Given that the last set variable of "bset" represents the minimum
4682 * of the bounds in "cst", check whether we need to split the domain
4683 * based on which bound attains the minimum.
4685 * We simply call need_split_basic_map here. This is safe because
4686 * the position of the minimum is computed from "cst" and not
4687 * from "bmap".
4689 static int need_split_basic_set(__isl_keep isl_basic_set *bset,
4690 __isl_keep isl_mat *cst)
4692 return need_split_basic_map(bset_to_bmap(bset), cst);
4695 /* Given that the last set variable of "set" represents the minimum
4696 * of the bounds in "cst", check whether we need to split the domain
4697 * based on which bound attains the minimum.
4699 static int need_split_set(__isl_keep isl_set *set, __isl_keep isl_mat *cst)
4701 int i;
4703 for (i = 0; i < set->n; ++i)
4704 if (need_split_basic_set(set->p[i], cst))
4705 return 1;
4707 return 0;
4710 /* Given a set of which the last set variable is the minimum
4711 * of the bounds in "cst", split each basic set in the set
4712 * in pieces where one of the bounds is (strictly) smaller than the others.
4713 * This subdivision is given in "min_expr".
4714 * The variable is subsequently projected out.
4716 * We only do the split when it is needed.
4717 * For example if the last input variable m = min(a,b) and the only
4718 * constraints in the given basic set are lower bounds on m,
4719 * i.e., l <= m = min(a,b), then we can simply project out m
4720 * to obtain l <= a and l <= b, without having to split on whether
4721 * m is equal to a or b.
4723 static __isl_give isl_set *split(__isl_take isl_set *empty,
4724 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
4726 int n_in;
4727 int i;
4728 isl_space *dim;
4729 isl_set *res;
4731 if (!empty || !min_expr || !cst)
4732 goto error;
4734 n_in = isl_set_dim(empty, isl_dim_set);
4735 dim = isl_set_get_space(empty);
4736 dim = isl_space_drop_dims(dim, isl_dim_set, n_in - 1, 1);
4737 res = isl_set_empty(dim);
4739 for (i = 0; i < empty->n; ++i) {
4740 isl_set *set;
4742 set = isl_set_from_basic_set(isl_basic_set_copy(empty->p[i]));
4743 if (need_split_basic_set(empty->p[i], cst))
4744 set = isl_set_intersect(set, isl_set_copy(min_expr));
4745 set = isl_set_remove_dims(set, isl_dim_set, n_in - 1, 1);
4747 res = isl_set_union_disjoint(res, set);
4750 isl_set_free(empty);
4751 isl_set_free(min_expr);
4752 isl_mat_free(cst);
4753 return res;
4754 error:
4755 isl_set_free(empty);
4756 isl_set_free(min_expr);
4757 isl_mat_free(cst);
4758 return NULL;
4761 /* Given a map of which the last input variable is the minimum
4762 * of the bounds in "cst", split each basic set in the set
4763 * in pieces where one of the bounds is (strictly) smaller than the others.
4764 * This subdivision is given in "min_expr".
4765 * The variable is subsequently projected out.
4767 * The implementation is essentially the same as that of "split".
4769 static __isl_give isl_map *split_domain(__isl_take isl_map *opt,
4770 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
4772 int n_in;
4773 int i;
4774 isl_space *dim;
4775 isl_map *res;
4777 if (!opt || !min_expr || !cst)
4778 goto error;
4780 n_in = isl_map_dim(opt, isl_dim_in);
4781 dim = isl_map_get_space(opt);
4782 dim = isl_space_drop_dims(dim, isl_dim_in, n_in - 1, 1);
4783 res = isl_map_empty(dim);
4785 for (i = 0; i < opt->n; ++i) {
4786 isl_map *map;
4788 map = isl_map_from_basic_map(isl_basic_map_copy(opt->p[i]));
4789 if (need_split_basic_map(opt->p[i], cst))
4790 map = isl_map_intersect_domain(map,
4791 isl_set_copy(min_expr));
4792 map = isl_map_remove_dims(map, isl_dim_in, n_in - 1, 1);
4794 res = isl_map_union_disjoint(res, map);
4797 isl_map_free(opt);
4798 isl_set_free(min_expr);
4799 isl_mat_free(cst);
4800 return res;
4801 error:
4802 isl_map_free(opt);
4803 isl_set_free(min_expr);
4804 isl_mat_free(cst);
4805 return NULL;
4808 static __isl_give isl_map *basic_map_partial_lexopt(
4809 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4810 __isl_give isl_set **empty, int max);
4812 /* This function is called from basic_map_partial_lexopt_symm.
4813 * The last variable of "bmap" and "dom" corresponds to the minimum
4814 * of the bounds in "cst". "map_space" is the space of the original
4815 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
4816 * is the space of the original domain.
4818 * We recursively call basic_map_partial_lexopt and then plug in
4819 * the definition of the minimum in the result.
4821 static __isl_give isl_map *basic_map_partial_lexopt_symm_core(
4822 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4823 __isl_give isl_set **empty, int max, __isl_take isl_mat *cst,
4824 __isl_take isl_space *map_space, __isl_take isl_space *set_space)
4826 isl_map *opt;
4827 isl_set *min_expr;
4829 min_expr = set_minimum(isl_basic_set_get_space(dom), isl_mat_copy(cst));
4831 opt = basic_map_partial_lexopt(bmap, dom, empty, max);
4833 if (empty) {
4834 *empty = split(*empty,
4835 isl_set_copy(min_expr), isl_mat_copy(cst));
4836 *empty = isl_set_reset_space(*empty, set_space);
4839 opt = split_domain(opt, min_expr, cst);
4840 opt = isl_map_reset_space(opt, map_space);
4842 return opt;
4845 /* Extract a domain from "bmap" for the purpose of computing
4846 * a lexicographic optimum.
4848 * This function is only called when the caller wants to compute a full
4849 * lexicographic optimum, i.e., without specifying a domain. In this case,
4850 * the caller is not interested in the part of the domain space where
4851 * there is no solution and the domain can be initialized to those constraints
4852 * of "bmap" that only involve the parameters and the input dimensions.
4853 * This relieves the parametric programming engine from detecting those
4854 * inequalities and transferring them to the context. More importantly,
4855 * it ensures that those inequalities are transferred first and not
4856 * intermixed with inequalities that actually split the domain.
4858 * If the caller does not require the absence of existentially quantified
4859 * variables in the result (i.e., if ISL_OPT_QE is not set in "flags"),
4860 * then the actual domain of "bmap" can be used. This ensures that
4861 * the domain does not need to be split at all just to separate out
4862 * pieces of the domain that do not have a solution from piece that do.
4863 * This domain cannot be used in general because it may involve
4864 * (unknown) existentially quantified variables which will then also
4865 * appear in the solution.
4867 static __isl_give isl_basic_set *extract_domain(__isl_keep isl_basic_map *bmap,
4868 unsigned flags)
4870 int n_div;
4871 int n_out;
4873 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4874 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4875 bmap = isl_basic_map_copy(bmap);
4876 if (ISL_FL_ISSET(flags, ISL_OPT_QE)) {
4877 bmap = isl_basic_map_drop_constraints_involving_dims(bmap,
4878 isl_dim_div, 0, n_div);
4879 bmap = isl_basic_map_drop_constraints_involving_dims(bmap,
4880 isl_dim_out, 0, n_out);
4882 return isl_basic_map_domain(bmap);
4885 #undef TYPE
4886 #define TYPE isl_map
4887 #undef SUFFIX
4888 #define SUFFIX
4889 #include "isl_tab_lexopt_templ.c"
4891 struct isl_sol_for {
4892 struct isl_sol sol;
4893 int (*fn)(__isl_take isl_basic_set *dom,
4894 __isl_take isl_aff_list *list, void *user);
4895 void *user;
4898 static void sol_for_free(struct isl_sol *sol)
4902 /* Add the solution identified by the tableau and the context tableau.
4903 * In particular, "dom" represents the context and "ma" expresses
4904 * the solution on that context.
4906 * See documentation of sol_add for more details.
4908 * Instead of constructing a basic map, this function calls a user
4909 * defined function with the current context as a basic set and
4910 * a list of affine expressions representing the relation between
4911 * the input and output. The space over which the affine expressions
4912 * are defined is the same as that of the domain. The number of
4913 * affine expressions in the list is equal to the number of output variables.
4915 static void sol_for_add(struct isl_sol_for *sol,
4916 __isl_take isl_basic_set *dom, __isl_take isl_multi_aff *ma)
4918 int i, n;
4919 isl_ctx *ctx;
4920 isl_aff *aff;
4921 isl_aff_list *list;
4923 if (sol->sol.error || !dom || !ma)
4924 goto error;
4926 ctx = isl_basic_set_get_ctx(dom);
4927 n = isl_multi_aff_dim(ma, isl_dim_out);
4928 list = isl_aff_list_alloc(ctx, n);
4929 for (i = 0; i < n; ++i) {
4930 aff = isl_multi_aff_get_aff(ma, i);
4931 list = isl_aff_list_add(list, aff);
4934 dom = isl_basic_set_finalize(dom);
4936 if (sol->fn(isl_basic_set_copy(dom), list, sol->user) < 0)
4937 goto error;
4939 isl_basic_set_free(dom);
4940 isl_multi_aff_free(ma);
4941 return;
4942 error:
4943 isl_basic_set_free(dom);
4944 isl_multi_aff_free(ma);
4945 sol->sol.error = 1;
4948 static void sol_for_add_wrap(struct isl_sol *sol,
4949 __isl_take isl_basic_set *dom, __isl_take isl_multi_aff *ma)
4951 sol_for_add((struct isl_sol_for *)sol, dom, ma);
4954 static struct isl_sol_for *sol_for_init(__isl_keep isl_basic_map *bmap, int max,
4955 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4956 void *user),
4957 void *user)
4959 struct isl_sol_for *sol_for = NULL;
4960 isl_space *dom_dim;
4961 struct isl_basic_set *dom = NULL;
4963 sol_for = isl_calloc_type(bmap->ctx, struct isl_sol_for);
4964 if (!sol_for)
4965 goto error;
4967 dom_dim = isl_space_domain(isl_space_copy(bmap->dim));
4968 dom = isl_basic_set_universe(dom_dim);
4970 sol_for->sol.free = &sol_for_free;
4971 if (sol_init(&sol_for->sol, bmap, dom, max) < 0)
4972 goto error;
4973 sol_for->fn = fn;
4974 sol_for->user = user;
4975 sol_for->sol.add = &sol_for_add_wrap;
4976 sol_for->sol.add_empty = NULL;
4978 isl_basic_set_free(dom);
4979 return sol_for;
4980 error:
4981 isl_basic_set_free(dom);
4982 sol_free(&sol_for->sol);
4983 return NULL;
4986 static void sol_for_find_solutions(struct isl_sol_for *sol_for,
4987 struct isl_tab *tab)
4989 find_solutions_main(&sol_for->sol, tab);
4992 int isl_basic_map_foreach_lexopt(__isl_keep isl_basic_map *bmap, int max,
4993 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4994 void *user),
4995 void *user)
4997 struct isl_sol_for *sol_for = NULL;
4999 bmap = isl_basic_map_copy(bmap);
5000 bmap = isl_basic_map_detect_equalities(bmap);
5001 if (!bmap)
5002 return -1;
5004 sol_for = sol_for_init(bmap, max, fn, user);
5005 if (!sol_for)
5006 goto error;
5008 if (isl_basic_map_plain_is_empty(bmap))
5009 /* nothing */;
5010 else {
5011 struct isl_tab *tab;
5012 struct isl_context *context = sol_for->sol.context;
5013 tab = tab_for_lexmin(bmap,
5014 context->op->peek_basic_set(context), 1, max);
5015 tab = context->op->detect_nonnegative_parameters(context, tab);
5016 sol_for_find_solutions(sol_for, tab);
5017 if (sol_for->sol.error)
5018 goto error;
5021 sol_free(&sol_for->sol);
5022 isl_basic_map_free(bmap);
5023 return 0;
5024 error:
5025 sol_free(&sol_for->sol);
5026 isl_basic_map_free(bmap);
5027 return -1;
5030 int isl_basic_set_foreach_lexopt(__isl_keep isl_basic_set *bset, int max,
5031 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
5032 void *user),
5033 void *user)
5035 return isl_basic_map_foreach_lexopt(bset, max, fn, user);
5038 /* Check if the given sequence of len variables starting at pos
5039 * represents a trivial (i.e., zero) solution.
5040 * The variables are assumed to be non-negative and to come in pairs,
5041 * with each pair representing a variable of unrestricted sign.
5042 * The solution is trivial if each such pair in the sequence consists
5043 * of two identical values, meaning that the variable being represented
5044 * has value zero.
5046 static int region_is_trivial(struct isl_tab *tab, int pos, int len)
5048 int i;
5050 if (len == 0)
5051 return 0;
5053 for (i = 0; i < len; i += 2) {
5054 int neg_row;
5055 int pos_row;
5057 neg_row = tab->var[pos + i].is_row ?
5058 tab->var[pos + i].index : -1;
5059 pos_row = tab->var[pos + i + 1].is_row ?
5060 tab->var[pos + i + 1].index : -1;
5062 if ((neg_row < 0 ||
5063 isl_int_is_zero(tab->mat->row[neg_row][1])) &&
5064 (pos_row < 0 ||
5065 isl_int_is_zero(tab->mat->row[pos_row][1])))
5066 continue;
5068 if (neg_row < 0 || pos_row < 0)
5069 return 0;
5070 if (isl_int_ne(tab->mat->row[neg_row][1],
5071 tab->mat->row[pos_row][1]))
5072 return 0;
5075 return 1;
5078 /* Return the index of the first trivial region or -1 if all regions
5079 * are non-trivial.
5081 static int first_trivial_region(struct isl_tab *tab,
5082 int n_region, struct isl_region *region)
5084 int i;
5086 for (i = 0; i < n_region; ++i) {
5087 if (region_is_trivial(tab, region[i].pos, region[i].len))
5088 return i;
5091 return -1;
5094 /* Check if the solution is optimal, i.e., whether the first
5095 * n_op entries are zero.
5097 static int is_optimal(__isl_keep isl_vec *sol, int n_op)
5099 int i;
5101 for (i = 0; i < n_op; ++i)
5102 if (!isl_int_is_zero(sol->el[1 + i]))
5103 return 0;
5104 return 1;
5107 /* Add constraints to "tab" that ensure that any solution is significantly
5108 * better than that represented by "sol". That is, find the first
5109 * relevant (within first n_op) non-zero coefficient and force it (along
5110 * with all previous coefficients) to be zero.
5111 * If the solution is already optimal (all relevant coefficients are zero),
5112 * then just mark the table as empty.
5114 * This function assumes that at least 2 * n_op more rows and at least
5115 * 2 * n_op more elements in the constraint array are available in the tableau.
5117 static int force_better_solution(struct isl_tab *tab,
5118 __isl_keep isl_vec *sol, int n_op)
5120 int i;
5121 isl_ctx *ctx;
5122 isl_vec *v = NULL;
5124 if (!sol)
5125 return -1;
5127 for (i = 0; i < n_op; ++i)
5128 if (!isl_int_is_zero(sol->el[1 + i]))
5129 break;
5131 if (i == n_op) {
5132 if (isl_tab_mark_empty(tab) < 0)
5133 return -1;
5134 return 0;
5137 ctx = isl_vec_get_ctx(sol);
5138 v = isl_vec_alloc(ctx, 1 + tab->n_var);
5139 if (!v)
5140 return -1;
5142 for (; i >= 0; --i) {
5143 v = isl_vec_clr(v);
5144 isl_int_set_si(v->el[1 + i], -1);
5145 if (add_lexmin_eq(tab, v->el) < 0)
5146 goto error;
5149 isl_vec_free(v);
5150 return 0;
5151 error:
5152 isl_vec_free(v);
5153 return -1;
5156 struct isl_trivial {
5157 int update;
5158 int region;
5159 int side;
5160 struct isl_tab_undo *snap;
5163 /* Return the lexicographically smallest non-trivial solution of the
5164 * given ILP problem.
5166 * All variables are assumed to be non-negative.
5168 * n_op is the number of initial coordinates to optimize.
5169 * That is, once a solution has been found, we will only continue looking
5170 * for solution that result in significantly better values for those
5171 * initial coordinates. That is, we only continue looking for solutions
5172 * that increase the number of initial zeros in this sequence.
5174 * A solution is non-trivial, if it is non-trivial on each of the
5175 * specified regions. Each region represents a sequence of pairs
5176 * of variables. A solution is non-trivial on such a region if
5177 * at least one of these pairs consists of different values, i.e.,
5178 * such that the non-negative variable represented by the pair is non-zero.
5180 * Whenever a conflict is encountered, all constraints involved are
5181 * reported to the caller through a call to "conflict".
5183 * We perform a simple branch-and-bound backtracking search.
5184 * Each level in the search represents initially trivial region that is forced
5185 * to be non-trivial.
5186 * At each level we consider n cases, where n is the length of the region.
5187 * In terms of the n/2 variables of unrestricted signs being encoded by
5188 * the region, we consider the cases
5189 * x_0 >= 1
5190 * x_0 <= -1
5191 * x_0 = 0 and x_1 >= 1
5192 * x_0 = 0 and x_1 <= -1
5193 * x_0 = 0 and x_1 = 0 and x_2 >= 1
5194 * x_0 = 0 and x_1 = 0 and x_2 <= -1
5195 * ...
5196 * The cases are considered in this order, assuming that each pair
5197 * x_i_a x_i_b represents the value x_i_b - x_i_a.
5198 * That is, x_0 >= 1 is enforced by adding the constraint
5199 * x_0_b - x_0_a >= 1
5201 __isl_give isl_vec *isl_tab_basic_set_non_trivial_lexmin(
5202 __isl_take isl_basic_set *bset, int n_op, int n_region,
5203 struct isl_region *region,
5204 int (*conflict)(int con, void *user), void *user)
5206 int i, j;
5207 int r;
5208 isl_ctx *ctx;
5209 isl_vec *v = NULL;
5210 isl_vec *sol = NULL;
5211 struct isl_tab *tab;
5212 struct isl_trivial *triv = NULL;
5213 int level, init;
5215 if (!bset)
5216 return NULL;
5218 ctx = isl_basic_set_get_ctx(bset);
5219 sol = isl_vec_alloc(ctx, 0);
5221 tab = tab_for_lexmin(bset, NULL, 0, 0);
5222 if (!tab)
5223 goto error;
5224 tab->conflict = conflict;
5225 tab->conflict_user = user;
5227 v = isl_vec_alloc(ctx, 1 + tab->n_var);
5228 triv = isl_calloc_array(ctx, struct isl_trivial, n_region);
5229 if (!v || (n_region && !triv))
5230 goto error;
5232 level = 0;
5233 init = 1;
5235 while (level >= 0) {
5236 int side, base;
5238 if (init) {
5239 tab = cut_to_integer_lexmin(tab, CUT_ONE);
5240 if (!tab)
5241 goto error;
5242 if (tab->empty)
5243 goto backtrack;
5244 r = first_trivial_region(tab, n_region, region);
5245 if (r < 0) {
5246 for (i = 0; i < level; ++i)
5247 triv[i].update = 1;
5248 isl_vec_free(sol);
5249 sol = isl_tab_get_sample_value(tab);
5250 if (!sol)
5251 goto error;
5252 if (is_optimal(sol, n_op))
5253 break;
5254 goto backtrack;
5256 if (level >= n_region)
5257 isl_die(ctx, isl_error_internal,
5258 "nesting level too deep", goto error);
5259 if (isl_tab_extend_cons(tab,
5260 2 * region[r].len + 2 * n_op) < 0)
5261 goto error;
5262 triv[level].region = r;
5263 triv[level].side = 0;
5266 r = triv[level].region;
5267 side = triv[level].side;
5268 base = 2 * (side/2);
5270 if (side >= region[r].len) {
5271 backtrack:
5272 level--;
5273 init = 0;
5274 if (level >= 0)
5275 if (isl_tab_rollback(tab, triv[level].snap) < 0)
5276 goto error;
5277 continue;
5280 if (triv[level].update) {
5281 if (force_better_solution(tab, sol, n_op) < 0)
5282 goto error;
5283 triv[level].update = 0;
5286 if (side == base && base >= 2) {
5287 for (j = base - 2; j < base; ++j) {
5288 v = isl_vec_clr(v);
5289 isl_int_set_si(v->el[1 + region[r].pos + j], 1);
5290 if (add_lexmin_eq(tab, v->el) < 0)
5291 goto error;
5295 triv[level].snap = isl_tab_snap(tab);
5296 if (isl_tab_push_basis(tab) < 0)
5297 goto error;
5299 v = isl_vec_clr(v);
5300 isl_int_set_si(v->el[0], -1);
5301 isl_int_set_si(v->el[1 + region[r].pos + side], -1);
5302 isl_int_set_si(v->el[1 + region[r].pos + (side ^ 1)], 1);
5303 tab = add_lexmin_ineq(tab, v->el);
5305 triv[level].side++;
5306 level++;
5307 init = 1;
5310 free(triv);
5311 isl_vec_free(v);
5312 isl_tab_free(tab);
5313 isl_basic_set_free(bset);
5315 return sol;
5316 error:
5317 free(triv);
5318 isl_vec_free(v);
5319 isl_tab_free(tab);
5320 isl_basic_set_free(bset);
5321 isl_vec_free(sol);
5322 return NULL;
5325 /* Wrapper for a tableau that is used for computing
5326 * the lexicographically smallest rational point of a non-negative set.
5327 * This point is represented by the sample value of "tab",
5328 * unless "tab" is empty.
5330 struct isl_tab_lexmin {
5331 isl_ctx *ctx;
5332 struct isl_tab *tab;
5335 /* Free "tl" and return NULL.
5337 __isl_null isl_tab_lexmin *isl_tab_lexmin_free(__isl_take isl_tab_lexmin *tl)
5339 if (!tl)
5340 return NULL;
5341 isl_ctx_deref(tl->ctx);
5342 isl_tab_free(tl->tab);
5343 free(tl);
5345 return NULL;
5348 /* Construct an isl_tab_lexmin for computing
5349 * the lexicographically smallest rational point in "bset",
5350 * assuming that all variables are non-negative.
5352 __isl_give isl_tab_lexmin *isl_tab_lexmin_from_basic_set(
5353 __isl_take isl_basic_set *bset)
5355 isl_ctx *ctx;
5356 isl_tab_lexmin *tl;
5358 if (!bset)
5359 return NULL;
5361 ctx = isl_basic_set_get_ctx(bset);
5362 tl = isl_calloc_type(ctx, struct isl_tab_lexmin);
5363 if (!tl)
5364 goto error;
5365 tl->ctx = ctx;
5366 isl_ctx_ref(ctx);
5367 tl->tab = tab_for_lexmin(bset, NULL, 0, 0);
5368 isl_basic_set_free(bset);
5369 if (!tl->tab)
5370 return isl_tab_lexmin_free(tl);
5371 return tl;
5372 error:
5373 isl_basic_set_free(bset);
5374 isl_tab_lexmin_free(tl);
5375 return NULL;
5378 /* Return the dimension of the set represented by "tl".
5380 int isl_tab_lexmin_dim(__isl_keep isl_tab_lexmin *tl)
5382 return tl ? tl->tab->n_var : -1;
5385 /* Add the equality with coefficients "eq" to "tl", updating the optimal
5386 * solution if needed.
5387 * The equality is added as two opposite inequality constraints.
5389 __isl_give isl_tab_lexmin *isl_tab_lexmin_add_eq(__isl_take isl_tab_lexmin *tl,
5390 isl_int *eq)
5392 unsigned n_var;
5394 if (!tl || !eq)
5395 return isl_tab_lexmin_free(tl);
5397 if (isl_tab_extend_cons(tl->tab, 2) < 0)
5398 return isl_tab_lexmin_free(tl);
5399 n_var = tl->tab->n_var;
5400 isl_seq_neg(eq, eq, 1 + n_var);
5401 tl->tab = add_lexmin_ineq(tl->tab, eq);
5402 isl_seq_neg(eq, eq, 1 + n_var);
5403 tl->tab = add_lexmin_ineq(tl->tab, eq);
5405 if (!tl->tab)
5406 return isl_tab_lexmin_free(tl);
5408 return tl;
5411 /* Return the lexicographically smallest rational point in the basic set
5412 * from which "tl" was constructed.
5413 * If the original input was empty, then return a zero-length vector.
5415 __isl_give isl_vec *isl_tab_lexmin_get_solution(__isl_keep isl_tab_lexmin *tl)
5417 if (!tl)
5418 return NULL;
5419 if (tl->tab->empty)
5420 return isl_vec_alloc(tl->ctx, 0);
5421 else
5422 return isl_tab_get_sample_value(tl->tab);
5425 /* Return the lexicographically smallest rational point in "bset",
5426 * assuming that all variables are non-negative.
5427 * If "bset" is empty, then return a zero-length vector.
5429 __isl_give isl_vec *isl_tab_basic_set_non_neg_lexmin(
5430 __isl_take isl_basic_set *bset)
5432 isl_tab_lexmin *tl;
5433 isl_vec *sol;
5435 tl = isl_tab_lexmin_from_basic_set(bset);
5436 sol = isl_tab_lexmin_get_solution(tl);
5437 isl_tab_lexmin_free(tl);
5438 return sol;
5441 struct isl_sol_pma {
5442 struct isl_sol sol;
5443 isl_pw_multi_aff *pma;
5444 isl_set *empty;
5447 static void sol_pma_free(struct isl_sol *sol)
5449 struct isl_sol_pma *sol_pma = (struct isl_sol_pma *) sol;
5450 isl_pw_multi_aff_free(sol_pma->pma);
5451 isl_set_free(sol_pma->empty);
5454 /* This function is called for parts of the context where there is
5455 * no solution, with "bset" corresponding to the context tableau.
5456 * Simply add the basic set to the set "empty".
5458 static void sol_pma_add_empty(struct isl_sol_pma *sol,
5459 __isl_take isl_basic_set *bset)
5461 if (!bset || !sol->empty)
5462 goto error;
5464 sol->empty = isl_set_grow(sol->empty, 1);
5465 bset = isl_basic_set_simplify(bset);
5466 bset = isl_basic_set_finalize(bset);
5467 sol->empty = isl_set_add_basic_set(sol->empty, bset);
5468 if (!sol->empty)
5469 sol->sol.error = 1;
5470 return;
5471 error:
5472 isl_basic_set_free(bset);
5473 sol->sol.error = 1;
5476 /* Given a basic set "dom" that represents the context and a tuple of
5477 * affine expressions "maff" defined over this domain, construct
5478 * an isl_pw_multi_aff with a single cell corresponding to "dom" and
5479 * the affine expressions in "maff".
5481 static void sol_pma_add(struct isl_sol_pma *sol,
5482 __isl_take isl_basic_set *dom, __isl_take isl_multi_aff *maff)
5484 isl_pw_multi_aff *pma;
5486 dom = isl_basic_set_simplify(dom);
5487 dom = isl_basic_set_finalize(dom);
5488 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(dom), maff);
5489 sol->pma = isl_pw_multi_aff_add_disjoint(sol->pma, pma);
5490 if (!sol->pma)
5491 sol->sol.error = 1;
5494 static void sol_pma_add_empty_wrap(struct isl_sol *sol,
5495 __isl_take isl_basic_set *bset)
5497 sol_pma_add_empty((struct isl_sol_pma *)sol, bset);
5500 static void sol_pma_add_wrap(struct isl_sol *sol,
5501 __isl_take isl_basic_set *dom, __isl_take isl_multi_aff *ma)
5503 sol_pma_add((struct isl_sol_pma *)sol, dom, ma);
5506 /* Construct an isl_sol_pma structure for accumulating the solution.
5507 * If track_empty is set, then we also keep track of the parts
5508 * of the context where there is no solution.
5509 * If max is set, then we are solving a maximization, rather than
5510 * a minimization problem, which means that the variables in the
5511 * tableau have value "M - x" rather than "M + x".
5513 static struct isl_sol *sol_pma_init(__isl_keep isl_basic_map *bmap,
5514 __isl_take isl_basic_set *dom, int track_empty, int max)
5516 struct isl_sol_pma *sol_pma = NULL;
5517 isl_space *space;
5519 if (!bmap)
5520 goto error;
5522 sol_pma = isl_calloc_type(bmap->ctx, struct isl_sol_pma);
5523 if (!sol_pma)
5524 goto error;
5526 sol_pma->sol.free = &sol_pma_free;
5527 if (sol_init(&sol_pma->sol, bmap, dom, max) < 0)
5528 goto error;
5529 sol_pma->sol.add = &sol_pma_add_wrap;
5530 sol_pma->sol.add_empty = track_empty ? &sol_pma_add_empty_wrap : NULL;
5531 space = isl_space_copy(sol_pma->sol.space);
5532 sol_pma->pma = isl_pw_multi_aff_empty(space);
5533 if (!sol_pma->pma)
5534 goto error;
5536 if (track_empty) {
5537 sol_pma->empty = isl_set_alloc_space(isl_basic_set_get_space(dom),
5538 1, ISL_SET_DISJOINT);
5539 if (!sol_pma->empty)
5540 goto error;
5543 isl_basic_set_free(dom);
5544 return &sol_pma->sol;
5545 error:
5546 isl_basic_set_free(dom);
5547 sol_free(&sol_pma->sol);
5548 return NULL;
5551 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
5552 * some obvious symmetries.
5554 * We call basic_map_partial_lexopt_base_sol and extract the results.
5556 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_base_pw_multi_aff(
5557 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5558 __isl_give isl_set **empty, int max)
5560 isl_pw_multi_aff *result = NULL;
5561 struct isl_sol *sol;
5562 struct isl_sol_pma *sol_pma;
5564 sol = basic_map_partial_lexopt_base_sol(bmap, dom, empty, max,
5565 &sol_pma_init);
5566 if (!sol)
5567 return NULL;
5568 sol_pma = (struct isl_sol_pma *) sol;
5570 result = isl_pw_multi_aff_copy(sol_pma->pma);
5571 if (empty)
5572 *empty = isl_set_copy(sol_pma->empty);
5573 sol_free(&sol_pma->sol);
5574 return result;
5577 /* Given that the last input variable of "maff" represents the minimum
5578 * of some bounds, check whether we need to plug in the expression
5579 * of the minimum.
5581 * In particular, check if the last input variable appears in any
5582 * of the expressions in "maff".
5584 static int need_substitution(__isl_keep isl_multi_aff *maff)
5586 int i;
5587 unsigned pos;
5589 pos = isl_multi_aff_dim(maff, isl_dim_in) - 1;
5591 for (i = 0; i < maff->n; ++i)
5592 if (isl_aff_involves_dims(maff->p[i], isl_dim_in, pos, 1))
5593 return 1;
5595 return 0;
5598 /* Given a set of upper bounds on the last "input" variable m,
5599 * construct a piecewise affine expression that selects
5600 * the minimal upper bound to m, i.e.,
5601 * divide the space into cells where one
5602 * of the upper bounds is smaller than all the others and select
5603 * this upper bound on that cell.
5605 * In particular, if there are n bounds b_i, then the result
5606 * consists of n cell, each one of the form
5608 * b_i <= b_j for j > i
5609 * b_i < b_j for j < i
5611 * The affine expression on this cell is
5613 * b_i
5615 static __isl_give isl_pw_aff *set_minimum_pa(__isl_take isl_space *space,
5616 __isl_take isl_mat *var)
5618 int i;
5619 isl_aff *aff = NULL;
5620 isl_basic_set *bset = NULL;
5621 isl_pw_aff *paff = NULL;
5622 isl_space *pw_space;
5623 isl_local_space *ls = NULL;
5625 if (!space || !var)
5626 goto error;
5628 ls = isl_local_space_from_space(isl_space_copy(space));
5629 pw_space = isl_space_copy(space);
5630 pw_space = isl_space_from_domain(pw_space);
5631 pw_space = isl_space_add_dims(pw_space, isl_dim_out, 1);
5632 paff = isl_pw_aff_alloc_size(pw_space, var->n_row);
5634 for (i = 0; i < var->n_row; ++i) {
5635 isl_pw_aff *paff_i;
5637 aff = isl_aff_alloc(isl_local_space_copy(ls));
5638 bset = isl_basic_set_alloc_space(isl_space_copy(space), 0,
5639 0, var->n_row - 1);
5640 if (!aff || !bset)
5641 goto error;
5642 isl_int_set_si(aff->v->el[0], 1);
5643 isl_seq_cpy(aff->v->el + 1, var->row[i], var->n_col);
5644 isl_int_set_si(aff->v->el[1 + var->n_col], 0);
5645 bset = select_minimum(bset, var, i);
5646 paff_i = isl_pw_aff_alloc(isl_set_from_basic_set(bset), aff);
5647 paff = isl_pw_aff_add_disjoint(paff, paff_i);
5650 isl_local_space_free(ls);
5651 isl_space_free(space);
5652 isl_mat_free(var);
5653 return paff;
5654 error:
5655 isl_aff_free(aff);
5656 isl_basic_set_free(bset);
5657 isl_pw_aff_free(paff);
5658 isl_local_space_free(ls);
5659 isl_space_free(space);
5660 isl_mat_free(var);
5661 return NULL;
5664 /* Given a piecewise multi-affine expression of which the last input variable
5665 * is the minimum of the bounds in "cst", plug in the value of the minimum.
5666 * This minimum expression is given in "min_expr_pa".
5667 * The set "min_expr" contains the same information, but in the form of a set.
5668 * The variable is subsequently projected out.
5670 * The implementation is similar to those of "split" and "split_domain".
5671 * If the variable appears in a given expression, then minimum expression
5672 * is plugged in. Otherwise, if the variable appears in the constraints
5673 * and a split is required, then the domain is split. Otherwise, no split
5674 * is performed.
5676 static __isl_give isl_pw_multi_aff *split_domain_pma(
5677 __isl_take isl_pw_multi_aff *opt, __isl_take isl_pw_aff *min_expr_pa,
5678 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
5680 int n_in;
5681 int i;
5682 isl_space *space;
5683 isl_pw_multi_aff *res;
5685 if (!opt || !min_expr || !cst)
5686 goto error;
5688 n_in = isl_pw_multi_aff_dim(opt, isl_dim_in);
5689 space = isl_pw_multi_aff_get_space(opt);
5690 space = isl_space_drop_dims(space, isl_dim_in, n_in - 1, 1);
5691 res = isl_pw_multi_aff_empty(space);
5693 for (i = 0; i < opt->n; ++i) {
5694 isl_pw_multi_aff *pma;
5696 pma = isl_pw_multi_aff_alloc(isl_set_copy(opt->p[i].set),
5697 isl_multi_aff_copy(opt->p[i].maff));
5698 if (need_substitution(opt->p[i].maff))
5699 pma = isl_pw_multi_aff_substitute(pma,
5700 isl_dim_in, n_in - 1, min_expr_pa);
5701 else if (need_split_set(opt->p[i].set, cst))
5702 pma = isl_pw_multi_aff_intersect_domain(pma,
5703 isl_set_copy(min_expr));
5704 pma = isl_pw_multi_aff_project_out(pma,
5705 isl_dim_in, n_in - 1, 1);
5707 res = isl_pw_multi_aff_add_disjoint(res, pma);
5710 isl_pw_multi_aff_free(opt);
5711 isl_pw_aff_free(min_expr_pa);
5712 isl_set_free(min_expr);
5713 isl_mat_free(cst);
5714 return res;
5715 error:
5716 isl_pw_multi_aff_free(opt);
5717 isl_pw_aff_free(min_expr_pa);
5718 isl_set_free(min_expr);
5719 isl_mat_free(cst);
5720 return NULL;
5723 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_pw_multi_aff(
5724 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5725 __isl_give isl_set **empty, int max);
5727 /* This function is called from basic_map_partial_lexopt_symm.
5728 * The last variable of "bmap" and "dom" corresponds to the minimum
5729 * of the bounds in "cst". "map_space" is the space of the original
5730 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
5731 * is the space of the original domain.
5733 * We recursively call basic_map_partial_lexopt and then plug in
5734 * the definition of the minimum in the result.
5736 static __isl_give isl_pw_multi_aff *
5737 basic_map_partial_lexopt_symm_core_pw_multi_aff(
5738 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5739 __isl_give isl_set **empty, int max, __isl_take isl_mat *cst,
5740 __isl_take isl_space *map_space, __isl_take isl_space *set_space)
5742 isl_pw_multi_aff *opt;
5743 isl_pw_aff *min_expr_pa;
5744 isl_set *min_expr;
5746 min_expr = set_minimum(isl_basic_set_get_space(dom), isl_mat_copy(cst));
5747 min_expr_pa = set_minimum_pa(isl_basic_set_get_space(dom),
5748 isl_mat_copy(cst));
5750 opt = basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, max);
5752 if (empty) {
5753 *empty = split(*empty,
5754 isl_set_copy(min_expr), isl_mat_copy(cst));
5755 *empty = isl_set_reset_space(*empty, set_space);
5758 opt = split_domain_pma(opt, min_expr_pa, min_expr, cst);
5759 opt = isl_pw_multi_aff_reset_space(opt, map_space);
5761 return opt;
5764 #undef TYPE
5765 #define TYPE isl_pw_multi_aff
5766 #undef SUFFIX
5767 #define SUFFIX _pw_multi_aff
5768 #include "isl_tab_lexopt_templ.c"