isl_factorization.h: protect against multiple inclusions
[isl.git] / isl_tab_pip.c
blob20abe47e3f3adad0a0218680198ae85d058dc010
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 isl_stat 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 ? isl_stat_error : isl_stat_ok;
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 isl_stat (*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 isl_stat 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) ? isl_stat_ok : isl_stat_error;
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 (isl_tab_track_bset(tab, bset) < 0)
2759 goto error;
2760 tab = isl_tab_init_samples(tab);
2761 return tab;
2762 error:
2763 isl_tab_free(tab);
2764 return NULL;
2767 static struct isl_context *isl_context_lex_alloc(struct isl_basic_set *dom)
2769 struct isl_context_lex *clex;
2771 if (!dom)
2772 return NULL;
2774 clex = isl_alloc_type(dom->ctx, struct isl_context_lex);
2775 if (!clex)
2776 return NULL;
2778 clex->context.op = &isl_context_lex_op;
2780 clex->tab = context_tab_for_lexmin(isl_basic_set_copy(dom));
2781 if (restore_lexmin(clex->tab) < 0)
2782 goto error;
2783 clex->tab = check_integer_feasible(clex->tab);
2784 if (!clex->tab)
2785 goto error;
2787 return &clex->context;
2788 error:
2789 clex->context.op->free(&clex->context);
2790 return NULL;
2793 /* Representation of the context when using generalized basis reduction.
2795 * "shifted" contains the offsets of the unit hypercubes that lie inside the
2796 * context. Any rational point in "shifted" can therefore be rounded
2797 * up to an integer point in the context.
2798 * If the context is constrained by any equality, then "shifted" is not used
2799 * as it would be empty.
2801 struct isl_context_gbr {
2802 struct isl_context context;
2803 struct isl_tab *tab;
2804 struct isl_tab *shifted;
2805 struct isl_tab *cone;
2808 static struct isl_tab *context_gbr_detect_nonnegative_parameters(
2809 struct isl_context *context, struct isl_tab *tab)
2811 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2812 if (!tab)
2813 return NULL;
2814 return tab_detect_nonnegative_parameters(tab, cgbr->tab);
2817 static struct isl_basic_set *context_gbr_peek_basic_set(
2818 struct isl_context *context)
2820 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2821 if (!cgbr->tab)
2822 return NULL;
2823 return isl_tab_peek_bset(cgbr->tab);
2826 static struct isl_tab *context_gbr_peek_tab(struct isl_context *context)
2828 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2829 return cgbr->tab;
2832 /* Initialize the "shifted" tableau of the context, which
2833 * contains the constraints of the original tableau shifted
2834 * by the sum of all negative coefficients. This ensures
2835 * that any rational point in the shifted tableau can
2836 * be rounded up to yield an integer point in the original tableau.
2838 static void gbr_init_shifted(struct isl_context_gbr *cgbr)
2840 int i, j;
2841 struct isl_vec *cst;
2842 struct isl_basic_set *bset = isl_tab_peek_bset(cgbr->tab);
2843 unsigned dim = isl_basic_set_total_dim(bset);
2845 cst = isl_vec_alloc(cgbr->tab->mat->ctx, bset->n_ineq);
2846 if (!cst)
2847 return;
2849 for (i = 0; i < bset->n_ineq; ++i) {
2850 isl_int_set(cst->el[i], bset->ineq[i][0]);
2851 for (j = 0; j < dim; ++j) {
2852 if (!isl_int_is_neg(bset->ineq[i][1 + j]))
2853 continue;
2854 isl_int_add(bset->ineq[i][0], bset->ineq[i][0],
2855 bset->ineq[i][1 + j]);
2859 cgbr->shifted = isl_tab_from_basic_set(bset, 0);
2861 for (i = 0; i < bset->n_ineq; ++i)
2862 isl_int_set(bset->ineq[i][0], cst->el[i]);
2864 isl_vec_free(cst);
2867 /* Check if the shifted tableau is non-empty, and if so
2868 * use the sample point to construct an integer point
2869 * of the context tableau.
2871 static struct isl_vec *gbr_get_shifted_sample(struct isl_context_gbr *cgbr)
2873 struct isl_vec *sample;
2875 if (!cgbr->shifted)
2876 gbr_init_shifted(cgbr);
2877 if (!cgbr->shifted)
2878 return NULL;
2879 if (cgbr->shifted->empty)
2880 return isl_vec_alloc(cgbr->tab->mat->ctx, 0);
2882 sample = isl_tab_get_sample_value(cgbr->shifted);
2883 sample = isl_vec_ceil(sample);
2885 return sample;
2888 static struct isl_basic_set *drop_constant_terms(struct isl_basic_set *bset)
2890 int i;
2892 if (!bset)
2893 return NULL;
2895 for (i = 0; i < bset->n_eq; ++i)
2896 isl_int_set_si(bset->eq[i][0], 0);
2898 for (i = 0; i < bset->n_ineq; ++i)
2899 isl_int_set_si(bset->ineq[i][0], 0);
2901 return bset;
2904 static int use_shifted(struct isl_context_gbr *cgbr)
2906 if (!cgbr->tab)
2907 return 0;
2908 return cgbr->tab->bmap->n_eq == 0 && cgbr->tab->bmap->n_div == 0;
2911 static struct isl_vec *gbr_get_sample(struct isl_context_gbr *cgbr)
2913 struct isl_basic_set *bset;
2914 struct isl_basic_set *cone;
2916 if (isl_tab_sample_is_integer(cgbr->tab))
2917 return isl_tab_get_sample_value(cgbr->tab);
2919 if (use_shifted(cgbr)) {
2920 struct isl_vec *sample;
2922 sample = gbr_get_shifted_sample(cgbr);
2923 if (!sample || sample->size > 0)
2924 return sample;
2926 isl_vec_free(sample);
2929 if (!cgbr->cone) {
2930 bset = isl_tab_peek_bset(cgbr->tab);
2931 cgbr->cone = isl_tab_from_recession_cone(bset, 0);
2932 if (!cgbr->cone)
2933 return NULL;
2934 if (isl_tab_track_bset(cgbr->cone,
2935 isl_basic_set_copy(bset)) < 0)
2936 return NULL;
2938 if (isl_tab_detect_implicit_equalities(cgbr->cone) < 0)
2939 return NULL;
2941 if (cgbr->cone->n_dead == cgbr->cone->n_col) {
2942 struct isl_vec *sample;
2943 struct isl_tab_undo *snap;
2945 if (cgbr->tab->basis) {
2946 if (cgbr->tab->basis->n_col != 1 + cgbr->tab->n_var) {
2947 isl_mat_free(cgbr->tab->basis);
2948 cgbr->tab->basis = NULL;
2950 cgbr->tab->n_zero = 0;
2951 cgbr->tab->n_unbounded = 0;
2954 snap = isl_tab_snap(cgbr->tab);
2956 sample = isl_tab_sample(cgbr->tab);
2958 if (!sample || isl_tab_rollback(cgbr->tab, snap) < 0) {
2959 isl_vec_free(sample);
2960 return NULL;
2963 return sample;
2966 cone = isl_basic_set_dup(isl_tab_peek_bset(cgbr->cone));
2967 cone = drop_constant_terms(cone);
2968 cone = isl_basic_set_update_from_tab(cone, cgbr->cone);
2969 cone = isl_basic_set_underlying_set(cone);
2970 cone = isl_basic_set_gauss(cone, NULL);
2972 bset = isl_basic_set_dup(isl_tab_peek_bset(cgbr->tab));
2973 bset = isl_basic_set_update_from_tab(bset, cgbr->tab);
2974 bset = isl_basic_set_underlying_set(bset);
2975 bset = isl_basic_set_gauss(bset, NULL);
2977 return isl_basic_set_sample_with_cone(bset, cone);
2980 static void check_gbr_integer_feasible(struct isl_context_gbr *cgbr)
2982 struct isl_vec *sample;
2984 if (!cgbr->tab)
2985 return;
2987 if (cgbr->tab->empty)
2988 return;
2990 sample = gbr_get_sample(cgbr);
2991 if (!sample)
2992 goto error;
2994 if (sample->size == 0) {
2995 isl_vec_free(sample);
2996 if (isl_tab_mark_empty(cgbr->tab) < 0)
2997 goto error;
2998 return;
3001 if (isl_tab_add_sample(cgbr->tab, sample) < 0)
3002 goto error;
3004 return;
3005 error:
3006 isl_tab_free(cgbr->tab);
3007 cgbr->tab = NULL;
3010 static struct isl_tab *add_gbr_eq(struct isl_tab *tab, isl_int *eq)
3012 if (!tab)
3013 return NULL;
3015 if (isl_tab_extend_cons(tab, 2) < 0)
3016 goto error;
3018 if (isl_tab_add_eq(tab, eq) < 0)
3019 goto error;
3021 return tab;
3022 error:
3023 isl_tab_free(tab);
3024 return NULL;
3027 /* Add the equality described by "eq" to the context.
3028 * If "check" is set, then we check if the context is empty after
3029 * adding the equality.
3030 * If "update" is set, then we check if the samples are still valid.
3032 * We do not explicitly add shifted copies of the equality to
3033 * cgbr->shifted since they would conflict with each other.
3034 * Instead, we directly mark cgbr->shifted empty.
3036 static void context_gbr_add_eq(struct isl_context *context, isl_int *eq,
3037 int check, int update)
3039 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3041 cgbr->tab = add_gbr_eq(cgbr->tab, eq);
3043 if (cgbr->shifted && !cgbr->shifted->empty && use_shifted(cgbr)) {
3044 if (isl_tab_mark_empty(cgbr->shifted) < 0)
3045 goto error;
3048 if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
3049 if (isl_tab_extend_cons(cgbr->cone, 2) < 0)
3050 goto error;
3051 if (isl_tab_add_eq(cgbr->cone, eq) < 0)
3052 goto error;
3055 if (check) {
3056 int v = tab_has_valid_sample(cgbr->tab, eq, 1);
3057 if (v < 0)
3058 goto error;
3059 if (!v)
3060 check_gbr_integer_feasible(cgbr);
3062 if (update)
3063 cgbr->tab = check_samples(cgbr->tab, eq, 1);
3064 return;
3065 error:
3066 isl_tab_free(cgbr->tab);
3067 cgbr->tab = NULL;
3070 static void add_gbr_ineq(struct isl_context_gbr *cgbr, isl_int *ineq)
3072 if (!cgbr->tab)
3073 return;
3075 if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
3076 goto error;
3078 if (isl_tab_add_ineq(cgbr->tab, ineq) < 0)
3079 goto error;
3081 if (cgbr->shifted && !cgbr->shifted->empty && use_shifted(cgbr)) {
3082 int i;
3083 unsigned dim;
3084 dim = isl_basic_map_total_dim(cgbr->tab->bmap);
3086 if (isl_tab_extend_cons(cgbr->shifted, 1) < 0)
3087 goto error;
3089 for (i = 0; i < dim; ++i) {
3090 if (!isl_int_is_neg(ineq[1 + i]))
3091 continue;
3092 isl_int_add(ineq[0], ineq[0], ineq[1 + i]);
3095 if (isl_tab_add_ineq(cgbr->shifted, ineq) < 0)
3096 goto error;
3098 for (i = 0; i < dim; ++i) {
3099 if (!isl_int_is_neg(ineq[1 + i]))
3100 continue;
3101 isl_int_sub(ineq[0], ineq[0], ineq[1 + i]);
3105 if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
3106 if (isl_tab_extend_cons(cgbr->cone, 1) < 0)
3107 goto error;
3108 if (isl_tab_add_ineq(cgbr->cone, ineq) < 0)
3109 goto error;
3112 return;
3113 error:
3114 isl_tab_free(cgbr->tab);
3115 cgbr->tab = NULL;
3118 static void context_gbr_add_ineq(struct isl_context *context, isl_int *ineq,
3119 int check, int update)
3121 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3123 add_gbr_ineq(cgbr, ineq);
3124 if (!cgbr->tab)
3125 return;
3127 if (check) {
3128 int v = tab_has_valid_sample(cgbr->tab, ineq, 0);
3129 if (v < 0)
3130 goto error;
3131 if (!v)
3132 check_gbr_integer_feasible(cgbr);
3134 if (update)
3135 cgbr->tab = check_samples(cgbr->tab, ineq, 0);
3136 return;
3137 error:
3138 isl_tab_free(cgbr->tab);
3139 cgbr->tab = NULL;
3142 static isl_stat context_gbr_add_ineq_wrap(void *user, isl_int *ineq)
3144 struct isl_context *context = (struct isl_context *)user;
3145 context_gbr_add_ineq(context, ineq, 0, 0);
3146 return context->op->is_ok(context) ? isl_stat_ok : isl_stat_error;
3149 static enum isl_tab_row_sign context_gbr_ineq_sign(struct isl_context *context,
3150 isl_int *ineq, int strict)
3152 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3153 return tab_ineq_sign(cgbr->tab, ineq, strict);
3156 /* Check whether "ineq" can be added to the tableau without rendering
3157 * it infeasible.
3159 static int context_gbr_test_ineq(struct isl_context *context, isl_int *ineq)
3161 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3162 struct isl_tab_undo *snap;
3163 struct isl_tab_undo *shifted_snap = NULL;
3164 struct isl_tab_undo *cone_snap = NULL;
3165 int feasible;
3167 if (!cgbr->tab)
3168 return -1;
3170 if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
3171 return -1;
3173 snap = isl_tab_snap(cgbr->tab);
3174 if (cgbr->shifted)
3175 shifted_snap = isl_tab_snap(cgbr->shifted);
3176 if (cgbr->cone)
3177 cone_snap = isl_tab_snap(cgbr->cone);
3178 add_gbr_ineq(cgbr, ineq);
3179 check_gbr_integer_feasible(cgbr);
3180 if (!cgbr->tab)
3181 return -1;
3182 feasible = !cgbr->tab->empty;
3183 if (isl_tab_rollback(cgbr->tab, snap) < 0)
3184 return -1;
3185 if (shifted_snap) {
3186 if (isl_tab_rollback(cgbr->shifted, shifted_snap))
3187 return -1;
3188 } else if (cgbr->shifted) {
3189 isl_tab_free(cgbr->shifted);
3190 cgbr->shifted = NULL;
3192 if (cone_snap) {
3193 if (isl_tab_rollback(cgbr->cone, cone_snap))
3194 return -1;
3195 } else if (cgbr->cone) {
3196 isl_tab_free(cgbr->cone);
3197 cgbr->cone = NULL;
3200 return feasible;
3203 /* Return the column of the last of the variables associated to
3204 * a column that has a non-zero coefficient.
3205 * This function is called in a context where only coefficients
3206 * of parameters or divs can be non-zero.
3208 static int last_non_zero_var_col(struct isl_tab *tab, isl_int *p)
3210 int i;
3211 int col;
3213 if (tab->n_var == 0)
3214 return -1;
3216 for (i = tab->n_var - 1; i >= 0; --i) {
3217 if (i >= tab->n_param && i < tab->n_var - tab->n_div)
3218 continue;
3219 if (tab->var[i].is_row)
3220 continue;
3221 col = tab->var[i].index;
3222 if (!isl_int_is_zero(p[col]))
3223 return col;
3226 return -1;
3229 /* Look through all the recently added equalities in the context
3230 * to see if we can propagate any of them to the main tableau.
3232 * The newly added equalities in the context are encoded as pairs
3233 * of inequalities starting at inequality "first".
3235 * We tentatively add each of these equalities to the main tableau
3236 * and if this happens to result in a row with a final coefficient
3237 * that is one or negative one, we use it to kill a column
3238 * in the main tableau. Otherwise, we discard the tentatively
3239 * added row.
3240 * This tentative addition of equality constraints turns
3241 * on the undo facility of the tableau. Turn it off again
3242 * at the end, assuming it was turned off to begin with.
3244 * Return 0 on success and -1 on failure.
3246 static int propagate_equalities(struct isl_context_gbr *cgbr,
3247 struct isl_tab *tab, unsigned first)
3249 int i;
3250 struct isl_vec *eq = NULL;
3251 isl_bool needs_undo;
3253 needs_undo = isl_tab_need_undo(tab);
3254 if (needs_undo < 0)
3255 goto error;
3256 eq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
3257 if (!eq)
3258 goto error;
3260 if (isl_tab_extend_cons(tab, (cgbr->tab->bmap->n_ineq - first)/2) < 0)
3261 goto error;
3263 isl_seq_clr(eq->el + 1 + tab->n_param,
3264 tab->n_var - tab->n_param - tab->n_div);
3265 for (i = first; i < cgbr->tab->bmap->n_ineq; i += 2) {
3266 int j;
3267 int r;
3268 struct isl_tab_undo *snap;
3269 snap = isl_tab_snap(tab);
3271 isl_seq_cpy(eq->el, cgbr->tab->bmap->ineq[i], 1 + tab->n_param);
3272 isl_seq_cpy(eq->el + 1 + tab->n_var - tab->n_div,
3273 cgbr->tab->bmap->ineq[i] + 1 + tab->n_param,
3274 tab->n_div);
3276 r = isl_tab_add_row(tab, eq->el);
3277 if (r < 0)
3278 goto error;
3279 r = tab->con[r].index;
3280 j = last_non_zero_var_col(tab, tab->mat->row[r] + 2 + tab->M);
3281 if (j < 0 || j < tab->n_dead ||
3282 !isl_int_is_one(tab->mat->row[r][0]) ||
3283 (!isl_int_is_one(tab->mat->row[r][2 + tab->M + j]) &&
3284 !isl_int_is_negone(tab->mat->row[r][2 + tab->M + j]))) {
3285 if (isl_tab_rollback(tab, snap) < 0)
3286 goto error;
3287 continue;
3289 if (isl_tab_pivot(tab, r, j) < 0)
3290 goto error;
3291 if (isl_tab_kill_col(tab, j) < 0)
3292 goto error;
3294 if (restore_lexmin(tab) < 0)
3295 goto error;
3298 if (!needs_undo)
3299 isl_tab_clear_undo(tab);
3300 isl_vec_free(eq);
3302 return 0;
3303 error:
3304 isl_vec_free(eq);
3305 isl_tab_free(cgbr->tab);
3306 cgbr->tab = NULL;
3307 return -1;
3310 static int context_gbr_detect_equalities(struct isl_context *context,
3311 struct isl_tab *tab)
3313 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3314 unsigned n_ineq;
3316 if (!cgbr->cone) {
3317 struct isl_basic_set *bset = isl_tab_peek_bset(cgbr->tab);
3318 cgbr->cone = isl_tab_from_recession_cone(bset, 0);
3319 if (!cgbr->cone)
3320 goto error;
3321 if (isl_tab_track_bset(cgbr->cone,
3322 isl_basic_set_copy(bset)) < 0)
3323 goto error;
3325 if (isl_tab_detect_implicit_equalities(cgbr->cone) < 0)
3326 goto error;
3328 n_ineq = cgbr->tab->bmap->n_ineq;
3329 cgbr->tab = isl_tab_detect_equalities(cgbr->tab, cgbr->cone);
3330 if (!cgbr->tab)
3331 return -1;
3332 if (cgbr->tab->bmap->n_ineq > n_ineq &&
3333 propagate_equalities(cgbr, tab, n_ineq) < 0)
3334 return -1;
3336 return 0;
3337 error:
3338 isl_tab_free(cgbr->tab);
3339 cgbr->tab = NULL;
3340 return -1;
3343 static int context_gbr_get_div(struct isl_context *context, struct isl_tab *tab,
3344 struct isl_vec *div)
3346 return get_div(tab, context, div);
3349 static isl_bool context_gbr_insert_div(struct isl_context *context, int pos,
3350 __isl_keep isl_vec *div)
3352 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3353 if (cgbr->cone) {
3354 int r, n_div, o_div;
3356 n_div = isl_basic_map_dim(cgbr->cone->bmap, isl_dim_div);
3357 o_div = cgbr->cone->n_var - n_div;
3359 if (isl_tab_extend_cons(cgbr->cone, 3) < 0)
3360 return isl_bool_error;
3361 if (isl_tab_extend_vars(cgbr->cone, 1) < 0)
3362 return isl_bool_error;
3363 if ((r = isl_tab_insert_var(cgbr->cone, pos)) <0)
3364 return isl_bool_error;
3366 cgbr->cone->bmap = isl_basic_map_insert_div(cgbr->cone->bmap,
3367 r - o_div, div);
3368 if (!cgbr->cone->bmap)
3369 return isl_bool_error;
3370 if (isl_tab_push_var(cgbr->cone, isl_tab_undo_bmap_div,
3371 &cgbr->cone->var[r]) < 0)
3372 return isl_bool_error;
3374 return context_tab_insert_div(cgbr->tab, pos, div,
3375 context_gbr_add_ineq_wrap, context);
3378 static int context_gbr_best_split(struct isl_context *context,
3379 struct isl_tab *tab)
3381 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3382 struct isl_tab_undo *snap;
3383 int r;
3385 snap = isl_tab_snap(cgbr->tab);
3386 r = best_split(tab, cgbr->tab);
3388 if (r >= 0 && isl_tab_rollback(cgbr->tab, snap) < 0)
3389 return -1;
3391 return r;
3394 static int context_gbr_is_empty(struct isl_context *context)
3396 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3397 if (!cgbr->tab)
3398 return -1;
3399 return cgbr->tab->empty;
3402 struct isl_gbr_tab_undo {
3403 struct isl_tab_undo *tab_snap;
3404 struct isl_tab_undo *shifted_snap;
3405 struct isl_tab_undo *cone_snap;
3408 static void *context_gbr_save(struct isl_context *context)
3410 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3411 struct isl_gbr_tab_undo *snap;
3413 if (!cgbr->tab)
3414 return NULL;
3416 snap = isl_alloc_type(cgbr->tab->mat->ctx, struct isl_gbr_tab_undo);
3417 if (!snap)
3418 return NULL;
3420 snap->tab_snap = isl_tab_snap(cgbr->tab);
3421 if (isl_tab_save_samples(cgbr->tab) < 0)
3422 goto error;
3424 if (cgbr->shifted)
3425 snap->shifted_snap = isl_tab_snap(cgbr->shifted);
3426 else
3427 snap->shifted_snap = NULL;
3429 if (cgbr->cone)
3430 snap->cone_snap = isl_tab_snap(cgbr->cone);
3431 else
3432 snap->cone_snap = NULL;
3434 return snap;
3435 error:
3436 free(snap);
3437 return NULL;
3440 static void context_gbr_restore(struct isl_context *context, void *save)
3442 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3443 struct isl_gbr_tab_undo *snap = (struct isl_gbr_tab_undo *)save;
3444 if (!snap)
3445 goto error;
3446 if (isl_tab_rollback(cgbr->tab, snap->tab_snap) < 0)
3447 goto error;
3449 if (snap->shifted_snap) {
3450 if (isl_tab_rollback(cgbr->shifted, snap->shifted_snap) < 0)
3451 goto error;
3452 } else if (cgbr->shifted) {
3453 isl_tab_free(cgbr->shifted);
3454 cgbr->shifted = NULL;
3457 if (snap->cone_snap) {
3458 if (isl_tab_rollback(cgbr->cone, snap->cone_snap) < 0)
3459 goto error;
3460 } else if (cgbr->cone) {
3461 isl_tab_free(cgbr->cone);
3462 cgbr->cone = NULL;
3465 free(snap);
3467 return;
3468 error:
3469 free(snap);
3470 isl_tab_free(cgbr->tab);
3471 cgbr->tab = NULL;
3474 static void context_gbr_discard(void *save)
3476 struct isl_gbr_tab_undo *snap = (struct isl_gbr_tab_undo *)save;
3477 free(snap);
3480 static int context_gbr_is_ok(struct isl_context *context)
3482 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3483 return !!cgbr->tab;
3486 static void context_gbr_invalidate(struct isl_context *context)
3488 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3489 isl_tab_free(cgbr->tab);
3490 cgbr->tab = NULL;
3493 static __isl_null struct isl_context *context_gbr_free(
3494 struct isl_context *context)
3496 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3497 isl_tab_free(cgbr->tab);
3498 isl_tab_free(cgbr->shifted);
3499 isl_tab_free(cgbr->cone);
3500 free(cgbr);
3502 return NULL;
3505 struct isl_context_op isl_context_gbr_op = {
3506 context_gbr_detect_nonnegative_parameters,
3507 context_gbr_peek_basic_set,
3508 context_gbr_peek_tab,
3509 context_gbr_add_eq,
3510 context_gbr_add_ineq,
3511 context_gbr_ineq_sign,
3512 context_gbr_test_ineq,
3513 context_gbr_get_div,
3514 context_gbr_insert_div,
3515 context_gbr_detect_equalities,
3516 context_gbr_best_split,
3517 context_gbr_is_empty,
3518 context_gbr_is_ok,
3519 context_gbr_save,
3520 context_gbr_restore,
3521 context_gbr_discard,
3522 context_gbr_invalidate,
3523 context_gbr_free,
3526 static struct isl_context *isl_context_gbr_alloc(__isl_keep isl_basic_set *dom)
3528 struct isl_context_gbr *cgbr;
3530 if (!dom)
3531 return NULL;
3533 cgbr = isl_calloc_type(dom->ctx, struct isl_context_gbr);
3534 if (!cgbr)
3535 return NULL;
3537 cgbr->context.op = &isl_context_gbr_op;
3539 cgbr->shifted = NULL;
3540 cgbr->cone = NULL;
3541 cgbr->tab = isl_tab_from_basic_set(dom, 1);
3542 cgbr->tab = isl_tab_init_samples(cgbr->tab);
3543 if (!cgbr->tab)
3544 goto error;
3545 check_gbr_integer_feasible(cgbr);
3547 return &cgbr->context;
3548 error:
3549 cgbr->context.op->free(&cgbr->context);
3550 return NULL;
3553 /* Allocate a context corresponding to "dom".
3554 * The representation specific fields are initialized by
3555 * isl_context_lex_alloc or isl_context_gbr_alloc.
3556 * The shared "n_unknown" field is initialized to the number
3557 * of final unknown integer divisions in "dom".
3559 static struct isl_context *isl_context_alloc(__isl_keep isl_basic_set *dom)
3561 struct isl_context *context;
3562 int first;
3564 if (!dom)
3565 return NULL;
3567 if (dom->ctx->opt->context == ISL_CONTEXT_LEXMIN)
3568 context = isl_context_lex_alloc(dom);
3569 else
3570 context = isl_context_gbr_alloc(dom);
3572 if (!context)
3573 return NULL;
3575 first = isl_basic_set_first_unknown_div(dom);
3576 if (first < 0)
3577 return context->op->free(context);
3578 context->n_unknown = isl_basic_set_dim(dom, isl_dim_div) - first;
3580 return context;
3583 /* Initialize some common fields of "sol", which keeps track
3584 * of the solution of an optimization problem on "bmap" over
3585 * the domain "dom".
3586 * If "max" is set, then a maximization problem is being solved, rather than
3587 * a minimization problem, which means that the variables in the
3588 * tableau have value "M - x" rather than "M + x".
3590 static isl_stat sol_init(struct isl_sol *sol, __isl_keep isl_basic_map *bmap,
3591 __isl_keep isl_basic_set *dom, int max)
3593 sol->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
3594 sol->dec_level.callback.run = &sol_dec_level_wrap;
3595 sol->dec_level.sol = sol;
3596 sol->max = max;
3597 sol->n_out = isl_basic_map_dim(bmap, isl_dim_out);
3598 sol->space = isl_basic_map_get_space(bmap);
3600 sol->context = isl_context_alloc(dom);
3601 if (!sol->space || !sol->context)
3602 return isl_stat_error;
3604 return isl_stat_ok;
3607 /* Construct an isl_sol_map structure for accumulating the solution.
3608 * If track_empty is set, then we also keep track of the parts
3609 * of the context where there is no solution.
3610 * If max is set, then we are solving a maximization, rather than
3611 * a minimization problem, which means that the variables in the
3612 * tableau have value "M - x" rather than "M + x".
3614 static struct isl_sol *sol_map_init(__isl_keep isl_basic_map *bmap,
3615 __isl_take isl_basic_set *dom, int track_empty, int max)
3617 struct isl_sol_map *sol_map = NULL;
3618 isl_space *space;
3620 if (!bmap)
3621 goto error;
3623 sol_map = isl_calloc_type(bmap->ctx, struct isl_sol_map);
3624 if (!sol_map)
3625 goto error;
3627 sol_map->sol.free = &sol_map_free;
3628 if (sol_init(&sol_map->sol, bmap, dom, max) < 0)
3629 goto error;
3630 sol_map->sol.add = &sol_map_add_wrap;
3631 sol_map->sol.add_empty = track_empty ? &sol_map_add_empty_wrap : NULL;
3632 space = isl_space_copy(sol_map->sol.space);
3633 sol_map->map = isl_map_alloc_space(space, 1, ISL_MAP_DISJOINT);
3634 if (!sol_map->map)
3635 goto error;
3637 if (track_empty) {
3638 sol_map->empty = isl_set_alloc_space(isl_basic_set_get_space(dom),
3639 1, ISL_SET_DISJOINT);
3640 if (!sol_map->empty)
3641 goto error;
3644 isl_basic_set_free(dom);
3645 return &sol_map->sol;
3646 error:
3647 isl_basic_set_free(dom);
3648 sol_free(&sol_map->sol);
3649 return NULL;
3652 /* Check whether all coefficients of (non-parameter) variables
3653 * are non-positive, meaning that no pivots can be performed on the row.
3655 static int is_critical(struct isl_tab *tab, int row)
3657 int j;
3658 unsigned off = 2 + tab->M;
3660 for (j = tab->n_dead; j < tab->n_col; ++j) {
3661 if (tab->col_var[j] >= 0 &&
3662 (tab->col_var[j] < tab->n_param ||
3663 tab->col_var[j] >= tab->n_var - tab->n_div))
3664 continue;
3666 if (isl_int_is_pos(tab->mat->row[row][off + j]))
3667 return 0;
3670 return 1;
3673 /* Check whether the inequality represented by vec is strict over the integers,
3674 * i.e., there are no integer values satisfying the constraint with
3675 * equality. This happens if the gcd of the coefficients is not a divisor
3676 * of the constant term. If so, scale the constraint down by the gcd
3677 * of the coefficients.
3679 static int is_strict(struct isl_vec *vec)
3681 isl_int gcd;
3682 int strict = 0;
3684 isl_int_init(gcd);
3685 isl_seq_gcd(vec->el + 1, vec->size - 1, &gcd);
3686 if (!isl_int_is_one(gcd)) {
3687 strict = !isl_int_is_divisible_by(vec->el[0], gcd);
3688 isl_int_fdiv_q(vec->el[0], vec->el[0], gcd);
3689 isl_seq_scale_down(vec->el + 1, vec->el + 1, gcd, vec->size-1);
3691 isl_int_clear(gcd);
3693 return strict;
3696 /* Determine the sign of the given row of the main tableau.
3697 * The result is one of
3698 * isl_tab_row_pos: always non-negative; no pivot needed
3699 * isl_tab_row_neg: always non-positive; pivot
3700 * isl_tab_row_any: can be both positive and negative; split
3702 * We first handle some simple cases
3703 * - the row sign may be known already
3704 * - the row may be obviously non-negative
3705 * - the parametric constant may be equal to that of another row
3706 * for which we know the sign. This sign will be either "pos" or
3707 * "any". If it had been "neg" then we would have pivoted before.
3709 * If none of these cases hold, we check the value of the row for each
3710 * of the currently active samples. Based on the signs of these values
3711 * we make an initial determination of the sign of the row.
3713 * all zero -> unk(nown)
3714 * all non-negative -> pos
3715 * all non-positive -> neg
3716 * both negative and positive -> all
3718 * If we end up with "all", we are done.
3719 * Otherwise, we perform a check for positive and/or negative
3720 * values as follows.
3722 * samples neg unk pos
3723 * <0 ? Y N Y N
3724 * pos any pos
3725 * >0 ? Y N Y N
3726 * any neg any neg
3728 * There is no special sign for "zero", because we can usually treat zero
3729 * as either non-negative or non-positive, whatever works out best.
3730 * However, if the row is "critical", meaning that pivoting is impossible
3731 * then we don't want to limp zero with the non-positive case, because
3732 * then we we would lose the solution for those values of the parameters
3733 * where the value of the row is zero. Instead, we treat 0 as non-negative
3734 * ensuring a split if the row can attain both zero and negative values.
3735 * The same happens when the original constraint was one that could not
3736 * be satisfied with equality by any integer values of the parameters.
3737 * In this case, we normalize the constraint, but then a value of zero
3738 * for the normalized constraint is actually a positive value for the
3739 * original constraint, so again we need to treat zero as non-negative.
3740 * In both these cases, we have the following decision tree instead:
3742 * all non-negative -> pos
3743 * all negative -> neg
3744 * both negative and non-negative -> all
3746 * samples neg pos
3747 * <0 ? Y N
3748 * any pos
3749 * >=0 ? Y N
3750 * any neg
3752 static enum isl_tab_row_sign row_sign(struct isl_tab *tab,
3753 struct isl_sol *sol, int row)
3755 struct isl_vec *ineq = NULL;
3756 enum isl_tab_row_sign res = isl_tab_row_unknown;
3757 int critical;
3758 int strict;
3759 int row2;
3761 if (tab->row_sign[row] != isl_tab_row_unknown)
3762 return tab->row_sign[row];
3763 if (is_obviously_nonneg(tab, row))
3764 return isl_tab_row_pos;
3765 for (row2 = tab->n_redundant; row2 < tab->n_row; ++row2) {
3766 if (tab->row_sign[row2] == isl_tab_row_unknown)
3767 continue;
3768 if (identical_parameter_line(tab, row, row2))
3769 return tab->row_sign[row2];
3772 critical = is_critical(tab, row);
3774 ineq = get_row_parameter_ineq(tab, row);
3775 if (!ineq)
3776 goto error;
3778 strict = is_strict(ineq);
3780 res = sol->context->op->ineq_sign(sol->context, ineq->el,
3781 critical || strict);
3783 if (res == isl_tab_row_unknown || res == isl_tab_row_pos) {
3784 /* test for negative values */
3785 int feasible;
3786 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3787 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3789 feasible = sol->context->op->test_ineq(sol->context, ineq->el);
3790 if (feasible < 0)
3791 goto error;
3792 if (!feasible)
3793 res = isl_tab_row_pos;
3794 else
3795 res = (res == isl_tab_row_unknown) ? isl_tab_row_neg
3796 : isl_tab_row_any;
3797 if (res == isl_tab_row_neg) {
3798 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3799 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3803 if (res == isl_tab_row_neg) {
3804 /* test for positive values */
3805 int feasible;
3806 if (!critical && !strict)
3807 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3809 feasible = sol->context->op->test_ineq(sol->context, ineq->el);
3810 if (feasible < 0)
3811 goto error;
3812 if (feasible)
3813 res = isl_tab_row_any;
3816 isl_vec_free(ineq);
3817 return res;
3818 error:
3819 isl_vec_free(ineq);
3820 return isl_tab_row_unknown;
3823 static void find_solutions(struct isl_sol *sol, struct isl_tab *tab);
3825 /* Find solutions for values of the parameters that satisfy the given
3826 * inequality.
3828 * We currently take a snapshot of the context tableau that is reset
3829 * when we return from this function, while we make a copy of the main
3830 * tableau, leaving the original main tableau untouched.
3831 * These are fairly arbitrary choices. Making a copy also of the context
3832 * tableau would obviate the need to undo any changes made to it later,
3833 * while taking a snapshot of the main tableau could reduce memory usage.
3834 * If we were to switch to taking a snapshot of the main tableau,
3835 * we would have to keep in mind that we need to save the row signs
3836 * and that we need to do this before saving the current basis
3837 * such that the basis has been restore before we restore the row signs.
3839 static void find_in_pos(struct isl_sol *sol, struct isl_tab *tab, isl_int *ineq)
3841 void *saved;
3843 if (!sol->context)
3844 goto error;
3845 saved = sol->context->op->save(sol->context);
3847 tab = isl_tab_dup(tab);
3848 if (!tab)
3849 goto error;
3851 sol->context->op->add_ineq(sol->context, ineq, 0, 1);
3853 find_solutions(sol, tab);
3855 if (!sol->error)
3856 sol->context->op->restore(sol->context, saved);
3857 else
3858 sol->context->op->discard(saved);
3859 return;
3860 error:
3861 sol->error = 1;
3864 /* Record the absence of solutions for those values of the parameters
3865 * that do not satisfy the given inequality with equality.
3867 static void no_sol_in_strict(struct isl_sol *sol,
3868 struct isl_tab *tab, struct isl_vec *ineq)
3870 int empty;
3871 void *saved;
3873 if (!sol->context || sol->error)
3874 goto error;
3875 saved = sol->context->op->save(sol->context);
3877 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3879 sol->context->op->add_ineq(sol->context, ineq->el, 1, 0);
3880 if (!sol->context)
3881 goto error;
3883 empty = tab->empty;
3884 tab->empty = 1;
3885 sol_add(sol, tab);
3886 tab->empty = empty;
3888 isl_int_add_ui(ineq->el[0], ineq->el[0], 1);
3890 sol->context->op->restore(sol->context, saved);
3891 return;
3892 error:
3893 sol->error = 1;
3896 /* Reset all row variables that are marked to have a sign that may
3897 * be both positive and negative to have an unknown sign.
3899 static void reset_any_to_unknown(struct isl_tab *tab)
3901 int row;
3903 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3904 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
3905 continue;
3906 if (tab->row_sign[row] == isl_tab_row_any)
3907 tab->row_sign[row] = isl_tab_row_unknown;
3911 /* Compute the lexicographic minimum of the set represented by the main
3912 * tableau "tab" within the context "sol->context_tab".
3913 * On entry the sample value of the main tableau is lexicographically
3914 * less than or equal to this lexicographic minimum.
3915 * Pivots are performed until a feasible point is found, which is then
3916 * necessarily equal to the minimum, or until the tableau is found to
3917 * be infeasible. Some pivots may need to be performed for only some
3918 * feasible values of the context tableau. If so, the context tableau
3919 * is split into a part where the pivot is needed and a part where it is not.
3921 * Whenever we enter the main loop, the main tableau is such that no
3922 * "obvious" pivots need to be performed on it, where "obvious" means
3923 * that the given row can be seen to be negative without looking at
3924 * the context tableau. In particular, for non-parametric problems,
3925 * no pivots need to be performed on the main tableau.
3926 * The caller of find_solutions is responsible for making this property
3927 * hold prior to the first iteration of the loop, while restore_lexmin
3928 * is called before every other iteration.
3930 * Inside the main loop, we first examine the signs of the rows of
3931 * the main tableau within the context of the context tableau.
3932 * If we find a row that is always non-positive for all values of
3933 * the parameters satisfying the context tableau and negative for at
3934 * least one value of the parameters, we perform the appropriate pivot
3935 * and start over. An exception is the case where no pivot can be
3936 * performed on the row. In this case, we require that the sign of
3937 * the row is negative for all values of the parameters (rather than just
3938 * non-positive). This special case is handled inside row_sign, which
3939 * will say that the row can have any sign if it determines that it can
3940 * attain both negative and zero values.
3942 * If we can't find a row that always requires a pivot, but we can find
3943 * one or more rows that require a pivot for some values of the parameters
3944 * (i.e., the row can attain both positive and negative signs), then we split
3945 * the context tableau into two parts, one where we force the sign to be
3946 * non-negative and one where we force is to be negative.
3947 * The non-negative part is handled by a recursive call (through find_in_pos).
3948 * Upon returning from this call, we continue with the negative part and
3949 * perform the required pivot.
3951 * If no such rows can be found, all rows are non-negative and we have
3952 * found a (rational) feasible point. If we only wanted a rational point
3953 * then we are done.
3954 * Otherwise, we check if all values of the sample point of the tableau
3955 * are integral for the variables. If so, we have found the minimal
3956 * integral point and we are done.
3957 * If the sample point is not integral, then we need to make a distinction
3958 * based on whether the constant term is non-integral or the coefficients
3959 * of the parameters. Furthermore, in order to decide how to handle
3960 * the non-integrality, we also need to know whether the coefficients
3961 * of the other columns in the tableau are integral. This leads
3962 * to the following table. The first two rows do not correspond
3963 * to a non-integral sample point and are only mentioned for completeness.
3965 * constant parameters other
3967 * int int int |
3968 * int int rat | -> no problem
3970 * rat int int -> fail
3972 * rat int rat -> cut
3974 * int rat rat |
3975 * rat rat rat | -> parametric cut
3977 * int rat int |
3978 * rat rat int | -> split context
3980 * If the parametric constant is completely integral, then there is nothing
3981 * to be done. If the constant term is non-integral, but all the other
3982 * coefficient are integral, then there is nothing that can be done
3983 * and the tableau has no integral solution.
3984 * If, on the other hand, one or more of the other columns have rational
3985 * coefficients, but the parameter coefficients are all integral, then
3986 * we can perform a regular (non-parametric) cut.
3987 * Finally, if there is any parameter coefficient that is non-integral,
3988 * then we need to involve the context tableau. There are two cases here.
3989 * If at least one other column has a rational coefficient, then we
3990 * can perform a parametric cut in the main tableau by adding a new
3991 * integer division in the context tableau.
3992 * If all other columns have integral coefficients, then we need to
3993 * enforce that the rational combination of parameters (c + \sum a_i y_i)/m
3994 * is always integral. We do this by introducing an integer division
3995 * q = floor((c + \sum a_i y_i)/m) and stipulating that its argument should
3996 * always be integral in the context tableau, i.e., m q = c + \sum a_i y_i.
3997 * Since q is expressed in the tableau as
3998 * c + \sum a_i y_i - m q >= 0
3999 * -c - \sum a_i y_i + m q + m - 1 >= 0
4000 * it is sufficient to add the inequality
4001 * -c - \sum a_i y_i + m q >= 0
4002 * In the part of the context where this inequality does not hold, the
4003 * main tableau is marked as being empty.
4005 static void find_solutions(struct isl_sol *sol, struct isl_tab *tab)
4007 struct isl_context *context;
4008 int r;
4010 if (!tab || sol->error)
4011 goto error;
4013 context = sol->context;
4015 if (tab->empty)
4016 goto done;
4017 if (context->op->is_empty(context))
4018 goto done;
4020 for (r = 0; r >= 0 && tab && !tab->empty; r = restore_lexmin(tab)) {
4021 int flags;
4022 int row;
4023 enum isl_tab_row_sign sgn;
4024 int split = -1;
4025 int n_split = 0;
4027 for (row = tab->n_redundant; row < tab->n_row; ++row) {
4028 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
4029 continue;
4030 sgn = row_sign(tab, sol, row);
4031 if (!sgn)
4032 goto error;
4033 tab->row_sign[row] = sgn;
4034 if (sgn == isl_tab_row_any)
4035 n_split++;
4036 if (sgn == isl_tab_row_any && split == -1)
4037 split = row;
4038 if (sgn == isl_tab_row_neg)
4039 break;
4041 if (row < tab->n_row)
4042 continue;
4043 if (split != -1) {
4044 struct isl_vec *ineq;
4045 if (n_split != 1)
4046 split = context->op->best_split(context, tab);
4047 if (split < 0)
4048 goto error;
4049 ineq = get_row_parameter_ineq(tab, split);
4050 if (!ineq)
4051 goto error;
4052 is_strict(ineq);
4053 reset_any_to_unknown(tab);
4054 tab->row_sign[split] = isl_tab_row_pos;
4055 sol_inc_level(sol);
4056 find_in_pos(sol, tab, ineq->el);
4057 tab->row_sign[split] = isl_tab_row_neg;
4058 isl_seq_neg(ineq->el, ineq->el, ineq->size);
4059 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
4060 if (!sol->error)
4061 context->op->add_ineq(context, ineq->el, 0, 1);
4062 isl_vec_free(ineq);
4063 if (sol->error)
4064 goto error;
4065 continue;
4067 if (tab->rational)
4068 break;
4069 row = first_non_integer_row(tab, &flags);
4070 if (row < 0)
4071 break;
4072 if (ISL_FL_ISSET(flags, I_PAR)) {
4073 if (ISL_FL_ISSET(flags, I_VAR)) {
4074 if (isl_tab_mark_empty(tab) < 0)
4075 goto error;
4076 break;
4078 row = add_cut(tab, row);
4079 } else if (ISL_FL_ISSET(flags, I_VAR)) {
4080 struct isl_vec *div;
4081 struct isl_vec *ineq;
4082 int d;
4083 div = get_row_split_div(tab, row);
4084 if (!div)
4085 goto error;
4086 d = context->op->get_div(context, tab, div);
4087 isl_vec_free(div);
4088 if (d < 0)
4089 goto error;
4090 ineq = ineq_for_div(context->op->peek_basic_set(context), d);
4091 if (!ineq)
4092 goto error;
4093 sol_inc_level(sol);
4094 no_sol_in_strict(sol, tab, ineq);
4095 isl_seq_neg(ineq->el, ineq->el, ineq->size);
4096 context->op->add_ineq(context, ineq->el, 1, 1);
4097 isl_vec_free(ineq);
4098 if (sol->error || !context->op->is_ok(context))
4099 goto error;
4100 tab = set_row_cst_to_div(tab, row, d);
4101 if (context->op->is_empty(context))
4102 break;
4103 } else
4104 row = add_parametric_cut(tab, row, context);
4105 if (row < 0)
4106 goto error;
4108 if (r < 0)
4109 goto error;
4110 done:
4111 sol_add(sol, tab);
4112 isl_tab_free(tab);
4113 return;
4114 error:
4115 isl_tab_free(tab);
4116 sol->error = 1;
4119 /* Does "sol" contain a pair of partial solutions that could potentially
4120 * be merged?
4122 * We currently only check that "sol" is not in an error state
4123 * and that there are at least two partial solutions of which the final two
4124 * are defined at the same level.
4126 static int sol_has_mergeable_solutions(struct isl_sol *sol)
4128 if (sol->error)
4129 return 0;
4130 if (!sol->partial)
4131 return 0;
4132 if (!sol->partial->next)
4133 return 0;
4134 return sol->partial->level == sol->partial->next->level;
4137 /* Compute the lexicographic minimum of the set represented by the main
4138 * tableau "tab" within the context "sol->context_tab".
4140 * As a preprocessing step, we first transfer all the purely parametric
4141 * equalities from the main tableau to the context tableau, i.e.,
4142 * parameters that have been pivoted to a row.
4143 * These equalities are ignored by the main algorithm, because the
4144 * corresponding rows may not be marked as being non-negative.
4145 * In parts of the context where the added equality does not hold,
4146 * the main tableau is marked as being empty.
4148 * Before we embark on the actual computation, we save a copy
4149 * of the context. When we return, we check if there are any
4150 * partial solutions that can potentially be merged. If so,
4151 * we perform a rollback to the initial state of the context.
4152 * The merging of partial solutions happens inside calls to
4153 * sol_dec_level that are pushed onto the undo stack of the context.
4154 * If there are no partial solutions that can potentially be merged
4155 * then the rollback is skipped as it would just be wasted effort.
4157 static void find_solutions_main(struct isl_sol *sol, struct isl_tab *tab)
4159 int row;
4160 void *saved;
4162 if (!tab)
4163 goto error;
4165 sol->level = 0;
4167 for (row = tab->n_redundant; row < tab->n_row; ++row) {
4168 int p;
4169 struct isl_vec *eq;
4171 if (tab->row_var[row] < 0)
4172 continue;
4173 if (tab->row_var[row] >= tab->n_param &&
4174 tab->row_var[row] < tab->n_var - tab->n_div)
4175 continue;
4176 if (tab->row_var[row] < tab->n_param)
4177 p = tab->row_var[row];
4178 else
4179 p = tab->row_var[row]
4180 + tab->n_param - (tab->n_var - tab->n_div);
4182 eq = isl_vec_alloc(tab->mat->ctx, 1+tab->n_param+tab->n_div);
4183 if (!eq)
4184 goto error;
4185 get_row_parameter_line(tab, row, eq->el);
4186 isl_int_neg(eq->el[1 + p], tab->mat->row[row][0]);
4187 eq = isl_vec_normalize(eq);
4189 sol_inc_level(sol);
4190 no_sol_in_strict(sol, tab, eq);
4192 isl_seq_neg(eq->el, eq->el, eq->size);
4193 sol_inc_level(sol);
4194 no_sol_in_strict(sol, tab, eq);
4195 isl_seq_neg(eq->el, eq->el, eq->size);
4197 sol->context->op->add_eq(sol->context, eq->el, 1, 1);
4199 isl_vec_free(eq);
4201 if (isl_tab_mark_redundant(tab, row) < 0)
4202 goto error;
4204 if (sol->context->op->is_empty(sol->context))
4205 break;
4207 row = tab->n_redundant - 1;
4210 saved = sol->context->op->save(sol->context);
4212 find_solutions(sol, tab);
4214 if (sol_has_mergeable_solutions(sol))
4215 sol->context->op->restore(sol->context, saved);
4216 else
4217 sol->context->op->discard(saved);
4219 sol->level = 0;
4220 sol_pop(sol);
4222 return;
4223 error:
4224 isl_tab_free(tab);
4225 sol->error = 1;
4228 /* Check if integer division "div" of "dom" also occurs in "bmap".
4229 * If so, return its position within the divs.
4230 * If not, return -1.
4232 static int find_context_div(struct isl_basic_map *bmap,
4233 struct isl_basic_set *dom, unsigned div)
4235 int i;
4236 unsigned b_dim = isl_space_dim(bmap->dim, isl_dim_all);
4237 unsigned d_dim = isl_space_dim(dom->dim, isl_dim_all);
4239 if (isl_int_is_zero(dom->div[div][0]))
4240 return -1;
4241 if (isl_seq_first_non_zero(dom->div[div] + 2 + d_dim, dom->n_div) != -1)
4242 return -1;
4244 for (i = 0; i < bmap->n_div; ++i) {
4245 if (isl_int_is_zero(bmap->div[i][0]))
4246 continue;
4247 if (isl_seq_first_non_zero(bmap->div[i] + 2 + d_dim,
4248 (b_dim - d_dim) + bmap->n_div) != -1)
4249 continue;
4250 if (isl_seq_eq(bmap->div[i], dom->div[div], 2 + d_dim))
4251 return i;
4253 return -1;
4256 /* The correspondence between the variables in the main tableau,
4257 * the context tableau, and the input map and domain is as follows.
4258 * The first n_param and the last n_div variables of the main tableau
4259 * form the variables of the context tableau.
4260 * In the basic map, these n_param variables correspond to the
4261 * parameters and the input dimensions. In the domain, they correspond
4262 * to the parameters and the set dimensions.
4263 * The n_div variables correspond to the integer divisions in the domain.
4264 * To ensure that everything lines up, we may need to copy some of the
4265 * integer divisions of the domain to the map. These have to be placed
4266 * in the same order as those in the context and they have to be placed
4267 * after any other integer divisions that the map may have.
4268 * This function performs the required reordering.
4270 static struct isl_basic_map *align_context_divs(struct isl_basic_map *bmap,
4271 struct isl_basic_set *dom)
4273 int i;
4274 int common = 0;
4275 int other;
4277 for (i = 0; i < dom->n_div; ++i)
4278 if (find_context_div(bmap, dom, i) != -1)
4279 common++;
4280 other = bmap->n_div - common;
4281 if (dom->n_div - common > 0) {
4282 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
4283 dom->n_div - common, 0, 0);
4284 if (!bmap)
4285 return NULL;
4287 for (i = 0; i < dom->n_div; ++i) {
4288 int pos = find_context_div(bmap, dom, i);
4289 if (pos < 0) {
4290 pos = isl_basic_map_alloc_div(bmap);
4291 if (pos < 0)
4292 goto error;
4293 isl_int_set_si(bmap->div[pos][0], 0);
4295 if (pos != other + i)
4296 isl_basic_map_swap_div(bmap, pos, other + i);
4298 return bmap;
4299 error:
4300 isl_basic_map_free(bmap);
4301 return NULL;
4304 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4305 * some obvious symmetries.
4307 * We make sure the divs in the domain are properly ordered,
4308 * because they will be added one by one in the given order
4309 * during the construction of the solution map.
4310 * Furthermore, make sure that the known integer divisions
4311 * appear before any unknown integer division because the solution
4312 * may depend on the known integer divisions, while anything that
4313 * depends on any variable starting from the first unknown integer
4314 * division is ignored in sol_pma_add.
4316 static struct isl_sol *basic_map_partial_lexopt_base_sol(
4317 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4318 __isl_give isl_set **empty, int max,
4319 struct isl_sol *(*init)(__isl_keep isl_basic_map *bmap,
4320 __isl_take isl_basic_set *dom, int track_empty, int max))
4322 struct isl_tab *tab;
4323 struct isl_sol *sol = NULL;
4324 struct isl_context *context;
4326 if (dom->n_div) {
4327 dom = isl_basic_set_sort_divs(dom);
4328 bmap = align_context_divs(bmap, dom);
4330 sol = init(bmap, dom, !!empty, max);
4331 if (!sol)
4332 goto error;
4334 context = sol->context;
4335 if (isl_basic_set_plain_is_empty(context->op->peek_basic_set(context)))
4336 /* nothing */;
4337 else if (isl_basic_map_plain_is_empty(bmap)) {
4338 if (sol->add_empty)
4339 sol->add_empty(sol,
4340 isl_basic_set_copy(context->op->peek_basic_set(context)));
4341 } else {
4342 tab = tab_for_lexmin(bmap,
4343 context->op->peek_basic_set(context), 1, max);
4344 tab = context->op->detect_nonnegative_parameters(context, tab);
4345 find_solutions_main(sol, tab);
4347 if (sol->error)
4348 goto error;
4350 isl_basic_map_free(bmap);
4351 return sol;
4352 error:
4353 sol_free(sol);
4354 isl_basic_map_free(bmap);
4355 return NULL;
4358 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4359 * some obvious symmetries.
4361 * We call basic_map_partial_lexopt_base_sol and extract the results.
4363 static __isl_give isl_map *basic_map_partial_lexopt_base(
4364 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4365 __isl_give isl_set **empty, int max)
4367 isl_map *result = NULL;
4368 struct isl_sol *sol;
4369 struct isl_sol_map *sol_map;
4371 sol = basic_map_partial_lexopt_base_sol(bmap, dom, empty, max,
4372 &sol_map_init);
4373 if (!sol)
4374 return NULL;
4375 sol_map = (struct isl_sol_map *) sol;
4377 result = isl_map_copy(sol_map->map);
4378 if (empty)
4379 *empty = isl_set_copy(sol_map->empty);
4380 sol_free(&sol_map->sol);
4381 return result;
4384 /* Return a count of the number of occurrences of the "n" first
4385 * variables in the inequality constraints of "bmap".
4387 static __isl_give int *count_occurrences(__isl_keep isl_basic_map *bmap,
4388 int n)
4390 int i, j;
4391 isl_ctx *ctx;
4392 int *occurrences;
4394 if (!bmap)
4395 return NULL;
4396 ctx = isl_basic_map_get_ctx(bmap);
4397 occurrences = isl_calloc_array(ctx, int, n);
4398 if (!occurrences)
4399 return NULL;
4401 for (i = 0; i < bmap->n_ineq; ++i) {
4402 for (j = 0; j < n; ++j) {
4403 if (!isl_int_is_zero(bmap->ineq[i][1 + j]))
4404 occurrences[j]++;
4408 return occurrences;
4411 /* Do all of the "n" variables with non-zero coefficients in "c"
4412 * occur in exactly a single constraint.
4413 * "occurrences" is an array of length "n" containing the number
4414 * of occurrences of each of the variables in the inequality constraints.
4416 static int single_occurrence(int n, isl_int *c, int *occurrences)
4418 int i;
4420 for (i = 0; i < n; ++i) {
4421 if (isl_int_is_zero(c[i]))
4422 continue;
4423 if (occurrences[i] != 1)
4424 return 0;
4427 return 1;
4430 /* Do all of the "n" initial variables that occur in inequality constraint
4431 * "ineq" of "bmap" only occur in that constraint?
4433 static int all_single_occurrence(__isl_keep isl_basic_map *bmap, int ineq,
4434 int n)
4436 int i, j;
4438 for (i = 0; i < n; ++i) {
4439 if (isl_int_is_zero(bmap->ineq[ineq][1 + i]))
4440 continue;
4441 for (j = 0; j < bmap->n_ineq; ++j) {
4442 if (j == ineq)
4443 continue;
4444 if (!isl_int_is_zero(bmap->ineq[j][1 + i]))
4445 return 0;
4449 return 1;
4452 /* Structure used during detection of parallel constraints.
4453 * n_in: number of "input" variables: isl_dim_param + isl_dim_in
4454 * n_out: number of "output" variables: isl_dim_out + isl_dim_div
4455 * val: the coefficients of the output variables
4457 struct isl_constraint_equal_info {
4458 isl_basic_map *bmap;
4459 unsigned n_in;
4460 unsigned n_out;
4461 isl_int *val;
4464 /* Check whether the coefficients of the output variables
4465 * of the constraint in "entry" are equal to info->val.
4467 static int constraint_equal(const void *entry, const void *val)
4469 isl_int **row = (isl_int **)entry;
4470 const struct isl_constraint_equal_info *info = val;
4472 return isl_seq_eq((*row) + 1 + info->n_in, info->val, info->n_out);
4475 /* Check whether "bmap" has a pair of constraints that have
4476 * the same coefficients for the output variables.
4477 * Note that the coefficients of the existentially quantified
4478 * variables need to be zero since the existentially quantified
4479 * of the result are usually not the same as those of the input.
4480 * Furthermore, check that each of the input variables that occur
4481 * in those constraints does not occur in any other constraint.
4482 * If so, return true and return the row indices of the two constraints
4483 * in *first and *second.
4485 static isl_bool parallel_constraints(__isl_keep isl_basic_map *bmap,
4486 int *first, int *second)
4488 int i;
4489 isl_ctx *ctx;
4490 int *occurrences = NULL;
4491 struct isl_hash_table *table = NULL;
4492 struct isl_hash_table_entry *entry;
4493 struct isl_constraint_equal_info info;
4494 unsigned n_out;
4495 unsigned n_div;
4497 ctx = isl_basic_map_get_ctx(bmap);
4498 table = isl_hash_table_alloc(ctx, bmap->n_ineq);
4499 if (!table)
4500 goto error;
4502 info.n_in = isl_basic_map_dim(bmap, isl_dim_param) +
4503 isl_basic_map_dim(bmap, isl_dim_in);
4504 occurrences = count_occurrences(bmap, info.n_in);
4505 if (info.n_in && !occurrences)
4506 goto error;
4507 info.bmap = bmap;
4508 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4509 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4510 info.n_out = n_out + n_div;
4511 for (i = 0; i < bmap->n_ineq; ++i) {
4512 uint32_t hash;
4514 info.val = bmap->ineq[i] + 1 + info.n_in;
4515 if (isl_seq_first_non_zero(info.val, n_out) < 0)
4516 continue;
4517 if (isl_seq_first_non_zero(info.val + n_out, n_div) >= 0)
4518 continue;
4519 if (!single_occurrence(info.n_in, bmap->ineq[i] + 1,
4520 occurrences))
4521 continue;
4522 hash = isl_seq_get_hash(info.val, info.n_out);
4523 entry = isl_hash_table_find(ctx, table, hash,
4524 constraint_equal, &info, 1);
4525 if (!entry)
4526 goto error;
4527 if (entry->data)
4528 break;
4529 entry->data = &bmap->ineq[i];
4532 if (i < bmap->n_ineq) {
4533 *first = ((isl_int **)entry->data) - bmap->ineq;
4534 *second = i;
4537 isl_hash_table_free(ctx, table);
4538 free(occurrences);
4540 return i < bmap->n_ineq;
4541 error:
4542 isl_hash_table_free(ctx, table);
4543 free(occurrences);
4544 return isl_bool_error;
4547 /* Given a set of upper bounds in "var", add constraints to "bset"
4548 * that make the i-th bound smallest.
4550 * In particular, if there are n bounds b_i, then add the constraints
4552 * b_i <= b_j for j > i
4553 * b_i < b_j for j < i
4555 static __isl_give isl_basic_set *select_minimum(__isl_take isl_basic_set *bset,
4556 __isl_keep isl_mat *var, int i)
4558 isl_ctx *ctx;
4559 int j, k;
4561 ctx = isl_mat_get_ctx(var);
4563 for (j = 0; j < var->n_row; ++j) {
4564 if (j == i)
4565 continue;
4566 k = isl_basic_set_alloc_inequality(bset);
4567 if (k < 0)
4568 goto error;
4569 isl_seq_combine(bset->ineq[k], ctx->one, var->row[j],
4570 ctx->negone, var->row[i], var->n_col);
4571 isl_int_set_si(bset->ineq[k][var->n_col], 0);
4572 if (j < i)
4573 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
4576 bset = isl_basic_set_finalize(bset);
4578 return bset;
4579 error:
4580 isl_basic_set_free(bset);
4581 return NULL;
4584 /* Given a set of upper bounds on the last "input" variable m,
4585 * construct a set that assigns the minimal upper bound to m, i.e.,
4586 * construct a set that divides the space into cells where one
4587 * of the upper bounds is smaller than all the others and assign
4588 * this upper bound to m.
4590 * In particular, if there are n bounds b_i, then the result
4591 * consists of n basic sets, each one of the form
4593 * m = b_i
4594 * b_i <= b_j for j > i
4595 * b_i < b_j for j < i
4597 static __isl_give isl_set *set_minimum(__isl_take isl_space *dim,
4598 __isl_take isl_mat *var)
4600 int i, k;
4601 isl_basic_set *bset = NULL;
4602 isl_set *set = NULL;
4604 if (!dim || !var)
4605 goto error;
4607 set = isl_set_alloc_space(isl_space_copy(dim),
4608 var->n_row, ISL_SET_DISJOINT);
4610 for (i = 0; i < var->n_row; ++i) {
4611 bset = isl_basic_set_alloc_space(isl_space_copy(dim), 0,
4612 1, var->n_row - 1);
4613 k = isl_basic_set_alloc_equality(bset);
4614 if (k < 0)
4615 goto error;
4616 isl_seq_cpy(bset->eq[k], var->row[i], var->n_col);
4617 isl_int_set_si(bset->eq[k][var->n_col], -1);
4618 bset = select_minimum(bset, var, i);
4619 set = isl_set_add_basic_set(set, bset);
4622 isl_space_free(dim);
4623 isl_mat_free(var);
4624 return set;
4625 error:
4626 isl_basic_set_free(bset);
4627 isl_set_free(set);
4628 isl_space_free(dim);
4629 isl_mat_free(var);
4630 return NULL;
4633 /* Given that the last input variable of "bmap" represents the minimum
4634 * of the bounds in "cst", check whether we need to split the domain
4635 * based on which bound attains the minimum.
4637 * A split is needed when the minimum appears in an integer division
4638 * or in an equality. Otherwise, it is only needed if it appears in
4639 * an upper bound that is different from the upper bounds on which it
4640 * is defined.
4642 static isl_bool need_split_basic_map(__isl_keep isl_basic_map *bmap,
4643 __isl_keep isl_mat *cst)
4645 int i, j;
4646 unsigned total;
4647 unsigned pos;
4649 pos = cst->n_col - 1;
4650 total = isl_basic_map_dim(bmap, isl_dim_all);
4652 for (i = 0; i < bmap->n_div; ++i)
4653 if (!isl_int_is_zero(bmap->div[i][2 + pos]))
4654 return isl_bool_true;
4656 for (i = 0; i < bmap->n_eq; ++i)
4657 if (!isl_int_is_zero(bmap->eq[i][1 + pos]))
4658 return isl_bool_true;
4660 for (i = 0; i < bmap->n_ineq; ++i) {
4661 if (isl_int_is_nonneg(bmap->ineq[i][1 + pos]))
4662 continue;
4663 if (!isl_int_is_negone(bmap->ineq[i][1 + pos]))
4664 return isl_bool_true;
4665 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + pos + 1,
4666 total - pos - 1) >= 0)
4667 return isl_bool_true;
4669 for (j = 0; j < cst->n_row; ++j)
4670 if (isl_seq_eq(bmap->ineq[i], cst->row[j], cst->n_col))
4671 break;
4672 if (j >= cst->n_row)
4673 return isl_bool_true;
4676 return isl_bool_false;
4679 /* Given that the last set variable of "bset" represents the minimum
4680 * of the bounds in "cst", check whether we need to split the domain
4681 * based on which bound attains the minimum.
4683 * We simply call need_split_basic_map here. This is safe because
4684 * the position of the minimum is computed from "cst" and not
4685 * from "bmap".
4687 static isl_bool need_split_basic_set(__isl_keep isl_basic_set *bset,
4688 __isl_keep isl_mat *cst)
4690 return need_split_basic_map(bset_to_bmap(bset), cst);
4693 /* Given that the last set variable of "set" represents the minimum
4694 * of the bounds in "cst", check whether we need to split the domain
4695 * based on which bound attains the minimum.
4697 static isl_bool need_split_set(__isl_keep isl_set *set, __isl_keep isl_mat *cst)
4699 int i;
4701 for (i = 0; i < set->n; ++i) {
4702 isl_bool split;
4704 split = need_split_basic_set(set->p[i], cst);
4705 if (split < 0 || split)
4706 return split;
4709 return isl_bool_false;
4712 /* Given a set of which the last set variable is the minimum
4713 * of the bounds in "cst", split each basic set in the set
4714 * in pieces where one of the bounds is (strictly) smaller than the others.
4715 * This subdivision is given in "min_expr".
4716 * The variable is subsequently projected out.
4718 * We only do the split when it is needed.
4719 * For example if the last input variable m = min(a,b) and the only
4720 * constraints in the given basic set are lower bounds on m,
4721 * i.e., l <= m = min(a,b), then we can simply project out m
4722 * to obtain l <= a and l <= b, without having to split on whether
4723 * m is equal to a or b.
4725 static __isl_give isl_set *split(__isl_take isl_set *empty,
4726 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
4728 int n_in;
4729 int i;
4730 isl_space *dim;
4731 isl_set *res;
4733 if (!empty || !min_expr || !cst)
4734 goto error;
4736 n_in = isl_set_dim(empty, isl_dim_set);
4737 dim = isl_set_get_space(empty);
4738 dim = isl_space_drop_dims(dim, isl_dim_set, n_in - 1, 1);
4739 res = isl_set_empty(dim);
4741 for (i = 0; i < empty->n; ++i) {
4742 isl_bool split;
4743 isl_set *set;
4745 set = isl_set_from_basic_set(isl_basic_set_copy(empty->p[i]));
4746 split = need_split_basic_set(empty->p[i], cst);
4747 if (split < 0)
4748 set = isl_set_free(set);
4749 else if (split)
4750 set = isl_set_intersect(set, isl_set_copy(min_expr));
4751 set = isl_set_remove_dims(set, isl_dim_set, n_in - 1, 1);
4753 res = isl_set_union_disjoint(res, set);
4756 isl_set_free(empty);
4757 isl_set_free(min_expr);
4758 isl_mat_free(cst);
4759 return res;
4760 error:
4761 isl_set_free(empty);
4762 isl_set_free(min_expr);
4763 isl_mat_free(cst);
4764 return NULL;
4767 /* Given a map of which the last input variable is the minimum
4768 * of the bounds in "cst", split each basic set in the set
4769 * in pieces where one of the bounds is (strictly) smaller than the others.
4770 * This subdivision is given in "min_expr".
4771 * The variable is subsequently projected out.
4773 * The implementation is essentially the same as that of "split".
4775 static __isl_give isl_map *split_domain(__isl_take isl_map *opt,
4776 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
4778 int n_in;
4779 int i;
4780 isl_space *dim;
4781 isl_map *res;
4783 if (!opt || !min_expr || !cst)
4784 goto error;
4786 n_in = isl_map_dim(opt, isl_dim_in);
4787 dim = isl_map_get_space(opt);
4788 dim = isl_space_drop_dims(dim, isl_dim_in, n_in - 1, 1);
4789 res = isl_map_empty(dim);
4791 for (i = 0; i < opt->n; ++i) {
4792 isl_map *map;
4793 isl_bool split;
4795 map = isl_map_from_basic_map(isl_basic_map_copy(opt->p[i]));
4796 split = need_split_basic_map(opt->p[i], cst);
4797 if (split < 0)
4798 map = isl_map_free(map);
4799 else if (split)
4800 map = isl_map_intersect_domain(map,
4801 isl_set_copy(min_expr));
4802 map = isl_map_remove_dims(map, isl_dim_in, n_in - 1, 1);
4804 res = isl_map_union_disjoint(res, map);
4807 isl_map_free(opt);
4808 isl_set_free(min_expr);
4809 isl_mat_free(cst);
4810 return res;
4811 error:
4812 isl_map_free(opt);
4813 isl_set_free(min_expr);
4814 isl_mat_free(cst);
4815 return NULL;
4818 static __isl_give isl_map *basic_map_partial_lexopt(
4819 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4820 __isl_give isl_set **empty, int max);
4822 /* This function is called from basic_map_partial_lexopt_symm.
4823 * The last variable of "bmap" and "dom" corresponds to the minimum
4824 * of the bounds in "cst". "map_space" is the space of the original
4825 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
4826 * is the space of the original domain.
4828 * We recursively call basic_map_partial_lexopt and then plug in
4829 * the definition of the minimum in the result.
4831 static __isl_give isl_map *basic_map_partial_lexopt_symm_core(
4832 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4833 __isl_give isl_set **empty, int max, __isl_take isl_mat *cst,
4834 __isl_take isl_space *map_space, __isl_take isl_space *set_space)
4836 isl_map *opt;
4837 isl_set *min_expr;
4839 min_expr = set_minimum(isl_basic_set_get_space(dom), isl_mat_copy(cst));
4841 opt = basic_map_partial_lexopt(bmap, dom, empty, max);
4843 if (empty) {
4844 *empty = split(*empty,
4845 isl_set_copy(min_expr), isl_mat_copy(cst));
4846 *empty = isl_set_reset_space(*empty, set_space);
4849 opt = split_domain(opt, min_expr, cst);
4850 opt = isl_map_reset_space(opt, map_space);
4852 return opt;
4855 /* Extract a domain from "bmap" for the purpose of computing
4856 * a lexicographic optimum.
4858 * This function is only called when the caller wants to compute a full
4859 * lexicographic optimum, i.e., without specifying a domain. In this case,
4860 * the caller is not interested in the part of the domain space where
4861 * there is no solution and the domain can be initialized to those constraints
4862 * of "bmap" that only involve the parameters and the input dimensions.
4863 * This relieves the parametric programming engine from detecting those
4864 * inequalities and transferring them to the context. More importantly,
4865 * it ensures that those inequalities are transferred first and not
4866 * intermixed with inequalities that actually split the domain.
4868 * If the caller does not require the absence of existentially quantified
4869 * variables in the result (i.e., if ISL_OPT_QE is not set in "flags"),
4870 * then the actual domain of "bmap" can be used. This ensures that
4871 * the domain does not need to be split at all just to separate out
4872 * pieces of the domain that do not have a solution from piece that do.
4873 * This domain cannot be used in general because it may involve
4874 * (unknown) existentially quantified variables which will then also
4875 * appear in the solution.
4877 static __isl_give isl_basic_set *extract_domain(__isl_keep isl_basic_map *bmap,
4878 unsigned flags)
4880 int n_div;
4881 int n_out;
4883 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4884 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4885 bmap = isl_basic_map_copy(bmap);
4886 if (ISL_FL_ISSET(flags, ISL_OPT_QE)) {
4887 bmap = isl_basic_map_drop_constraints_involving_dims(bmap,
4888 isl_dim_div, 0, n_div);
4889 bmap = isl_basic_map_drop_constraints_involving_dims(bmap,
4890 isl_dim_out, 0, n_out);
4892 return isl_basic_map_domain(bmap);
4895 #undef TYPE
4896 #define TYPE isl_map
4897 #undef SUFFIX
4898 #define SUFFIX
4899 #include "isl_tab_lexopt_templ.c"
4901 struct isl_sol_for {
4902 struct isl_sol sol;
4903 isl_stat (*fn)(__isl_take isl_basic_set *dom,
4904 __isl_take isl_aff_list *list, void *user);
4905 void *user;
4908 static void sol_for_free(struct isl_sol *sol)
4912 /* Add the solution identified by the tableau and the context tableau.
4913 * In particular, "dom" represents the context and "ma" expresses
4914 * the solution on that context.
4916 * See documentation of sol_add for more details.
4918 * Instead of constructing a basic map, this function calls a user
4919 * defined function with the current context as a basic set and
4920 * a list of affine expressions representing the relation between
4921 * the input and output. The space over which the affine expressions
4922 * are defined is the same as that of the domain. The number of
4923 * affine expressions in the list is equal to the number of output variables.
4925 static void sol_for_add(struct isl_sol_for *sol,
4926 __isl_take isl_basic_set *dom, __isl_take isl_multi_aff *ma)
4928 int i, n;
4929 isl_ctx *ctx;
4930 isl_aff *aff;
4931 isl_aff_list *list;
4933 if (sol->sol.error || !dom || !ma)
4934 goto error;
4936 ctx = isl_basic_set_get_ctx(dom);
4937 n = isl_multi_aff_dim(ma, isl_dim_out);
4938 list = isl_aff_list_alloc(ctx, n);
4939 for (i = 0; i < n; ++i) {
4940 aff = isl_multi_aff_get_aff(ma, i);
4941 list = isl_aff_list_add(list, aff);
4944 dom = isl_basic_set_finalize(dom);
4946 if (sol->fn(isl_basic_set_copy(dom), list, sol->user) < 0)
4947 goto error;
4949 isl_basic_set_free(dom);
4950 isl_multi_aff_free(ma);
4951 return;
4952 error:
4953 isl_basic_set_free(dom);
4954 isl_multi_aff_free(ma);
4955 sol->sol.error = 1;
4958 static void sol_for_add_wrap(struct isl_sol *sol,
4959 __isl_take isl_basic_set *dom, __isl_take isl_multi_aff *ma)
4961 sol_for_add((struct isl_sol_for *)sol, dom, ma);
4964 static struct isl_sol_for *sol_for_init(__isl_keep isl_basic_map *bmap, int max,
4965 isl_stat (*fn)(__isl_take isl_basic_set *dom,
4966 __isl_take isl_aff_list *list, void *user),
4967 void *user)
4969 struct isl_sol_for *sol_for = NULL;
4970 isl_space *dom_dim;
4971 struct isl_basic_set *dom = NULL;
4973 sol_for = isl_calloc_type(bmap->ctx, struct isl_sol_for);
4974 if (!sol_for)
4975 goto error;
4977 dom_dim = isl_space_domain(isl_space_copy(bmap->dim));
4978 dom = isl_basic_set_universe(dom_dim);
4980 sol_for->sol.free = &sol_for_free;
4981 if (sol_init(&sol_for->sol, bmap, dom, max) < 0)
4982 goto error;
4983 sol_for->fn = fn;
4984 sol_for->user = user;
4985 sol_for->sol.add = &sol_for_add_wrap;
4986 sol_for->sol.add_empty = NULL;
4988 isl_basic_set_free(dom);
4989 return sol_for;
4990 error:
4991 isl_basic_set_free(dom);
4992 sol_free(&sol_for->sol);
4993 return NULL;
4996 static void sol_for_find_solutions(struct isl_sol_for *sol_for,
4997 struct isl_tab *tab)
4999 find_solutions_main(&sol_for->sol, tab);
5002 isl_stat isl_basic_map_foreach_lexopt(__isl_keep isl_basic_map *bmap, int max,
5003 isl_stat (*fn)(__isl_take isl_basic_set *dom,
5004 __isl_take isl_aff_list *list, void *user),
5005 void *user)
5007 struct isl_sol_for *sol_for = NULL;
5009 bmap = isl_basic_map_copy(bmap);
5010 bmap = isl_basic_map_detect_equalities(bmap);
5011 if (!bmap)
5012 return isl_stat_error;
5014 sol_for = sol_for_init(bmap, max, fn, user);
5015 if (!sol_for)
5016 goto error;
5018 if (isl_basic_map_plain_is_empty(bmap))
5019 /* nothing */;
5020 else {
5021 struct isl_tab *tab;
5022 struct isl_context *context = sol_for->sol.context;
5023 tab = tab_for_lexmin(bmap,
5024 context->op->peek_basic_set(context), 1, max);
5025 tab = context->op->detect_nonnegative_parameters(context, tab);
5026 sol_for_find_solutions(sol_for, tab);
5027 if (sol_for->sol.error)
5028 goto error;
5031 sol_free(&sol_for->sol);
5032 isl_basic_map_free(bmap);
5033 return isl_stat_ok;
5034 error:
5035 sol_free(&sol_for->sol);
5036 isl_basic_map_free(bmap);
5037 return isl_stat_error;
5040 isl_stat isl_basic_set_foreach_lexopt(__isl_keep isl_basic_set *bset, int max,
5041 isl_stat (*fn)(__isl_take isl_basic_set *dom,
5042 __isl_take isl_aff_list *list, void *user),
5043 void *user)
5045 return isl_basic_map_foreach_lexopt(bset, max, fn, user);
5048 /* Check if the given sequence of len variables starting at pos
5049 * represents a trivial (i.e., zero) solution.
5050 * The variables are assumed to be non-negative and to come in pairs,
5051 * with each pair representing a variable of unrestricted sign.
5052 * The solution is trivial if each such pair in the sequence consists
5053 * of two identical values, meaning that the variable being represented
5054 * has value zero.
5056 static int region_is_trivial(struct isl_tab *tab, int pos, int len)
5058 int i;
5060 if (len == 0)
5061 return 0;
5063 for (i = 0; i < len; i += 2) {
5064 int neg_row;
5065 int pos_row;
5067 neg_row = tab->var[pos + i].is_row ?
5068 tab->var[pos + i].index : -1;
5069 pos_row = tab->var[pos + i + 1].is_row ?
5070 tab->var[pos + i + 1].index : -1;
5072 if ((neg_row < 0 ||
5073 isl_int_is_zero(tab->mat->row[neg_row][1])) &&
5074 (pos_row < 0 ||
5075 isl_int_is_zero(tab->mat->row[pos_row][1])))
5076 continue;
5078 if (neg_row < 0 || pos_row < 0)
5079 return 0;
5080 if (isl_int_ne(tab->mat->row[neg_row][1],
5081 tab->mat->row[pos_row][1]))
5082 return 0;
5085 return 1;
5088 /* Return the index of the first trivial region or -1 if all regions
5089 * are non-trivial.
5091 static int first_trivial_region(struct isl_tab *tab,
5092 int n_region, struct isl_region *region)
5094 int i;
5096 for (i = 0; i < n_region; ++i) {
5097 if (region_is_trivial(tab, region[i].pos, region[i].len))
5098 return i;
5101 return -1;
5104 /* Check if the solution is optimal, i.e., whether the first
5105 * n_op entries are zero.
5107 static int is_optimal(__isl_keep isl_vec *sol, int n_op)
5109 int i;
5111 for (i = 0; i < n_op; ++i)
5112 if (!isl_int_is_zero(sol->el[1 + i]))
5113 return 0;
5114 return 1;
5117 /* Add constraints to "tab" that ensure that any solution is significantly
5118 * better than that represented by "sol". That is, find the first
5119 * relevant (within first n_op) non-zero coefficient and force it (along
5120 * with all previous coefficients) to be zero.
5121 * If the solution is already optimal (all relevant coefficients are zero),
5122 * then just mark the table as empty.
5124 * This function assumes that at least 2 * n_op more rows and at least
5125 * 2 * n_op more elements in the constraint array are available in the tableau.
5127 static int force_better_solution(struct isl_tab *tab,
5128 __isl_keep isl_vec *sol, int n_op)
5130 int i;
5131 isl_ctx *ctx;
5132 isl_vec *v = NULL;
5134 if (!sol)
5135 return -1;
5137 for (i = 0; i < n_op; ++i)
5138 if (!isl_int_is_zero(sol->el[1 + i]))
5139 break;
5141 if (i == n_op) {
5142 if (isl_tab_mark_empty(tab) < 0)
5143 return -1;
5144 return 0;
5147 ctx = isl_vec_get_ctx(sol);
5148 v = isl_vec_alloc(ctx, 1 + tab->n_var);
5149 if (!v)
5150 return -1;
5152 for (; i >= 0; --i) {
5153 v = isl_vec_clr(v);
5154 isl_int_set_si(v->el[1 + i], -1);
5155 if (add_lexmin_eq(tab, v->el) < 0)
5156 goto error;
5159 isl_vec_free(v);
5160 return 0;
5161 error:
5162 isl_vec_free(v);
5163 return -1;
5166 struct isl_trivial {
5167 int update;
5168 int region;
5169 int side;
5170 struct isl_tab_undo *snap;
5173 /* Return the lexicographically smallest non-trivial solution of the
5174 * given ILP problem.
5176 * All variables are assumed to be non-negative.
5178 * n_op is the number of initial coordinates to optimize.
5179 * That is, once a solution has been found, we will only continue looking
5180 * for solution that result in significantly better values for those
5181 * initial coordinates. That is, we only continue looking for solutions
5182 * that increase the number of initial zeros in this sequence.
5184 * A solution is non-trivial, if it is non-trivial on each of the
5185 * specified regions. Each region represents a sequence of pairs
5186 * of variables. A solution is non-trivial on such a region if
5187 * at least one of these pairs consists of different values, i.e.,
5188 * such that the non-negative variable represented by the pair is non-zero.
5190 * Whenever a conflict is encountered, all constraints involved are
5191 * reported to the caller through a call to "conflict".
5193 * We perform a simple branch-and-bound backtracking search.
5194 * Each level in the search represents initially trivial region that is forced
5195 * to be non-trivial.
5196 * At each level we consider n cases, where n is the length of the region.
5197 * In terms of the n/2 variables of unrestricted signs being encoded by
5198 * the region, we consider the cases
5199 * x_0 >= 1
5200 * x_0 <= -1
5201 * x_0 = 0 and x_1 >= 1
5202 * x_0 = 0 and x_1 <= -1
5203 * x_0 = 0 and x_1 = 0 and x_2 >= 1
5204 * x_0 = 0 and x_1 = 0 and x_2 <= -1
5205 * ...
5206 * The cases are considered in this order, assuming that each pair
5207 * x_i_a x_i_b represents the value x_i_b - x_i_a.
5208 * That is, x_0 >= 1 is enforced by adding the constraint
5209 * x_0_b - x_0_a >= 1
5211 __isl_give isl_vec *isl_tab_basic_set_non_trivial_lexmin(
5212 __isl_take isl_basic_set *bset, int n_op, int n_region,
5213 struct isl_region *region,
5214 int (*conflict)(int con, void *user), void *user)
5216 int i, j;
5217 int r;
5218 isl_ctx *ctx;
5219 isl_vec *v = NULL;
5220 isl_vec *sol = NULL;
5221 struct isl_tab *tab;
5222 struct isl_trivial *triv = NULL;
5223 int level, init;
5225 if (!bset)
5226 return NULL;
5228 ctx = isl_basic_set_get_ctx(bset);
5229 sol = isl_vec_alloc(ctx, 0);
5231 tab = tab_for_lexmin(bset, NULL, 0, 0);
5232 if (!tab)
5233 goto error;
5234 tab->conflict = conflict;
5235 tab->conflict_user = user;
5237 v = isl_vec_alloc(ctx, 1 + tab->n_var);
5238 triv = isl_calloc_array(ctx, struct isl_trivial, n_region);
5239 if (!v || (n_region && !triv))
5240 goto error;
5242 level = 0;
5243 init = 1;
5245 while (level >= 0) {
5246 int side, base;
5248 if (init) {
5249 tab = cut_to_integer_lexmin(tab, CUT_ONE);
5250 if (!tab)
5251 goto error;
5252 if (tab->empty)
5253 goto backtrack;
5254 r = first_trivial_region(tab, n_region, region);
5255 if (r < 0) {
5256 for (i = 0; i < level; ++i)
5257 triv[i].update = 1;
5258 isl_vec_free(sol);
5259 sol = isl_tab_get_sample_value(tab);
5260 if (!sol)
5261 goto error;
5262 if (is_optimal(sol, n_op))
5263 break;
5264 goto backtrack;
5266 if (level >= n_region)
5267 isl_die(ctx, isl_error_internal,
5268 "nesting level too deep", goto error);
5269 if (isl_tab_extend_cons(tab,
5270 2 * region[r].len + 2 * n_op) < 0)
5271 goto error;
5272 triv[level].region = r;
5273 triv[level].side = 0;
5276 r = triv[level].region;
5277 side = triv[level].side;
5278 base = 2 * (side/2);
5280 if (side >= region[r].len) {
5281 backtrack:
5282 level--;
5283 init = 0;
5284 if (level >= 0)
5285 if (isl_tab_rollback(tab, triv[level].snap) < 0)
5286 goto error;
5287 continue;
5290 if (triv[level].update) {
5291 if (force_better_solution(tab, sol, n_op) < 0)
5292 goto error;
5293 triv[level].update = 0;
5296 if (side == base && base >= 2) {
5297 for (j = base - 2; j < base; ++j) {
5298 v = isl_vec_clr(v);
5299 isl_int_set_si(v->el[1 + region[r].pos + j], 1);
5300 if (add_lexmin_eq(tab, v->el) < 0)
5301 goto error;
5305 triv[level].snap = isl_tab_snap(tab);
5306 if (isl_tab_push_basis(tab) < 0)
5307 goto error;
5309 v = isl_vec_clr(v);
5310 isl_int_set_si(v->el[0], -1);
5311 isl_int_set_si(v->el[1 + region[r].pos + side], -1);
5312 isl_int_set_si(v->el[1 + region[r].pos + (side ^ 1)], 1);
5313 tab = add_lexmin_ineq(tab, v->el);
5315 triv[level].side++;
5316 level++;
5317 init = 1;
5320 free(triv);
5321 isl_vec_free(v);
5322 isl_tab_free(tab);
5323 isl_basic_set_free(bset);
5325 return sol;
5326 error:
5327 free(triv);
5328 isl_vec_free(v);
5329 isl_tab_free(tab);
5330 isl_basic_set_free(bset);
5331 isl_vec_free(sol);
5332 return NULL;
5335 /* Wrapper for a tableau that is used for computing
5336 * the lexicographically smallest rational point of a non-negative set.
5337 * This point is represented by the sample value of "tab",
5338 * unless "tab" is empty.
5340 struct isl_tab_lexmin {
5341 isl_ctx *ctx;
5342 struct isl_tab *tab;
5345 /* Free "tl" and return NULL.
5347 __isl_null isl_tab_lexmin *isl_tab_lexmin_free(__isl_take isl_tab_lexmin *tl)
5349 if (!tl)
5350 return NULL;
5351 isl_ctx_deref(tl->ctx);
5352 isl_tab_free(tl->tab);
5353 free(tl);
5355 return NULL;
5358 /* Construct an isl_tab_lexmin for computing
5359 * the lexicographically smallest rational point in "bset",
5360 * assuming that all variables are non-negative.
5362 __isl_give isl_tab_lexmin *isl_tab_lexmin_from_basic_set(
5363 __isl_take isl_basic_set *bset)
5365 isl_ctx *ctx;
5366 isl_tab_lexmin *tl;
5368 if (!bset)
5369 return NULL;
5371 ctx = isl_basic_set_get_ctx(bset);
5372 tl = isl_calloc_type(ctx, struct isl_tab_lexmin);
5373 if (!tl)
5374 goto error;
5375 tl->ctx = ctx;
5376 isl_ctx_ref(ctx);
5377 tl->tab = tab_for_lexmin(bset, NULL, 0, 0);
5378 isl_basic_set_free(bset);
5379 if (!tl->tab)
5380 return isl_tab_lexmin_free(tl);
5381 return tl;
5382 error:
5383 isl_basic_set_free(bset);
5384 isl_tab_lexmin_free(tl);
5385 return NULL;
5388 /* Return the dimension of the set represented by "tl".
5390 int isl_tab_lexmin_dim(__isl_keep isl_tab_lexmin *tl)
5392 return tl ? tl->tab->n_var : -1;
5395 /* Add the equality with coefficients "eq" to "tl", updating the optimal
5396 * solution if needed.
5397 * The equality is added as two opposite inequality constraints.
5399 __isl_give isl_tab_lexmin *isl_tab_lexmin_add_eq(__isl_take isl_tab_lexmin *tl,
5400 isl_int *eq)
5402 unsigned n_var;
5404 if (!tl || !eq)
5405 return isl_tab_lexmin_free(tl);
5407 if (isl_tab_extend_cons(tl->tab, 2) < 0)
5408 return isl_tab_lexmin_free(tl);
5409 n_var = tl->tab->n_var;
5410 isl_seq_neg(eq, eq, 1 + n_var);
5411 tl->tab = add_lexmin_ineq(tl->tab, eq);
5412 isl_seq_neg(eq, eq, 1 + n_var);
5413 tl->tab = add_lexmin_ineq(tl->tab, eq);
5415 if (!tl->tab)
5416 return isl_tab_lexmin_free(tl);
5418 return tl;
5421 /* Return the lexicographically smallest rational point in the basic set
5422 * from which "tl" was constructed.
5423 * If the original input was empty, then return a zero-length vector.
5425 __isl_give isl_vec *isl_tab_lexmin_get_solution(__isl_keep isl_tab_lexmin *tl)
5427 if (!tl)
5428 return NULL;
5429 if (tl->tab->empty)
5430 return isl_vec_alloc(tl->ctx, 0);
5431 else
5432 return isl_tab_get_sample_value(tl->tab);
5435 /* Return the lexicographically smallest rational point in "bset",
5436 * assuming that all variables are non-negative.
5437 * If "bset" is empty, then return a zero-length vector.
5439 __isl_give isl_vec *isl_tab_basic_set_non_neg_lexmin(
5440 __isl_take isl_basic_set *bset)
5442 isl_tab_lexmin *tl;
5443 isl_vec *sol;
5445 tl = isl_tab_lexmin_from_basic_set(bset);
5446 sol = isl_tab_lexmin_get_solution(tl);
5447 isl_tab_lexmin_free(tl);
5448 return sol;
5451 struct isl_sol_pma {
5452 struct isl_sol sol;
5453 isl_pw_multi_aff *pma;
5454 isl_set *empty;
5457 static void sol_pma_free(struct isl_sol *sol)
5459 struct isl_sol_pma *sol_pma = (struct isl_sol_pma *) sol;
5460 isl_pw_multi_aff_free(sol_pma->pma);
5461 isl_set_free(sol_pma->empty);
5464 /* This function is called for parts of the context where there is
5465 * no solution, with "bset" corresponding to the context tableau.
5466 * Simply add the basic set to the set "empty".
5468 static void sol_pma_add_empty(struct isl_sol_pma *sol,
5469 __isl_take isl_basic_set *bset)
5471 if (!bset || !sol->empty)
5472 goto error;
5474 sol->empty = isl_set_grow(sol->empty, 1);
5475 bset = isl_basic_set_simplify(bset);
5476 bset = isl_basic_set_finalize(bset);
5477 sol->empty = isl_set_add_basic_set(sol->empty, bset);
5478 if (!sol->empty)
5479 sol->sol.error = 1;
5480 return;
5481 error:
5482 isl_basic_set_free(bset);
5483 sol->sol.error = 1;
5486 /* Given a basic set "dom" that represents the context and a tuple of
5487 * affine expressions "maff" defined over this domain, construct
5488 * an isl_pw_multi_aff with a single cell corresponding to "dom" and
5489 * the affine expressions in "maff".
5491 static void sol_pma_add(struct isl_sol_pma *sol,
5492 __isl_take isl_basic_set *dom, __isl_take isl_multi_aff *maff)
5494 isl_pw_multi_aff *pma;
5496 dom = isl_basic_set_simplify(dom);
5497 dom = isl_basic_set_finalize(dom);
5498 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(dom), maff);
5499 sol->pma = isl_pw_multi_aff_add_disjoint(sol->pma, pma);
5500 if (!sol->pma)
5501 sol->sol.error = 1;
5504 static void sol_pma_add_empty_wrap(struct isl_sol *sol,
5505 __isl_take isl_basic_set *bset)
5507 sol_pma_add_empty((struct isl_sol_pma *)sol, bset);
5510 static void sol_pma_add_wrap(struct isl_sol *sol,
5511 __isl_take isl_basic_set *dom, __isl_take isl_multi_aff *ma)
5513 sol_pma_add((struct isl_sol_pma *)sol, dom, ma);
5516 /* Construct an isl_sol_pma structure for accumulating the solution.
5517 * If track_empty is set, then we also keep track of the parts
5518 * of the context where there is no solution.
5519 * If max is set, then we are solving a maximization, rather than
5520 * a minimization problem, which means that the variables in the
5521 * tableau have value "M - x" rather than "M + x".
5523 static struct isl_sol *sol_pma_init(__isl_keep isl_basic_map *bmap,
5524 __isl_take isl_basic_set *dom, int track_empty, int max)
5526 struct isl_sol_pma *sol_pma = NULL;
5527 isl_space *space;
5529 if (!bmap)
5530 goto error;
5532 sol_pma = isl_calloc_type(bmap->ctx, struct isl_sol_pma);
5533 if (!sol_pma)
5534 goto error;
5536 sol_pma->sol.free = &sol_pma_free;
5537 if (sol_init(&sol_pma->sol, bmap, dom, max) < 0)
5538 goto error;
5539 sol_pma->sol.add = &sol_pma_add_wrap;
5540 sol_pma->sol.add_empty = track_empty ? &sol_pma_add_empty_wrap : NULL;
5541 space = isl_space_copy(sol_pma->sol.space);
5542 sol_pma->pma = isl_pw_multi_aff_empty(space);
5543 if (!sol_pma->pma)
5544 goto error;
5546 if (track_empty) {
5547 sol_pma->empty = isl_set_alloc_space(isl_basic_set_get_space(dom),
5548 1, ISL_SET_DISJOINT);
5549 if (!sol_pma->empty)
5550 goto error;
5553 isl_basic_set_free(dom);
5554 return &sol_pma->sol;
5555 error:
5556 isl_basic_set_free(dom);
5557 sol_free(&sol_pma->sol);
5558 return NULL;
5561 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
5562 * some obvious symmetries.
5564 * We call basic_map_partial_lexopt_base_sol and extract the results.
5566 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_base_pw_multi_aff(
5567 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5568 __isl_give isl_set **empty, int max)
5570 isl_pw_multi_aff *result = NULL;
5571 struct isl_sol *sol;
5572 struct isl_sol_pma *sol_pma;
5574 sol = basic_map_partial_lexopt_base_sol(bmap, dom, empty, max,
5575 &sol_pma_init);
5576 if (!sol)
5577 return NULL;
5578 sol_pma = (struct isl_sol_pma *) sol;
5580 result = isl_pw_multi_aff_copy(sol_pma->pma);
5581 if (empty)
5582 *empty = isl_set_copy(sol_pma->empty);
5583 sol_free(&sol_pma->sol);
5584 return result;
5587 /* Given that the last input variable of "maff" represents the minimum
5588 * of some bounds, check whether we need to plug in the expression
5589 * of the minimum.
5591 * In particular, check if the last input variable appears in any
5592 * of the expressions in "maff".
5594 static int need_substitution(__isl_keep isl_multi_aff *maff)
5596 int i;
5597 unsigned pos;
5599 pos = isl_multi_aff_dim(maff, isl_dim_in) - 1;
5601 for (i = 0; i < maff->n; ++i)
5602 if (isl_aff_involves_dims(maff->p[i], isl_dim_in, pos, 1))
5603 return 1;
5605 return 0;
5608 /* Given a set of upper bounds on the last "input" variable m,
5609 * construct a piecewise affine expression that selects
5610 * the minimal upper bound to m, i.e.,
5611 * divide the space into cells where one
5612 * of the upper bounds is smaller than all the others and select
5613 * this upper bound on that cell.
5615 * In particular, if there are n bounds b_i, then the result
5616 * consists of n cell, each one of the form
5618 * b_i <= b_j for j > i
5619 * b_i < b_j for j < i
5621 * The affine expression on this cell is
5623 * b_i
5625 static __isl_give isl_pw_aff *set_minimum_pa(__isl_take isl_space *space,
5626 __isl_take isl_mat *var)
5628 int i;
5629 isl_aff *aff = NULL;
5630 isl_basic_set *bset = NULL;
5631 isl_pw_aff *paff = NULL;
5632 isl_space *pw_space;
5633 isl_local_space *ls = NULL;
5635 if (!space || !var)
5636 goto error;
5638 ls = isl_local_space_from_space(isl_space_copy(space));
5639 pw_space = isl_space_copy(space);
5640 pw_space = isl_space_from_domain(pw_space);
5641 pw_space = isl_space_add_dims(pw_space, isl_dim_out, 1);
5642 paff = isl_pw_aff_alloc_size(pw_space, var->n_row);
5644 for (i = 0; i < var->n_row; ++i) {
5645 isl_pw_aff *paff_i;
5647 aff = isl_aff_alloc(isl_local_space_copy(ls));
5648 bset = isl_basic_set_alloc_space(isl_space_copy(space), 0,
5649 0, var->n_row - 1);
5650 if (!aff || !bset)
5651 goto error;
5652 isl_int_set_si(aff->v->el[0], 1);
5653 isl_seq_cpy(aff->v->el + 1, var->row[i], var->n_col);
5654 isl_int_set_si(aff->v->el[1 + var->n_col], 0);
5655 bset = select_minimum(bset, var, i);
5656 paff_i = isl_pw_aff_alloc(isl_set_from_basic_set(bset), aff);
5657 paff = isl_pw_aff_add_disjoint(paff, paff_i);
5660 isl_local_space_free(ls);
5661 isl_space_free(space);
5662 isl_mat_free(var);
5663 return paff;
5664 error:
5665 isl_aff_free(aff);
5666 isl_basic_set_free(bset);
5667 isl_pw_aff_free(paff);
5668 isl_local_space_free(ls);
5669 isl_space_free(space);
5670 isl_mat_free(var);
5671 return NULL;
5674 /* Given a piecewise multi-affine expression of which the last input variable
5675 * is the minimum of the bounds in "cst", plug in the value of the minimum.
5676 * This minimum expression is given in "min_expr_pa".
5677 * The set "min_expr" contains the same information, but in the form of a set.
5678 * The variable is subsequently projected out.
5680 * The implementation is similar to those of "split" and "split_domain".
5681 * If the variable appears in a given expression, then minimum expression
5682 * is plugged in. Otherwise, if the variable appears in the constraints
5683 * and a split is required, then the domain is split. Otherwise, no split
5684 * is performed.
5686 static __isl_give isl_pw_multi_aff *split_domain_pma(
5687 __isl_take isl_pw_multi_aff *opt, __isl_take isl_pw_aff *min_expr_pa,
5688 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
5690 int n_in;
5691 int i;
5692 isl_space *space;
5693 isl_pw_multi_aff *res;
5695 if (!opt || !min_expr || !cst)
5696 goto error;
5698 n_in = isl_pw_multi_aff_dim(opt, isl_dim_in);
5699 space = isl_pw_multi_aff_get_space(opt);
5700 space = isl_space_drop_dims(space, isl_dim_in, n_in - 1, 1);
5701 res = isl_pw_multi_aff_empty(space);
5703 for (i = 0; i < opt->n; ++i) {
5704 isl_pw_multi_aff *pma;
5706 pma = isl_pw_multi_aff_alloc(isl_set_copy(opt->p[i].set),
5707 isl_multi_aff_copy(opt->p[i].maff));
5708 if (need_substitution(opt->p[i].maff))
5709 pma = isl_pw_multi_aff_substitute(pma,
5710 isl_dim_in, n_in - 1, min_expr_pa);
5711 else {
5712 isl_bool split;
5713 split = need_split_set(opt->p[i].set, cst);
5714 if (split < 0)
5715 pma = isl_pw_multi_aff_free(pma);
5716 else if (split)
5717 pma = isl_pw_multi_aff_intersect_domain(pma,
5718 isl_set_copy(min_expr));
5720 pma = isl_pw_multi_aff_project_out(pma,
5721 isl_dim_in, n_in - 1, 1);
5723 res = isl_pw_multi_aff_add_disjoint(res, pma);
5726 isl_pw_multi_aff_free(opt);
5727 isl_pw_aff_free(min_expr_pa);
5728 isl_set_free(min_expr);
5729 isl_mat_free(cst);
5730 return res;
5731 error:
5732 isl_pw_multi_aff_free(opt);
5733 isl_pw_aff_free(min_expr_pa);
5734 isl_set_free(min_expr);
5735 isl_mat_free(cst);
5736 return NULL;
5739 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_pw_multi_aff(
5740 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5741 __isl_give isl_set **empty, int max);
5743 /* This function is called from basic_map_partial_lexopt_symm.
5744 * The last variable of "bmap" and "dom" corresponds to the minimum
5745 * of the bounds in "cst". "map_space" is the space of the original
5746 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
5747 * is the space of the original domain.
5749 * We recursively call basic_map_partial_lexopt and then plug in
5750 * the definition of the minimum in the result.
5752 static __isl_give isl_pw_multi_aff *
5753 basic_map_partial_lexopt_symm_core_pw_multi_aff(
5754 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5755 __isl_give isl_set **empty, int max, __isl_take isl_mat *cst,
5756 __isl_take isl_space *map_space, __isl_take isl_space *set_space)
5758 isl_pw_multi_aff *opt;
5759 isl_pw_aff *min_expr_pa;
5760 isl_set *min_expr;
5762 min_expr = set_minimum(isl_basic_set_get_space(dom), isl_mat_copy(cst));
5763 min_expr_pa = set_minimum_pa(isl_basic_set_get_space(dom),
5764 isl_mat_copy(cst));
5766 opt = basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, max);
5768 if (empty) {
5769 *empty = split(*empty,
5770 isl_set_copy(min_expr), isl_mat_copy(cst));
5771 *empty = isl_set_reset_space(*empty, set_space);
5774 opt = split_domain_pma(opt, min_expr_pa, min_expr, cst);
5775 opt = isl_pw_multi_aff_reset_space(opt, map_space);
5777 return opt;
5780 #undef TYPE
5781 #define TYPE isl_pw_multi_aff
5782 #undef SUFFIX
5783 #define SUFFIX _pw_multi_aff
5784 #include "isl_tab_lexopt_templ.c"