2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the GNU LGPLv2.1 license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 #include "isl_map_private.h"
16 #include "isl_sample.h"
17 #include <isl_mat_private.h>
20 * The implementation of parametric integer linear programming in this file
21 * was inspired by the paper "Parametric Integer Programming" and the
22 * report "Solving systems of affine (in)equalities" by Paul Feautrier
25 * The strategy used for obtaining a feasible solution is different
26 * from the one used in isl_tab.c. In particular, in isl_tab.c,
27 * upon finding a constraint that is not yet satisfied, we pivot
28 * in a row that increases the constant term of row holding the
29 * constraint, making sure the sample solution remains feasible
30 * for all the constraints it already satisfied.
31 * Here, we always pivot in the row holding the constraint,
32 * choosing a column that induces the lexicographically smallest
33 * increment to the sample solution.
35 * By starting out from a sample value that is lexicographically
36 * smaller than any integer point in the problem space, the first
37 * feasible integer sample point we find will also be the lexicographically
38 * smallest. If all variables can be assumed to be non-negative,
39 * then the initial sample value may be chosen equal to zero.
40 * However, we will not make this assumption. Instead, we apply
41 * the "big parameter" trick. Any variable x is then not directly
42 * used in the tableau, but instead it its represented by another
43 * variable x' = M + x, where M is an arbitrarily large (positive)
44 * value. x' is therefore always non-negative, whatever the value of x.
45 * Taking as initial sample value x' = 0 corresponds to x = -M,
46 * which is always smaller than any possible value of x.
48 * The big parameter trick is used in the main tableau and
49 * also in the context tableau if isl_context_lex is used.
50 * In this case, each tableaus has its own big parameter.
51 * Before doing any real work, we check if all the parameters
52 * happen to be non-negative. If so, we drop the column corresponding
53 * to M from the initial context tableau.
54 * If isl_context_gbr is used, then the big parameter trick is only
55 * used in the main tableau.
59 struct isl_context_op
{
60 /* detect nonnegative parameters in context and mark them in tab */
61 struct isl_tab
*(*detect_nonnegative_parameters
)(
62 struct isl_context
*context
, struct isl_tab
*tab
);
63 /* return temporary reference to basic set representation of context */
64 struct isl_basic_set
*(*peek_basic_set
)(struct isl_context
*context
);
65 /* return temporary reference to tableau representation of context */
66 struct isl_tab
*(*peek_tab
)(struct isl_context
*context
);
67 /* add equality; check is 1 if eq may not be valid;
68 * update is 1 if we may want to call ineq_sign on context later.
70 void (*add_eq
)(struct isl_context
*context
, isl_int
*eq
,
71 int check
, int update
);
72 /* add inequality; check is 1 if ineq may not be valid;
73 * update is 1 if we may want to call ineq_sign on context later.
75 void (*add_ineq
)(struct isl_context
*context
, isl_int
*ineq
,
76 int check
, int update
);
77 /* check sign of ineq based on previous information.
78 * strict is 1 if saturation should be treated as a positive sign.
80 enum isl_tab_row_sign (*ineq_sign
)(struct isl_context
*context
,
81 isl_int
*ineq
, int strict
);
82 /* check if inequality maintains feasibility */
83 int (*test_ineq
)(struct isl_context
*context
, isl_int
*ineq
);
84 /* return index of a div that corresponds to "div" */
85 int (*get_div
)(struct isl_context
*context
, struct isl_tab
*tab
,
87 /* add div "div" to context and return non-negativity */
88 int (*add_div
)(struct isl_context
*context
, struct isl_vec
*div
);
89 int (*detect_equalities
)(struct isl_context
*context
,
91 /* return row index of "best" split */
92 int (*best_split
)(struct isl_context
*context
, struct isl_tab
*tab
);
93 /* check if context has already been determined to be empty */
94 int (*is_empty
)(struct isl_context
*context
);
95 /* check if context is still usable */
96 int (*is_ok
)(struct isl_context
*context
);
97 /* save a copy/snapshot of context */
98 void *(*save
)(struct isl_context
*context
);
99 /* restore saved context */
100 void (*restore
)(struct isl_context
*context
, void *);
101 /* invalidate context */
102 void (*invalidate
)(struct isl_context
*context
);
104 void (*free
)(struct isl_context
*context
);
108 struct isl_context_op
*op
;
111 struct isl_context_lex
{
112 struct isl_context context
;
116 struct isl_partial_sol
{
118 struct isl_basic_set
*dom
;
121 struct isl_partial_sol
*next
;
125 struct isl_sol_callback
{
126 struct isl_tab_callback callback
;
130 /* isl_sol is an interface for constructing a solution to
131 * a parametric integer linear programming problem.
132 * Every time the algorithm reaches a state where a solution
133 * can be read off from the tableau (including cases where the tableau
134 * is empty), the function "add" is called on the isl_sol passed
135 * to find_solutions_main.
137 * The context tableau is owned by isl_sol and is updated incrementally.
139 * There are currently two implementations of this interface,
140 * isl_sol_map, which simply collects the solutions in an isl_map
141 * and (optionally) the parts of the context where there is no solution
143 * isl_sol_for, which calls a user-defined function for each part of
152 struct isl_context
*context
;
153 struct isl_partial_sol
*partial
;
154 void (*add
)(struct isl_sol
*sol
,
155 struct isl_basic_set
*dom
, struct isl_mat
*M
);
156 void (*add_empty
)(struct isl_sol
*sol
, struct isl_basic_set
*bset
);
157 void (*free
)(struct isl_sol
*sol
);
158 struct isl_sol_callback dec_level
;
161 static void sol_free(struct isl_sol
*sol
)
163 struct isl_partial_sol
*partial
, *next
;
166 for (partial
= sol
->partial
; partial
; partial
= next
) {
167 next
= partial
->next
;
168 isl_basic_set_free(partial
->dom
);
169 isl_mat_free(partial
->M
);
175 /* Push a partial solution represented by a domain and mapping M
176 * onto the stack of partial solutions.
178 static void sol_push_sol(struct isl_sol
*sol
,
179 struct isl_basic_set
*dom
, struct isl_mat
*M
)
181 struct isl_partial_sol
*partial
;
183 if (sol
->error
|| !dom
)
186 partial
= isl_alloc_type(dom
->ctx
, struct isl_partial_sol
);
190 partial
->level
= sol
->level
;
193 partial
->next
= sol
->partial
;
195 sol
->partial
= partial
;
199 isl_basic_set_free(dom
);
203 /* Pop one partial solution from the partial solution stack and
204 * pass it on to sol->add or sol->add_empty.
206 static void sol_pop_one(struct isl_sol
*sol
)
208 struct isl_partial_sol
*partial
;
210 partial
= sol
->partial
;
211 sol
->partial
= partial
->next
;
214 sol
->add(sol
, partial
->dom
, partial
->M
);
216 sol
->add_empty(sol
, partial
->dom
);
220 /* Return a fresh copy of the domain represented by the context tableau.
222 static struct isl_basic_set
*sol_domain(struct isl_sol
*sol
)
224 struct isl_basic_set
*bset
;
229 bset
= isl_basic_set_dup(sol
->context
->op
->peek_basic_set(sol
->context
));
230 bset
= isl_basic_set_update_from_tab(bset
,
231 sol
->context
->op
->peek_tab(sol
->context
));
236 /* Check whether two partial solutions have the same mapping, where n_div
237 * is the number of divs that the two partial solutions have in common.
239 static int same_solution(struct isl_partial_sol
*s1
, struct isl_partial_sol
*s2
,
245 if (!s1
->M
!= !s2
->M
)
250 dim
= isl_basic_set_total_dim(s1
->dom
) - s1
->dom
->n_div
;
252 for (i
= 0; i
< s1
->M
->n_row
; ++i
) {
253 if (isl_seq_first_non_zero(s1
->M
->row
[i
]+1+dim
+n_div
,
254 s1
->M
->n_col
-1-dim
-n_div
) != -1)
256 if (isl_seq_first_non_zero(s2
->M
->row
[i
]+1+dim
+n_div
,
257 s2
->M
->n_col
-1-dim
-n_div
) != -1)
259 if (!isl_seq_eq(s1
->M
->row
[i
], s2
->M
->row
[i
], 1+dim
+n_div
))
265 /* Pop all solutions from the partial solution stack that were pushed onto
266 * the stack at levels that are deeper than the current level.
267 * If the two topmost elements on the stack have the same level
268 * and represent the same solution, then their domains are combined.
269 * This combined domain is the same as the current context domain
270 * as sol_pop is called each time we move back to a higher level.
272 static void sol_pop(struct isl_sol
*sol
)
274 struct isl_partial_sol
*partial
;
280 if (sol
->level
== 0) {
281 for (partial
= sol
->partial
; partial
; partial
= sol
->partial
)
286 partial
= sol
->partial
;
290 if (partial
->level
<= sol
->level
)
293 if (partial
->next
&& partial
->next
->level
== partial
->level
) {
294 n_div
= isl_basic_set_dim(
295 sol
->context
->op
->peek_basic_set(sol
->context
),
298 if (!same_solution(partial
, partial
->next
, n_div
)) {
302 struct isl_basic_set
*bset
;
304 bset
= sol_domain(sol
);
306 isl_basic_set_free(partial
->next
->dom
);
307 partial
->next
->dom
= bset
;
308 partial
->next
->level
= sol
->level
;
310 sol
->partial
= partial
->next
;
311 isl_basic_set_free(partial
->dom
);
312 isl_mat_free(partial
->M
);
319 static void sol_dec_level(struct isl_sol
*sol
)
329 static int sol_dec_level_wrap(struct isl_tab_callback
*cb
)
331 struct isl_sol_callback
*callback
= (struct isl_sol_callback
*)cb
;
333 sol_dec_level(callback
->sol
);
335 return callback
->sol
->error
? -1 : 0;
338 /* Move down to next level and push callback onto context tableau
339 * to decrease the level again when it gets rolled back across
340 * the current state. That is, dec_level will be called with
341 * the context tableau in the same state as it is when inc_level
344 static void sol_inc_level(struct isl_sol
*sol
)
352 tab
= sol
->context
->op
->peek_tab(sol
->context
);
353 if (isl_tab_push_callback(tab
, &sol
->dec_level
.callback
) < 0)
357 static void scale_rows(struct isl_mat
*mat
, isl_int m
, int n_row
)
361 if (isl_int_is_one(m
))
364 for (i
= 0; i
< n_row
; ++i
)
365 isl_seq_scale(mat
->row
[i
], mat
->row
[i
], m
, mat
->n_col
);
368 /* Add the solution identified by the tableau and the context tableau.
370 * The layout of the variables is as follows.
371 * tab->n_var is equal to the total number of variables in the input
372 * map (including divs that were copied from the context)
373 * + the number of extra divs constructed
374 * Of these, the first tab->n_param and the last tab->n_div variables
375 * correspond to the variables in the context, i.e.,
376 * tab->n_param + tab->n_div = context_tab->n_var
377 * tab->n_param is equal to the number of parameters and input
378 * dimensions in the input map
379 * tab->n_div is equal to the number of divs in the context
381 * If there is no solution, then call add_empty with a basic set
382 * that corresponds to the context tableau. (If add_empty is NULL,
385 * If there is a solution, then first construct a matrix that maps
386 * all dimensions of the context to the output variables, i.e.,
387 * the output dimensions in the input map.
388 * The divs in the input map (if any) that do not correspond to any
389 * div in the context do not appear in the solution.
390 * The algorithm will make sure that they have an integer value,
391 * but these values themselves are of no interest.
392 * We have to be careful not to drop or rearrange any divs in the
393 * context because that would change the meaning of the matrix.
395 * To extract the value of the output variables, it should be noted
396 * that we always use a big parameter M in the main tableau and so
397 * the variable stored in this tableau is not an output variable x itself, but
398 * x' = M + x (in case of minimization)
400 * x' = M - x (in case of maximization)
401 * If x' appears in a column, then its optimal value is zero,
402 * which means that the optimal value of x is an unbounded number
403 * (-M for minimization and M for maximization).
404 * We currently assume that the output dimensions in the original map
405 * are bounded, so this cannot occur.
406 * Similarly, when x' appears in a row, then the coefficient of M in that
407 * row is necessarily 1.
408 * If the row in the tableau represents
409 * d x' = c + d M + e(y)
410 * then, in case of minimization, the corresponding row in the matrix
413 * with a d = m, the (updated) common denominator of the matrix.
414 * In case of maximization, the row will be
417 static void sol_add(struct isl_sol
*sol
, struct isl_tab
*tab
)
419 struct isl_basic_set
*bset
= NULL
;
420 struct isl_mat
*mat
= NULL
;
425 if (sol
->error
|| !tab
)
428 if (tab
->empty
&& !sol
->add_empty
)
431 bset
= sol_domain(sol
);
434 sol_push_sol(sol
, bset
, NULL
);
440 mat
= isl_mat_alloc(tab
->mat
->ctx
, 1 + sol
->n_out
,
441 1 + tab
->n_param
+ tab
->n_div
);
447 isl_seq_clr(mat
->row
[0] + 1, mat
->n_col
- 1);
448 isl_int_set_si(mat
->row
[0][0], 1);
449 for (row
= 0; row
< sol
->n_out
; ++row
) {
450 int i
= tab
->n_param
+ row
;
453 isl_seq_clr(mat
->row
[1 + row
], mat
->n_col
);
454 if (!tab
->var
[i
].is_row
) {
456 isl_die(mat
->ctx
, isl_error_invalid
,
457 "unbounded optimum", goto error2
);
461 r
= tab
->var
[i
].index
;
463 isl_int_ne(tab
->mat
->row
[r
][2], tab
->mat
->row
[r
][0]))
464 isl_die(mat
->ctx
, isl_error_invalid
,
465 "unbounded optimum", goto error2
);
466 isl_int_gcd(m
, mat
->row
[0][0], tab
->mat
->row
[r
][0]);
467 isl_int_divexact(m
, tab
->mat
->row
[r
][0], m
);
468 scale_rows(mat
, m
, 1 + row
);
469 isl_int_divexact(m
, mat
->row
[0][0], tab
->mat
->row
[r
][0]);
470 isl_int_mul(mat
->row
[1 + row
][0], m
, tab
->mat
->row
[r
][1]);
471 for (j
= 0; j
< tab
->n_param
; ++j
) {
473 if (tab
->var
[j
].is_row
)
475 col
= tab
->var
[j
].index
;
476 isl_int_mul(mat
->row
[1 + row
][1 + j
], m
,
477 tab
->mat
->row
[r
][off
+ col
]);
479 for (j
= 0; j
< tab
->n_div
; ++j
) {
481 if (tab
->var
[tab
->n_var
- tab
->n_div
+j
].is_row
)
483 col
= tab
->var
[tab
->n_var
- tab
->n_div
+j
].index
;
484 isl_int_mul(mat
->row
[1 + row
][1 + tab
->n_param
+ j
], m
,
485 tab
->mat
->row
[r
][off
+ col
]);
488 isl_seq_neg(mat
->row
[1 + row
], mat
->row
[1 + row
],
494 sol_push_sol(sol
, bset
, mat
);
499 isl_basic_set_free(bset
);
507 struct isl_set
*empty
;
510 static void sol_map_free(struct isl_sol_map
*sol_map
)
514 if (sol_map
->sol
.context
)
515 sol_map
->sol
.context
->op
->free(sol_map
->sol
.context
);
516 isl_map_free(sol_map
->map
);
517 isl_set_free(sol_map
->empty
);
521 static void sol_map_free_wrap(struct isl_sol
*sol
)
523 sol_map_free((struct isl_sol_map
*)sol
);
526 /* This function is called for parts of the context where there is
527 * no solution, with "bset" corresponding to the context tableau.
528 * Simply add the basic set to the set "empty".
530 static void sol_map_add_empty(struct isl_sol_map
*sol
,
531 struct isl_basic_set
*bset
)
535 isl_assert(bset
->ctx
, sol
->empty
, goto error
);
537 sol
->empty
= isl_set_grow(sol
->empty
, 1);
538 bset
= isl_basic_set_simplify(bset
);
539 bset
= isl_basic_set_finalize(bset
);
540 sol
->empty
= isl_set_add_basic_set(sol
->empty
, isl_basic_set_copy(bset
));
543 isl_basic_set_free(bset
);
546 isl_basic_set_free(bset
);
550 static void sol_map_add_empty_wrap(struct isl_sol
*sol
,
551 struct isl_basic_set
*bset
)
553 sol_map_add_empty((struct isl_sol_map
*)sol
, bset
);
556 /* Add bset to sol's empty, but only if we are actually collecting
559 static void sol_map_add_empty_if_needed(struct isl_sol_map
*sol
,
560 struct isl_basic_set
*bset
)
563 sol_map_add_empty(sol
, bset
);
565 isl_basic_set_free(bset
);
568 /* Given a basic map "dom" that represents the context and an affine
569 * matrix "M" that maps the dimensions of the context to the
570 * output variables, construct a basic map with the same parameters
571 * and divs as the context, the dimensions of the context as input
572 * dimensions and a number of output dimensions that is equal to
573 * the number of output dimensions in the input map.
575 * The constraints and divs of the context are simply copied
576 * from "dom". For each row
580 * is added, with d the common denominator of M.
582 static void sol_map_add(struct isl_sol_map
*sol
,
583 struct isl_basic_set
*dom
, struct isl_mat
*M
)
586 struct isl_basic_map
*bmap
= NULL
;
587 isl_basic_set
*context_bset
;
595 if (sol
->sol
.error
|| !dom
|| !M
)
598 n_out
= sol
->sol
.n_out
;
599 n_eq
= dom
->n_eq
+ n_out
;
600 n_ineq
= dom
->n_ineq
;
602 nparam
= isl_basic_set_total_dim(dom
) - n_div
;
603 total
= isl_map_dim(sol
->map
, isl_dim_all
);
604 bmap
= isl_basic_map_alloc_dim(isl_map_get_dim(sol
->map
),
605 n_div
, n_eq
, 2 * n_div
+ n_ineq
);
608 if (sol
->sol
.rational
)
609 ISL_F_SET(bmap
, ISL_BASIC_MAP_RATIONAL
);
610 for (i
= 0; i
< dom
->n_div
; ++i
) {
611 int k
= isl_basic_map_alloc_div(bmap
);
614 isl_seq_cpy(bmap
->div
[k
], dom
->div
[i
], 1 + 1 + nparam
);
615 isl_seq_clr(bmap
->div
[k
] + 1 + 1 + nparam
, total
- nparam
);
616 isl_seq_cpy(bmap
->div
[k
] + 1 + 1 + total
,
617 dom
->div
[i
] + 1 + 1 + nparam
, i
);
619 for (i
= 0; i
< dom
->n_eq
; ++i
) {
620 int k
= isl_basic_map_alloc_equality(bmap
);
623 isl_seq_cpy(bmap
->eq
[k
], dom
->eq
[i
], 1 + nparam
);
624 isl_seq_clr(bmap
->eq
[k
] + 1 + nparam
, total
- nparam
);
625 isl_seq_cpy(bmap
->eq
[k
] + 1 + total
,
626 dom
->eq
[i
] + 1 + nparam
, n_div
);
628 for (i
= 0; i
< dom
->n_ineq
; ++i
) {
629 int k
= isl_basic_map_alloc_inequality(bmap
);
632 isl_seq_cpy(bmap
->ineq
[k
], dom
->ineq
[i
], 1 + nparam
);
633 isl_seq_clr(bmap
->ineq
[k
] + 1 + nparam
, total
- nparam
);
634 isl_seq_cpy(bmap
->ineq
[k
] + 1 + total
,
635 dom
->ineq
[i
] + 1 + nparam
, n_div
);
637 for (i
= 0; i
< M
->n_row
- 1; ++i
) {
638 int k
= isl_basic_map_alloc_equality(bmap
);
641 isl_seq_cpy(bmap
->eq
[k
], M
->row
[1 + i
], 1 + nparam
);
642 isl_seq_clr(bmap
->eq
[k
] + 1 + nparam
, n_out
);
643 isl_int_neg(bmap
->eq
[k
][1 + nparam
+ i
], M
->row
[0][0]);
644 isl_seq_cpy(bmap
->eq
[k
] + 1 + nparam
+ n_out
,
645 M
->row
[1 + i
] + 1 + nparam
, n_div
);
647 bmap
= isl_basic_map_simplify(bmap
);
648 bmap
= isl_basic_map_finalize(bmap
);
649 sol
->map
= isl_map_grow(sol
->map
, 1);
650 sol
->map
= isl_map_add_basic_map(sol
->map
, bmap
);
653 isl_basic_set_free(dom
);
657 isl_basic_set_free(dom
);
659 isl_basic_map_free(bmap
);
663 static void sol_map_add_wrap(struct isl_sol
*sol
,
664 struct isl_basic_set
*dom
, struct isl_mat
*M
)
666 sol_map_add((struct isl_sol_map
*)sol
, dom
, M
);
670 /* Store the "parametric constant" of row "row" of tableau "tab" in "line",
671 * i.e., the constant term and the coefficients of all variables that
672 * appear in the context tableau.
673 * Note that the coefficient of the big parameter M is NOT copied.
674 * The context tableau may not have a big parameter and even when it
675 * does, it is a different big parameter.
677 static void get_row_parameter_line(struct isl_tab
*tab
, int row
, isl_int
*line
)
680 unsigned off
= 2 + tab
->M
;
682 isl_int_set(line
[0], tab
->mat
->row
[row
][1]);
683 for (i
= 0; i
< tab
->n_param
; ++i
) {
684 if (tab
->var
[i
].is_row
)
685 isl_int_set_si(line
[1 + i
], 0);
687 int col
= tab
->var
[i
].index
;
688 isl_int_set(line
[1 + i
], tab
->mat
->row
[row
][off
+ col
]);
691 for (i
= 0; i
< tab
->n_div
; ++i
) {
692 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
693 isl_int_set_si(line
[1 + tab
->n_param
+ i
], 0);
695 int col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
696 isl_int_set(line
[1 + tab
->n_param
+ i
],
697 tab
->mat
->row
[row
][off
+ col
]);
702 /* Check if rows "row1" and "row2" have identical "parametric constants",
703 * as explained above.
704 * In this case, we also insist that the coefficients of the big parameter
705 * be the same as the values of the constants will only be the same
706 * if these coefficients are also the same.
708 static int identical_parameter_line(struct isl_tab
*tab
, int row1
, int row2
)
711 unsigned off
= 2 + tab
->M
;
713 if (isl_int_ne(tab
->mat
->row
[row1
][1], tab
->mat
->row
[row2
][1]))
716 if (tab
->M
&& isl_int_ne(tab
->mat
->row
[row1
][2],
717 tab
->mat
->row
[row2
][2]))
720 for (i
= 0; i
< tab
->n_param
+ tab
->n_div
; ++i
) {
721 int pos
= i
< tab
->n_param
? i
:
722 tab
->n_var
- tab
->n_div
+ i
- tab
->n_param
;
725 if (tab
->var
[pos
].is_row
)
727 col
= tab
->var
[pos
].index
;
728 if (isl_int_ne(tab
->mat
->row
[row1
][off
+ col
],
729 tab
->mat
->row
[row2
][off
+ col
]))
735 /* Return an inequality that expresses that the "parametric constant"
736 * should be non-negative.
737 * This function is only called when the coefficient of the big parameter
740 static struct isl_vec
*get_row_parameter_ineq(struct isl_tab
*tab
, int row
)
742 struct isl_vec
*ineq
;
744 ineq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_param
+ tab
->n_div
);
748 get_row_parameter_line(tab
, row
, ineq
->el
);
750 ineq
= isl_vec_normalize(ineq
);
755 /* Return a integer division for use in a parametric cut based on the given row.
756 * In particular, let the parametric constant of the row be
760 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
761 * The div returned is equal to
763 * floor(\sum_i {-a_i} y_i) = floor((\sum_i (-a_i mod d) y_i)/d)
765 static struct isl_vec
*get_row_parameter_div(struct isl_tab
*tab
, int row
)
769 div
= isl_vec_alloc(tab
->mat
->ctx
, 1 + 1 + tab
->n_param
+ tab
->n_div
);
773 isl_int_set(div
->el
[0], tab
->mat
->row
[row
][0]);
774 get_row_parameter_line(tab
, row
, div
->el
+ 1);
775 div
= isl_vec_normalize(div
);
776 isl_seq_neg(div
->el
+ 1, div
->el
+ 1, div
->size
- 1);
777 isl_seq_fdiv_r(div
->el
+ 1, div
->el
+ 1, div
->el
[0], div
->size
- 1);
782 /* Return a integer division for use in transferring an integrality constraint
784 * In particular, let the parametric constant of the row be
788 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
789 * The the returned div is equal to
791 * floor(\sum_i {a_i} y_i) = floor((\sum_i (a_i mod d) y_i)/d)
793 static struct isl_vec
*get_row_split_div(struct isl_tab
*tab
, int row
)
797 div
= isl_vec_alloc(tab
->mat
->ctx
, 1 + 1 + tab
->n_param
+ tab
->n_div
);
801 isl_int_set(div
->el
[0], tab
->mat
->row
[row
][0]);
802 get_row_parameter_line(tab
, row
, div
->el
+ 1);
803 div
= isl_vec_normalize(div
);
804 isl_seq_fdiv_r(div
->el
+ 1, div
->el
+ 1, div
->el
[0], div
->size
- 1);
809 /* Construct and return an inequality that expresses an upper bound
811 * In particular, if the div is given by
815 * then the inequality expresses
819 static struct isl_vec
*ineq_for_div(struct isl_basic_set
*bset
, unsigned div
)
823 struct isl_vec
*ineq
;
828 total
= isl_basic_set_total_dim(bset
);
829 div_pos
= 1 + total
- bset
->n_div
+ div
;
831 ineq
= isl_vec_alloc(bset
->ctx
, 1 + total
);
835 isl_seq_cpy(ineq
->el
, bset
->div
[div
] + 1, 1 + total
);
836 isl_int_neg(ineq
->el
[div_pos
], bset
->div
[div
][0]);
840 /* Given a row in the tableau and a div that was created
841 * using get_row_split_div and that been constrained to equality, i.e.,
843 * d = floor(\sum_i {a_i} y_i) = \sum_i {a_i} y_i
845 * replace the expression "\sum_i {a_i} y_i" in the row by d,
846 * i.e., we subtract "\sum_i {a_i} y_i" and add 1 d.
847 * The coefficients of the non-parameters in the tableau have been
848 * verified to be integral. We can therefore simply replace coefficient b
849 * by floor(b). For the coefficients of the parameters we have
850 * floor(a_i) = a_i - {a_i}, while for the other coefficients, we have
853 static struct isl_tab
*set_row_cst_to_div(struct isl_tab
*tab
, int row
, int div
)
855 isl_seq_fdiv_q(tab
->mat
->row
[row
] + 1, tab
->mat
->row
[row
] + 1,
856 tab
->mat
->row
[row
][0], 1 + tab
->M
+ tab
->n_col
);
858 isl_int_set_si(tab
->mat
->row
[row
][0], 1);
860 if (tab
->var
[tab
->n_var
- tab
->n_div
+ div
].is_row
) {
861 int drow
= tab
->var
[tab
->n_var
- tab
->n_div
+ div
].index
;
863 isl_assert(tab
->mat
->ctx
,
864 isl_int_is_one(tab
->mat
->row
[drow
][0]), goto error
);
865 isl_seq_combine(tab
->mat
->row
[row
] + 1,
866 tab
->mat
->ctx
->one
, tab
->mat
->row
[row
] + 1,
867 tab
->mat
->ctx
->one
, tab
->mat
->row
[drow
] + 1,
868 1 + tab
->M
+ tab
->n_col
);
870 int dcol
= tab
->var
[tab
->n_var
- tab
->n_div
+ div
].index
;
872 isl_int_set_si(tab
->mat
->row
[row
][2 + tab
->M
+ dcol
], 1);
881 /* Check if the (parametric) constant of the given row is obviously
882 * negative, meaning that we don't need to consult the context tableau.
883 * If there is a big parameter and its coefficient is non-zero,
884 * then this coefficient determines the outcome.
885 * Otherwise, we check whether the constant is negative and
886 * all non-zero coefficients of parameters are negative and
887 * belong to non-negative parameters.
889 static int is_obviously_neg(struct isl_tab
*tab
, int row
)
893 unsigned off
= 2 + tab
->M
;
896 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
898 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
902 if (isl_int_is_nonneg(tab
->mat
->row
[row
][1]))
904 for (i
= 0; i
< tab
->n_param
; ++i
) {
905 /* Eliminated parameter */
906 if (tab
->var
[i
].is_row
)
908 col
= tab
->var
[i
].index
;
909 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
911 if (!tab
->var
[i
].is_nonneg
)
913 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ col
]))
916 for (i
= 0; i
< tab
->n_div
; ++i
) {
917 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
919 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
920 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
922 if (!tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_nonneg
)
924 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ col
]))
930 /* Check if the (parametric) constant of the given row is obviously
931 * non-negative, meaning that we don't need to consult the context tableau.
932 * If there is a big parameter and its coefficient is non-zero,
933 * then this coefficient determines the outcome.
934 * Otherwise, we check whether the constant is non-negative and
935 * all non-zero coefficients of parameters are positive and
936 * belong to non-negative parameters.
938 static int is_obviously_nonneg(struct isl_tab
*tab
, int row
)
942 unsigned off
= 2 + tab
->M
;
945 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
947 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
951 if (isl_int_is_neg(tab
->mat
->row
[row
][1]))
953 for (i
= 0; i
< tab
->n_param
; ++i
) {
954 /* Eliminated parameter */
955 if (tab
->var
[i
].is_row
)
957 col
= tab
->var
[i
].index
;
958 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
960 if (!tab
->var
[i
].is_nonneg
)
962 if (isl_int_is_neg(tab
->mat
->row
[row
][off
+ col
]))
965 for (i
= 0; i
< tab
->n_div
; ++i
) {
966 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
968 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
969 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
971 if (!tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_nonneg
)
973 if (isl_int_is_neg(tab
->mat
->row
[row
][off
+ col
]))
979 /* Given a row r and two columns, return the column that would
980 * lead to the lexicographically smallest increment in the sample
981 * solution when leaving the basis in favor of the row.
982 * Pivoting with column c will increment the sample value by a non-negative
983 * constant times a_{V,c}/a_{r,c}, with a_{V,c} the elements of column c
984 * corresponding to the non-parametric variables.
985 * If variable v appears in a column c_v, the a_{v,c} = 1 iff c = c_v,
986 * with all other entries in this virtual row equal to zero.
987 * If variable v appears in a row, then a_{v,c} is the element in column c
990 * Let v be the first variable with a_{v,c1}/a_{r,c1} != a_{v,c2}/a_{r,c2}.
991 * Then if a_{v,c1}/a_{r,c1} < a_{v,c2}/a_{r,c2}, i.e.,
992 * a_{v,c2} a_{r,c1} - a_{v,c1} a_{r,c2} > 0, c1 results in the minimal
993 * increment. Otherwise, it's c2.
995 static int lexmin_col_pair(struct isl_tab
*tab
,
996 int row
, int col1
, int col2
, isl_int tmp
)
1001 tr
= tab
->mat
->row
[row
] + 2 + tab
->M
;
1003 for (i
= tab
->n_param
; i
< tab
->n_var
- tab
->n_div
; ++i
) {
1007 if (!tab
->var
[i
].is_row
) {
1008 if (tab
->var
[i
].index
== col1
)
1010 if (tab
->var
[i
].index
== col2
)
1015 if (tab
->var
[i
].index
== row
)
1018 r
= tab
->mat
->row
[tab
->var
[i
].index
] + 2 + tab
->M
;
1019 s1
= isl_int_sgn(r
[col1
]);
1020 s2
= isl_int_sgn(r
[col2
]);
1021 if (s1
== 0 && s2
== 0)
1028 isl_int_mul(tmp
, r
[col2
], tr
[col1
]);
1029 isl_int_submul(tmp
, r
[col1
], tr
[col2
]);
1030 if (isl_int_is_pos(tmp
))
1032 if (isl_int_is_neg(tmp
))
1038 /* Given a row in the tableau, find and return the column that would
1039 * result in the lexicographically smallest, but positive, increment
1040 * in the sample point.
1041 * If there is no such column, then return tab->n_col.
1042 * If anything goes wrong, return -1.
1044 static int lexmin_pivot_col(struct isl_tab
*tab
, int row
)
1047 int col
= tab
->n_col
;
1051 tr
= tab
->mat
->row
[row
] + 2 + tab
->M
;
1055 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
1056 if (tab
->col_var
[j
] >= 0 &&
1057 (tab
->col_var
[j
] < tab
->n_param
||
1058 tab
->col_var
[j
] >= tab
->n_var
- tab
->n_div
))
1061 if (!isl_int_is_pos(tr
[j
]))
1064 if (col
== tab
->n_col
)
1067 col
= lexmin_col_pair(tab
, row
, col
, j
, tmp
);
1068 isl_assert(tab
->mat
->ctx
, col
>= 0, goto error
);
1078 /* Return the first known violated constraint, i.e., a non-negative
1079 * constraint that currently has an either obviously negative value
1080 * or a previously determined to be negative value.
1082 * If any constraint has a negative coefficient for the big parameter,
1083 * if any, then we return one of these first.
1085 static int first_neg(struct isl_tab
*tab
)
1090 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
1091 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
1093 if (!isl_int_is_neg(tab
->mat
->row
[row
][2]))
1096 tab
->row_sign
[row
] = isl_tab_row_neg
;
1099 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
1100 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
1102 if (tab
->row_sign
) {
1103 if (tab
->row_sign
[row
] == 0 &&
1104 is_obviously_neg(tab
, row
))
1105 tab
->row_sign
[row
] = isl_tab_row_neg
;
1106 if (tab
->row_sign
[row
] != isl_tab_row_neg
)
1108 } else if (!is_obviously_neg(tab
, row
))
1115 /* Resolve all known or obviously violated constraints through pivoting.
1116 * In particular, as long as we can find any violated constraint, we
1117 * look for a pivoting column that would result in the lexicographically
1118 * smallest increment in the sample point. If there is no such column
1119 * then the tableau is infeasible.
1121 static struct isl_tab
*restore_lexmin(struct isl_tab
*tab
) WARN_UNUSED
;
1122 static struct isl_tab
*restore_lexmin(struct isl_tab
*tab
)
1130 while ((row
= first_neg(tab
)) != -1) {
1131 col
= lexmin_pivot_col(tab
, row
);
1132 if (col
>= tab
->n_col
) {
1133 if (isl_tab_mark_empty(tab
) < 0)
1139 if (isl_tab_pivot(tab
, row
, col
) < 0)
1148 /* Given a row that represents an equality, look for an appropriate
1150 * In particular, if there are any non-zero coefficients among
1151 * the non-parameter variables, then we take the last of these
1152 * variables. Eliminating this variable in terms of the other
1153 * variables and/or parameters does not influence the property
1154 * that all column in the initial tableau are lexicographically
1155 * positive. The row corresponding to the eliminated variable
1156 * will only have non-zero entries below the diagonal of the
1157 * initial tableau. That is, we transform
1163 * If there is no such non-parameter variable, then we are dealing with
1164 * pure parameter equality and we pick any parameter with coefficient 1 or -1
1165 * for elimination. This will ensure that the eliminated parameter
1166 * always has an integer value whenever all the other parameters are integral.
1167 * If there is no such parameter then we return -1.
1169 static int last_var_col_or_int_par_col(struct isl_tab
*tab
, int row
)
1171 unsigned off
= 2 + tab
->M
;
1174 for (i
= tab
->n_var
- tab
->n_div
- 1; i
>= 0 && i
>= tab
->n_param
; --i
) {
1176 if (tab
->var
[i
].is_row
)
1178 col
= tab
->var
[i
].index
;
1179 if (col
<= tab
->n_dead
)
1181 if (!isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1184 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
1185 if (isl_int_is_one(tab
->mat
->row
[row
][off
+ i
]))
1187 if (isl_int_is_negone(tab
->mat
->row
[row
][off
+ i
]))
1193 /* Add an equality that is known to be valid to the tableau.
1194 * We first check if we can eliminate a variable or a parameter.
1195 * If not, we add the equality as two inequalities.
1196 * In this case, the equality was a pure parameter equality and there
1197 * is no need to resolve any constraint violations.
1199 static struct isl_tab
*add_lexmin_valid_eq(struct isl_tab
*tab
, isl_int
*eq
)
1206 r
= isl_tab_add_row(tab
, eq
);
1210 r
= tab
->con
[r
].index
;
1211 i
= last_var_col_or_int_par_col(tab
, r
);
1213 tab
->con
[r
].is_nonneg
= 1;
1214 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1216 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1217 r
= isl_tab_add_row(tab
, eq
);
1220 tab
->con
[r
].is_nonneg
= 1;
1221 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1224 if (isl_tab_pivot(tab
, r
, i
) < 0)
1226 if (isl_tab_kill_col(tab
, i
) < 0)
1237 /* Check if the given row is a pure constant.
1239 static int is_constant(struct isl_tab
*tab
, int row
)
1241 unsigned off
= 2 + tab
->M
;
1243 return isl_seq_first_non_zero(tab
->mat
->row
[row
] + off
+ tab
->n_dead
,
1244 tab
->n_col
- tab
->n_dead
) == -1;
1247 /* Add an equality that may or may not be valid to the tableau.
1248 * If the resulting row is a pure constant, then it must be zero.
1249 * Otherwise, the resulting tableau is empty.
1251 * If the row is not a pure constant, then we add two inequalities,
1252 * each time checking that they can be satisfied.
1253 * In the end we try to use one of the two constraints to eliminate
1256 static struct isl_tab
*add_lexmin_eq(struct isl_tab
*tab
, isl_int
*eq
) WARN_UNUSED
;
1257 static struct isl_tab
*add_lexmin_eq(struct isl_tab
*tab
, isl_int
*eq
)
1261 struct isl_tab_undo
*snap
;
1265 snap
= isl_tab_snap(tab
);
1266 r1
= isl_tab_add_row(tab
, eq
);
1269 tab
->con
[r1
].is_nonneg
= 1;
1270 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r1
]) < 0)
1273 row
= tab
->con
[r1
].index
;
1274 if (is_constant(tab
, row
)) {
1275 if (!isl_int_is_zero(tab
->mat
->row
[row
][1]) ||
1276 (tab
->M
&& !isl_int_is_zero(tab
->mat
->row
[row
][2]))) {
1277 if (isl_tab_mark_empty(tab
) < 0)
1281 if (isl_tab_rollback(tab
, snap
) < 0)
1286 tab
= restore_lexmin(tab
);
1287 if (!tab
|| tab
->empty
)
1290 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1292 r2
= isl_tab_add_row(tab
, eq
);
1295 tab
->con
[r2
].is_nonneg
= 1;
1296 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r2
]) < 0)
1299 tab
= restore_lexmin(tab
);
1300 if (!tab
|| tab
->empty
)
1303 if (!tab
->con
[r1
].is_row
) {
1304 if (isl_tab_kill_col(tab
, tab
->con
[r1
].index
) < 0)
1306 } else if (!tab
->con
[r2
].is_row
) {
1307 if (isl_tab_kill_col(tab
, tab
->con
[r2
].index
) < 0)
1309 } else if (isl_int_is_zero(tab
->mat
->row
[tab
->con
[r1
].index
][1])) {
1310 unsigned off
= 2 + tab
->M
;
1312 int row
= tab
->con
[r1
].index
;
1313 i
= isl_seq_first_non_zero(tab
->mat
->row
[row
]+off
+tab
->n_dead
,
1314 tab
->n_col
- tab
->n_dead
);
1316 if (isl_tab_pivot(tab
, row
, tab
->n_dead
+ i
) < 0)
1318 if (isl_tab_kill_col(tab
, tab
->n_dead
+ i
) < 0)
1324 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, eq
);
1325 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1327 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1328 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, eq
);
1329 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1330 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1342 /* Add an inequality to the tableau, resolving violations using
1345 static struct isl_tab
*add_lexmin_ineq(struct isl_tab
*tab
, isl_int
*ineq
)
1352 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, ineq
);
1353 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1358 r
= isl_tab_add_row(tab
, ineq
);
1361 tab
->con
[r
].is_nonneg
= 1;
1362 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1364 if (isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
)) {
1365 if (isl_tab_mark_redundant(tab
, tab
->con
[r
].index
) < 0)
1370 tab
= restore_lexmin(tab
);
1371 if (tab
&& !tab
->empty
&& tab
->con
[r
].is_row
&&
1372 isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
))
1373 if (isl_tab_mark_redundant(tab
, tab
->con
[r
].index
) < 0)
1381 /* Check if the coefficients of the parameters are all integral.
1383 static int integer_parameter(struct isl_tab
*tab
, int row
)
1387 unsigned off
= 2 + tab
->M
;
1389 for (i
= 0; i
< tab
->n_param
; ++i
) {
1390 /* Eliminated parameter */
1391 if (tab
->var
[i
].is_row
)
1393 col
= tab
->var
[i
].index
;
1394 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ col
],
1395 tab
->mat
->row
[row
][0]))
1398 for (i
= 0; i
< tab
->n_div
; ++i
) {
1399 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
1401 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
1402 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ col
],
1403 tab
->mat
->row
[row
][0]))
1409 /* Check if the coefficients of the non-parameter variables are all integral.
1411 static int integer_variable(struct isl_tab
*tab
, int row
)
1414 unsigned off
= 2 + tab
->M
;
1416 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
1417 if (tab
->col_var
[i
] >= 0 &&
1418 (tab
->col_var
[i
] < tab
->n_param
||
1419 tab
->col_var
[i
] >= tab
->n_var
- tab
->n_div
))
1421 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ i
],
1422 tab
->mat
->row
[row
][0]))
1428 /* Check if the constant term is integral.
1430 static int integer_constant(struct isl_tab
*tab
, int row
)
1432 return isl_int_is_divisible_by(tab
->mat
->row
[row
][1],
1433 tab
->mat
->row
[row
][0]);
1436 #define I_CST 1 << 0
1437 #define I_PAR 1 << 1
1438 #define I_VAR 1 << 2
1440 /* Check for next (non-parameter) variable after "var" (first if var == -1)
1441 * that is non-integer and therefore requires a cut and return
1442 * the index of the variable.
1443 * For parametric tableaus, there are three parts in a row,
1444 * the constant, the coefficients of the parameters and the rest.
1445 * For each part, we check whether the coefficients in that part
1446 * are all integral and if so, set the corresponding flag in *f.
1447 * If the constant and the parameter part are integral, then the
1448 * current sample value is integral and no cut is required
1449 * (irrespective of whether the variable part is integral).
1451 static int next_non_integer_var(struct isl_tab
*tab
, int var
, int *f
)
1453 var
= var
< 0 ? tab
->n_param
: var
+ 1;
1455 for (; var
< tab
->n_var
- tab
->n_div
; ++var
) {
1458 if (!tab
->var
[var
].is_row
)
1460 row
= tab
->var
[var
].index
;
1461 if (integer_constant(tab
, row
))
1462 ISL_FL_SET(flags
, I_CST
);
1463 if (integer_parameter(tab
, row
))
1464 ISL_FL_SET(flags
, I_PAR
);
1465 if (ISL_FL_ISSET(flags
, I_CST
) && ISL_FL_ISSET(flags
, I_PAR
))
1467 if (integer_variable(tab
, row
))
1468 ISL_FL_SET(flags
, I_VAR
);
1475 /* Check for first (non-parameter) variable that is non-integer and
1476 * therefore requires a cut and return the corresponding row.
1477 * For parametric tableaus, there are three parts in a row,
1478 * the constant, the coefficients of the parameters and the rest.
1479 * For each part, we check whether the coefficients in that part
1480 * are all integral and if so, set the corresponding flag in *f.
1481 * If the constant and the parameter part are integral, then the
1482 * current sample value is integral and no cut is required
1483 * (irrespective of whether the variable part is integral).
1485 static int first_non_integer_row(struct isl_tab
*tab
, int *f
)
1487 int var
= next_non_integer_var(tab
, -1, f
);
1489 return var
< 0 ? -1 : tab
->var
[var
].index
;
1492 /* Add a (non-parametric) cut to cut away the non-integral sample
1493 * value of the given row.
1495 * If the row is given by
1497 * m r = f + \sum_i a_i y_i
1501 * c = - {-f/m} + \sum_i {a_i/m} y_i >= 0
1503 * The big parameter, if any, is ignored, since it is assumed to be big
1504 * enough to be divisible by any integer.
1505 * If the tableau is actually a parametric tableau, then this function
1506 * is only called when all coefficients of the parameters are integral.
1507 * The cut therefore has zero coefficients for the parameters.
1509 * The current value is known to be negative, so row_sign, if it
1510 * exists, is set accordingly.
1512 * Return the row of the cut or -1.
1514 static int add_cut(struct isl_tab
*tab
, int row
)
1519 unsigned off
= 2 + tab
->M
;
1521 if (isl_tab_extend_cons(tab
, 1) < 0)
1523 r
= isl_tab_allocate_con(tab
);
1527 r_row
= tab
->mat
->row
[tab
->con
[r
].index
];
1528 isl_int_set(r_row
[0], tab
->mat
->row
[row
][0]);
1529 isl_int_neg(r_row
[1], tab
->mat
->row
[row
][1]);
1530 isl_int_fdiv_r(r_row
[1], r_row
[1], tab
->mat
->row
[row
][0]);
1531 isl_int_neg(r_row
[1], r_row
[1]);
1533 isl_int_set_si(r_row
[2], 0);
1534 for (i
= 0; i
< tab
->n_col
; ++i
)
1535 isl_int_fdiv_r(r_row
[off
+ i
],
1536 tab
->mat
->row
[row
][off
+ i
], tab
->mat
->row
[row
][0]);
1538 tab
->con
[r
].is_nonneg
= 1;
1539 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1542 tab
->row_sign
[tab
->con
[r
].index
] = isl_tab_row_neg
;
1544 return tab
->con
[r
].index
;
1547 /* Given a non-parametric tableau, add cuts until an integer
1548 * sample point is obtained or until the tableau is determined
1549 * to be integer infeasible.
1550 * As long as there is any non-integer value in the sample point,
1551 * we add appropriate cuts, if possible, for each of these
1552 * non-integer values and then resolve the violated
1553 * cut constraints using restore_lexmin.
1554 * If one of the corresponding rows is equal to an integral
1555 * combination of variables/constraints plus a non-integral constant,
1556 * then there is no way to obtain an integer point and we return
1557 * a tableau that is marked empty.
1559 static struct isl_tab
*cut_to_integer_lexmin(struct isl_tab
*tab
)
1570 while ((var
= next_non_integer_var(tab
, -1, &flags
)) != -1) {
1572 if (ISL_FL_ISSET(flags
, I_VAR
)) {
1573 if (isl_tab_mark_empty(tab
) < 0)
1577 row
= tab
->var
[var
].index
;
1578 row
= add_cut(tab
, row
);
1581 } while ((var
= next_non_integer_var(tab
, var
, &flags
)) != -1);
1582 tab
= restore_lexmin(tab
);
1583 if (!tab
|| tab
->empty
)
1592 /* Check whether all the currently active samples also satisfy the inequality
1593 * "ineq" (treated as an equality if eq is set).
1594 * Remove those samples that do not.
1596 static struct isl_tab
*check_samples(struct isl_tab
*tab
, isl_int
*ineq
, int eq
)
1604 isl_assert(tab
->mat
->ctx
, tab
->bmap
, goto error
);
1605 isl_assert(tab
->mat
->ctx
, tab
->samples
, goto error
);
1606 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
, goto error
);
1609 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
1611 isl_seq_inner_product(ineq
, tab
->samples
->row
[i
],
1612 1 + tab
->n_var
, &v
);
1613 sgn
= isl_int_sgn(v
);
1614 if (eq
? (sgn
== 0) : (sgn
>= 0))
1616 tab
= isl_tab_drop_sample(tab
, i
);
1628 /* Check whether the sample value of the tableau is finite,
1629 * i.e., either the tableau does not use a big parameter, or
1630 * all values of the variables are equal to the big parameter plus
1631 * some constant. This constant is the actual sample value.
1633 static int sample_is_finite(struct isl_tab
*tab
)
1640 for (i
= 0; i
< tab
->n_var
; ++i
) {
1642 if (!tab
->var
[i
].is_row
)
1644 row
= tab
->var
[i
].index
;
1645 if (isl_int_ne(tab
->mat
->row
[row
][0], tab
->mat
->row
[row
][2]))
1651 /* Check if the context tableau of sol has any integer points.
1652 * Leave tab in empty state if no integer point can be found.
1653 * If an integer point can be found and if moreover it is finite,
1654 * then it is added to the list of sample values.
1656 * This function is only called when none of the currently active sample
1657 * values satisfies the most recently added constraint.
1659 static struct isl_tab
*check_integer_feasible(struct isl_tab
*tab
)
1661 struct isl_tab_undo
*snap
;
1667 snap
= isl_tab_snap(tab
);
1668 if (isl_tab_push_basis(tab
) < 0)
1671 tab
= cut_to_integer_lexmin(tab
);
1675 if (!tab
->empty
&& sample_is_finite(tab
)) {
1676 struct isl_vec
*sample
;
1678 sample
= isl_tab_get_sample_value(tab
);
1680 tab
= isl_tab_add_sample(tab
, sample
);
1683 if (!tab
->empty
&& isl_tab_rollback(tab
, snap
) < 0)
1692 /* Check if any of the currently active sample values satisfies
1693 * the inequality "ineq" (an equality if eq is set).
1695 static int tab_has_valid_sample(struct isl_tab
*tab
, isl_int
*ineq
, int eq
)
1703 isl_assert(tab
->mat
->ctx
, tab
->bmap
, return -1);
1704 isl_assert(tab
->mat
->ctx
, tab
->samples
, return -1);
1705 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
, return -1);
1708 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
1710 isl_seq_inner_product(ineq
, tab
->samples
->row
[i
],
1711 1 + tab
->n_var
, &v
);
1712 sgn
= isl_int_sgn(v
);
1713 if (eq
? (sgn
== 0) : (sgn
>= 0))
1718 return i
< tab
->n_sample
;
1721 /* Add a div specified by "div" to the tableau "tab" and return
1722 * 1 if the div is obviously non-negative.
1724 static int context_tab_add_div(struct isl_tab
*tab
, struct isl_vec
*div
,
1725 int (*add_ineq
)(void *user
, isl_int
*), void *user
)
1729 struct isl_mat
*samples
;
1732 r
= isl_tab_add_div(tab
, div
, add_ineq
, user
);
1735 nonneg
= tab
->var
[r
].is_nonneg
;
1736 tab
->var
[r
].frozen
= 1;
1738 samples
= isl_mat_extend(tab
->samples
,
1739 tab
->n_sample
, 1 + tab
->n_var
);
1740 tab
->samples
= samples
;
1743 for (i
= tab
->n_outside
; i
< samples
->n_row
; ++i
) {
1744 isl_seq_inner_product(div
->el
+ 1, samples
->row
[i
],
1745 div
->size
- 1, &samples
->row
[i
][samples
->n_col
- 1]);
1746 isl_int_fdiv_q(samples
->row
[i
][samples
->n_col
- 1],
1747 samples
->row
[i
][samples
->n_col
- 1], div
->el
[0]);
1753 /* Add a div specified by "div" to both the main tableau and
1754 * the context tableau. In case of the main tableau, we only
1755 * need to add an extra div. In the context tableau, we also
1756 * need to express the meaning of the div.
1757 * Return the index of the div or -1 if anything went wrong.
1759 static int add_div(struct isl_tab
*tab
, struct isl_context
*context
,
1760 struct isl_vec
*div
)
1765 if ((nonneg
= context
->op
->add_div(context
, div
)) < 0)
1768 if (!context
->op
->is_ok(context
))
1771 if (isl_tab_extend_vars(tab
, 1) < 0)
1773 r
= isl_tab_allocate_var(tab
);
1777 tab
->var
[r
].is_nonneg
= 1;
1778 tab
->var
[r
].frozen
= 1;
1781 return tab
->n_div
- 1;
1783 context
->op
->invalidate(context
);
1787 static int find_div(struct isl_tab
*tab
, isl_int
*div
, isl_int denom
)
1790 unsigned total
= isl_basic_map_total_dim(tab
->bmap
);
1792 for (i
= 0; i
< tab
->bmap
->n_div
; ++i
) {
1793 if (isl_int_ne(tab
->bmap
->div
[i
][0], denom
))
1795 if (!isl_seq_eq(tab
->bmap
->div
[i
] + 1, div
, 1 + total
))
1802 /* Return the index of a div that corresponds to "div".
1803 * We first check if we already have such a div and if not, we create one.
1805 static int get_div(struct isl_tab
*tab
, struct isl_context
*context
,
1806 struct isl_vec
*div
)
1809 struct isl_tab
*context_tab
= context
->op
->peek_tab(context
);
1814 d
= find_div(context_tab
, div
->el
+ 1, div
->el
[0]);
1818 return add_div(tab
, context
, div
);
1821 /* Add a parametric cut to cut away the non-integral sample value
1823 * Let a_i be the coefficients of the constant term and the parameters
1824 * and let b_i be the coefficients of the variables or constraints
1825 * in basis of the tableau.
1826 * Let q be the div q = floor(\sum_i {-a_i} y_i).
1828 * The cut is expressed as
1830 * c = \sum_i -{-a_i} y_i + \sum_i {b_i} x_i + q >= 0
1832 * If q did not already exist in the context tableau, then it is added first.
1833 * If q is in a column of the main tableau then the "+ q" can be accomplished
1834 * by setting the corresponding entry to the denominator of the constraint.
1835 * If q happens to be in a row of the main tableau, then the corresponding
1836 * row needs to be added instead (taking care of the denominators).
1837 * Note that this is very unlikely, but perhaps not entirely impossible.
1839 * The current value of the cut is known to be negative (or at least
1840 * non-positive), so row_sign is set accordingly.
1842 * Return the row of the cut or -1.
1844 static int add_parametric_cut(struct isl_tab
*tab
, int row
,
1845 struct isl_context
*context
)
1847 struct isl_vec
*div
;
1854 unsigned off
= 2 + tab
->M
;
1859 div
= get_row_parameter_div(tab
, row
);
1864 d
= context
->op
->get_div(context
, tab
, div
);
1868 if (isl_tab_extend_cons(tab
, 1) < 0)
1870 r
= isl_tab_allocate_con(tab
);
1874 r_row
= tab
->mat
->row
[tab
->con
[r
].index
];
1875 isl_int_set(r_row
[0], tab
->mat
->row
[row
][0]);
1876 isl_int_neg(r_row
[1], tab
->mat
->row
[row
][1]);
1877 isl_int_fdiv_r(r_row
[1], r_row
[1], tab
->mat
->row
[row
][0]);
1878 isl_int_neg(r_row
[1], r_row
[1]);
1880 isl_int_set_si(r_row
[2], 0);
1881 for (i
= 0; i
< tab
->n_param
; ++i
) {
1882 if (tab
->var
[i
].is_row
)
1884 col
= tab
->var
[i
].index
;
1885 isl_int_neg(r_row
[off
+ col
], tab
->mat
->row
[row
][off
+ col
]);
1886 isl_int_fdiv_r(r_row
[off
+ col
], r_row
[off
+ col
],
1887 tab
->mat
->row
[row
][0]);
1888 isl_int_neg(r_row
[off
+ col
], r_row
[off
+ col
]);
1890 for (i
= 0; i
< tab
->n_div
; ++i
) {
1891 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
1893 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
1894 isl_int_neg(r_row
[off
+ col
], tab
->mat
->row
[row
][off
+ col
]);
1895 isl_int_fdiv_r(r_row
[off
+ col
], r_row
[off
+ col
],
1896 tab
->mat
->row
[row
][0]);
1897 isl_int_neg(r_row
[off
+ col
], r_row
[off
+ col
]);
1899 for (i
= 0; i
< tab
->n_col
; ++i
) {
1900 if (tab
->col_var
[i
] >= 0 &&
1901 (tab
->col_var
[i
] < tab
->n_param
||
1902 tab
->col_var
[i
] >= tab
->n_var
- tab
->n_div
))
1904 isl_int_fdiv_r(r_row
[off
+ i
],
1905 tab
->mat
->row
[row
][off
+ i
], tab
->mat
->row
[row
][0]);
1907 if (tab
->var
[tab
->n_var
- tab
->n_div
+ d
].is_row
) {
1909 int d_row
= tab
->var
[tab
->n_var
- tab
->n_div
+ d
].index
;
1911 isl_int_gcd(gcd
, tab
->mat
->row
[d_row
][0], r_row
[0]);
1912 isl_int_divexact(r_row
[0], r_row
[0], gcd
);
1913 isl_int_divexact(gcd
, tab
->mat
->row
[d_row
][0], gcd
);
1914 isl_seq_combine(r_row
+ 1, gcd
, r_row
+ 1,
1915 r_row
[0], tab
->mat
->row
[d_row
] + 1,
1916 off
- 1 + tab
->n_col
);
1917 isl_int_mul(r_row
[0], r_row
[0], tab
->mat
->row
[d_row
][0]);
1920 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ d
].index
;
1921 isl_int_set(r_row
[off
+ col
], tab
->mat
->row
[row
][0]);
1924 tab
->con
[r
].is_nonneg
= 1;
1925 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1928 tab
->row_sign
[tab
->con
[r
].index
] = isl_tab_row_neg
;
1932 row
= tab
->con
[r
].index
;
1934 if (d
>= n
&& context
->op
->detect_equalities(context
, tab
) < 0)
1940 /* Construct a tableau for bmap that can be used for computing
1941 * the lexicographic minimum (or maximum) of bmap.
1942 * If not NULL, then dom is the domain where the minimum
1943 * should be computed. In this case, we set up a parametric
1944 * tableau with row signs (initialized to "unknown").
1945 * If M is set, then the tableau will use a big parameter.
1946 * If max is set, then a maximum should be computed instead of a minimum.
1947 * This means that for each variable x, the tableau will contain the variable
1948 * x' = M - x, rather than x' = M + x. This in turn means that the coefficient
1949 * of the variables in all constraints are negated prior to adding them
1952 static struct isl_tab
*tab_for_lexmin(struct isl_basic_map
*bmap
,
1953 struct isl_basic_set
*dom
, unsigned M
, int max
)
1956 struct isl_tab
*tab
;
1958 tab
= isl_tab_alloc(bmap
->ctx
, 2 * bmap
->n_eq
+ bmap
->n_ineq
+ 1,
1959 isl_basic_map_total_dim(bmap
), M
);
1963 tab
->rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
1965 tab
->n_param
= isl_basic_set_total_dim(dom
) - dom
->n_div
;
1966 tab
->n_div
= dom
->n_div
;
1967 tab
->row_sign
= isl_calloc_array(bmap
->ctx
,
1968 enum isl_tab_row_sign
, tab
->mat
->n_row
);
1972 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
)) {
1973 if (isl_tab_mark_empty(tab
) < 0)
1978 for (i
= tab
->n_param
; i
< tab
->n_var
- tab
->n_div
; ++i
) {
1979 tab
->var
[i
].is_nonneg
= 1;
1980 tab
->var
[i
].frozen
= 1;
1982 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1984 isl_seq_neg(bmap
->eq
[i
] + 1 + tab
->n_param
,
1985 bmap
->eq
[i
] + 1 + tab
->n_param
,
1986 tab
->n_var
- tab
->n_param
- tab
->n_div
);
1987 tab
= add_lexmin_valid_eq(tab
, bmap
->eq
[i
]);
1989 isl_seq_neg(bmap
->eq
[i
] + 1 + tab
->n_param
,
1990 bmap
->eq
[i
] + 1 + tab
->n_param
,
1991 tab
->n_var
- tab
->n_param
- tab
->n_div
);
1992 if (!tab
|| tab
->empty
)
1996 tab
= restore_lexmin(tab
);
1997 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1999 isl_seq_neg(bmap
->ineq
[i
] + 1 + tab
->n_param
,
2000 bmap
->ineq
[i
] + 1 + tab
->n_param
,
2001 tab
->n_var
- tab
->n_param
- tab
->n_div
);
2002 tab
= add_lexmin_ineq(tab
, bmap
->ineq
[i
]);
2004 isl_seq_neg(bmap
->ineq
[i
] + 1 + tab
->n_param
,
2005 bmap
->ineq
[i
] + 1 + tab
->n_param
,
2006 tab
->n_var
- tab
->n_param
- tab
->n_div
);
2007 if (!tab
|| tab
->empty
)
2016 /* Given a main tableau where more than one row requires a split,
2017 * determine and return the "best" row to split on.
2019 * Given two rows in the main tableau, if the inequality corresponding
2020 * to the first row is redundant with respect to that of the second row
2021 * in the current tableau, then it is better to split on the second row,
2022 * since in the positive part, both row will be positive.
2023 * (In the negative part a pivot will have to be performed and just about
2024 * anything can happen to the sign of the other row.)
2026 * As a simple heuristic, we therefore select the row that makes the most
2027 * of the other rows redundant.
2029 * Perhaps it would also be useful to look at the number of constraints
2030 * that conflict with any given constraint.
2032 static int best_split(struct isl_tab
*tab
, struct isl_tab
*context_tab
)
2034 struct isl_tab_undo
*snap
;
2040 if (isl_tab_extend_cons(context_tab
, 2) < 0)
2043 snap
= isl_tab_snap(context_tab
);
2045 for (split
= tab
->n_redundant
; split
< tab
->n_row
; ++split
) {
2046 struct isl_tab_undo
*snap2
;
2047 struct isl_vec
*ineq
= NULL
;
2051 if (!isl_tab_var_from_row(tab
, split
)->is_nonneg
)
2053 if (tab
->row_sign
[split
] != isl_tab_row_any
)
2056 ineq
= get_row_parameter_ineq(tab
, split
);
2059 ok
= isl_tab_add_ineq(context_tab
, ineq
->el
) >= 0;
2064 snap2
= isl_tab_snap(context_tab
);
2066 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
2067 struct isl_tab_var
*var
;
2071 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
2073 if (tab
->row_sign
[row
] != isl_tab_row_any
)
2076 ineq
= get_row_parameter_ineq(tab
, row
);
2079 ok
= isl_tab_add_ineq(context_tab
, ineq
->el
) >= 0;
2083 var
= &context_tab
->con
[context_tab
->n_con
- 1];
2084 if (!context_tab
->empty
&&
2085 !isl_tab_min_at_most_neg_one(context_tab
, var
))
2087 if (isl_tab_rollback(context_tab
, snap2
) < 0)
2090 if (best
== -1 || r
> best_r
) {
2094 if (isl_tab_rollback(context_tab
, snap
) < 0)
2101 static struct isl_basic_set
*context_lex_peek_basic_set(
2102 struct isl_context
*context
)
2104 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2107 return isl_tab_peek_bset(clex
->tab
);
2110 static struct isl_tab
*context_lex_peek_tab(struct isl_context
*context
)
2112 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2116 static void context_lex_extend(struct isl_context
*context
, int n
)
2118 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2121 if (isl_tab_extend_cons(clex
->tab
, n
) >= 0)
2123 isl_tab_free(clex
->tab
);
2127 static void context_lex_add_eq(struct isl_context
*context
, isl_int
*eq
,
2128 int check
, int update
)
2130 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2131 if (isl_tab_extend_cons(clex
->tab
, 2) < 0)
2133 clex
->tab
= add_lexmin_eq(clex
->tab
, eq
);
2135 int v
= tab_has_valid_sample(clex
->tab
, eq
, 1);
2139 clex
->tab
= check_integer_feasible(clex
->tab
);
2142 clex
->tab
= check_samples(clex
->tab
, eq
, 1);
2145 isl_tab_free(clex
->tab
);
2149 static void context_lex_add_ineq(struct isl_context
*context
, isl_int
*ineq
,
2150 int check
, int update
)
2152 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2153 if (isl_tab_extend_cons(clex
->tab
, 1) < 0)
2155 clex
->tab
= add_lexmin_ineq(clex
->tab
, ineq
);
2157 int v
= tab_has_valid_sample(clex
->tab
, ineq
, 0);
2161 clex
->tab
= check_integer_feasible(clex
->tab
);
2164 clex
->tab
= check_samples(clex
->tab
, ineq
, 0);
2167 isl_tab_free(clex
->tab
);
2171 static int context_lex_add_ineq_wrap(void *user
, isl_int
*ineq
)
2173 struct isl_context
*context
= (struct isl_context
*)user
;
2174 context_lex_add_ineq(context
, ineq
, 0, 0);
2175 return context
->op
->is_ok(context
) ? 0 : -1;
2178 /* Check which signs can be obtained by "ineq" on all the currently
2179 * active sample values. See row_sign for more information.
2181 static enum isl_tab_row_sign
tab_ineq_sign(struct isl_tab
*tab
, isl_int
*ineq
,
2187 enum isl_tab_row_sign res
= isl_tab_row_unknown
;
2189 isl_assert(tab
->mat
->ctx
, tab
->samples
, return isl_tab_row_unknown
);
2190 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
,
2191 return isl_tab_row_unknown
);
2194 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
2195 isl_seq_inner_product(tab
->samples
->row
[i
], ineq
,
2196 1 + tab
->n_var
, &tmp
);
2197 sgn
= isl_int_sgn(tmp
);
2198 if (sgn
> 0 || (sgn
== 0 && strict
)) {
2199 if (res
== isl_tab_row_unknown
)
2200 res
= isl_tab_row_pos
;
2201 if (res
== isl_tab_row_neg
)
2202 res
= isl_tab_row_any
;
2205 if (res
== isl_tab_row_unknown
)
2206 res
= isl_tab_row_neg
;
2207 if (res
== isl_tab_row_pos
)
2208 res
= isl_tab_row_any
;
2210 if (res
== isl_tab_row_any
)
2218 static enum isl_tab_row_sign
context_lex_ineq_sign(struct isl_context
*context
,
2219 isl_int
*ineq
, int strict
)
2221 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2222 return tab_ineq_sign(clex
->tab
, ineq
, strict
);
2225 /* Check whether "ineq" can be added to the tableau without rendering
2228 static int context_lex_test_ineq(struct isl_context
*context
, isl_int
*ineq
)
2230 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2231 struct isl_tab_undo
*snap
;
2237 if (isl_tab_extend_cons(clex
->tab
, 1) < 0)
2240 snap
= isl_tab_snap(clex
->tab
);
2241 if (isl_tab_push_basis(clex
->tab
) < 0)
2243 clex
->tab
= add_lexmin_ineq(clex
->tab
, ineq
);
2244 clex
->tab
= check_integer_feasible(clex
->tab
);
2247 feasible
= !clex
->tab
->empty
;
2248 if (isl_tab_rollback(clex
->tab
, snap
) < 0)
2254 static int context_lex_get_div(struct isl_context
*context
, struct isl_tab
*tab
,
2255 struct isl_vec
*div
)
2257 return get_div(tab
, context
, div
);
2260 /* Add a div specified by "div" to the context tableau and return
2261 * 1 if the div is obviously non-negative.
2262 * context_tab_add_div will always return 1, because all variables
2263 * in a isl_context_lex tableau are non-negative.
2264 * However, if we are using a big parameter in the context, then this only
2265 * reflects the non-negativity of the variable used to _encode_ the
2266 * div, i.e., div' = M + div, so we can't draw any conclusions.
2268 static int context_lex_add_div(struct isl_context
*context
, struct isl_vec
*div
)
2270 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2272 nonneg
= context_tab_add_div(clex
->tab
, div
,
2273 context_lex_add_ineq_wrap
, context
);
2281 static int context_lex_detect_equalities(struct isl_context
*context
,
2282 struct isl_tab
*tab
)
2287 static int context_lex_best_split(struct isl_context
*context
,
2288 struct isl_tab
*tab
)
2290 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2291 struct isl_tab_undo
*snap
;
2294 snap
= isl_tab_snap(clex
->tab
);
2295 if (isl_tab_push_basis(clex
->tab
) < 0)
2297 r
= best_split(tab
, clex
->tab
);
2299 if (r
>= 0 && isl_tab_rollback(clex
->tab
, snap
) < 0)
2305 static int context_lex_is_empty(struct isl_context
*context
)
2307 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2310 return clex
->tab
->empty
;
2313 static void *context_lex_save(struct isl_context
*context
)
2315 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2316 struct isl_tab_undo
*snap
;
2318 snap
= isl_tab_snap(clex
->tab
);
2319 if (isl_tab_push_basis(clex
->tab
) < 0)
2321 if (isl_tab_save_samples(clex
->tab
) < 0)
2327 static void context_lex_restore(struct isl_context
*context
, void *save
)
2329 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2330 if (isl_tab_rollback(clex
->tab
, (struct isl_tab_undo
*)save
) < 0) {
2331 isl_tab_free(clex
->tab
);
2336 static int context_lex_is_ok(struct isl_context
*context
)
2338 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2342 /* For each variable in the context tableau, check if the variable can
2343 * only attain non-negative values. If so, mark the parameter as non-negative
2344 * in the main tableau. This allows for a more direct identification of some
2345 * cases of violated constraints.
2347 static struct isl_tab
*tab_detect_nonnegative_parameters(struct isl_tab
*tab
,
2348 struct isl_tab
*context_tab
)
2351 struct isl_tab_undo
*snap
;
2352 struct isl_vec
*ineq
= NULL
;
2353 struct isl_tab_var
*var
;
2356 if (context_tab
->n_var
== 0)
2359 ineq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + context_tab
->n_var
);
2363 if (isl_tab_extend_cons(context_tab
, 1) < 0)
2366 snap
= isl_tab_snap(context_tab
);
2369 isl_seq_clr(ineq
->el
, ineq
->size
);
2370 for (i
= 0; i
< context_tab
->n_var
; ++i
) {
2371 isl_int_set_si(ineq
->el
[1 + i
], 1);
2372 if (isl_tab_add_ineq(context_tab
, ineq
->el
) < 0)
2374 var
= &context_tab
->con
[context_tab
->n_con
- 1];
2375 if (!context_tab
->empty
&&
2376 !isl_tab_min_at_most_neg_one(context_tab
, var
)) {
2378 if (i
>= tab
->n_param
)
2379 j
= i
- tab
->n_param
+ tab
->n_var
- tab
->n_div
;
2380 tab
->var
[j
].is_nonneg
= 1;
2383 isl_int_set_si(ineq
->el
[1 + i
], 0);
2384 if (isl_tab_rollback(context_tab
, snap
) < 0)
2388 if (context_tab
->M
&& n
== context_tab
->n_var
) {
2389 context_tab
->mat
= isl_mat_drop_cols(context_tab
->mat
, 2, 1);
2401 static struct isl_tab
*context_lex_detect_nonnegative_parameters(
2402 struct isl_context
*context
, struct isl_tab
*tab
)
2404 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2405 struct isl_tab_undo
*snap
;
2410 snap
= isl_tab_snap(clex
->tab
);
2411 if (isl_tab_push_basis(clex
->tab
) < 0)
2414 tab
= tab_detect_nonnegative_parameters(tab
, clex
->tab
);
2416 if (isl_tab_rollback(clex
->tab
, snap
) < 0)
2425 static void context_lex_invalidate(struct isl_context
*context
)
2427 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2428 isl_tab_free(clex
->tab
);
2432 static void context_lex_free(struct isl_context
*context
)
2434 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2435 isl_tab_free(clex
->tab
);
2439 struct isl_context_op isl_context_lex_op
= {
2440 context_lex_detect_nonnegative_parameters
,
2441 context_lex_peek_basic_set
,
2442 context_lex_peek_tab
,
2444 context_lex_add_ineq
,
2445 context_lex_ineq_sign
,
2446 context_lex_test_ineq
,
2447 context_lex_get_div
,
2448 context_lex_add_div
,
2449 context_lex_detect_equalities
,
2450 context_lex_best_split
,
2451 context_lex_is_empty
,
2454 context_lex_restore
,
2455 context_lex_invalidate
,
2459 static struct isl_tab
*context_tab_for_lexmin(struct isl_basic_set
*bset
)
2461 struct isl_tab
*tab
;
2463 bset
= isl_basic_set_cow(bset
);
2466 tab
= tab_for_lexmin((struct isl_basic_map
*)bset
, NULL
, 1, 0);
2469 if (isl_tab_track_bset(tab
, bset
) < 0)
2471 tab
= isl_tab_init_samples(tab
);
2474 isl_basic_set_free(bset
);
2478 static struct isl_context
*isl_context_lex_alloc(struct isl_basic_set
*dom
)
2480 struct isl_context_lex
*clex
;
2485 clex
= isl_alloc_type(dom
->ctx
, struct isl_context_lex
);
2489 clex
->context
.op
= &isl_context_lex_op
;
2491 clex
->tab
= context_tab_for_lexmin(isl_basic_set_copy(dom
));
2492 clex
->tab
= restore_lexmin(clex
->tab
);
2493 clex
->tab
= check_integer_feasible(clex
->tab
);
2497 return &clex
->context
;
2499 clex
->context
.op
->free(&clex
->context
);
2503 struct isl_context_gbr
{
2504 struct isl_context context
;
2505 struct isl_tab
*tab
;
2506 struct isl_tab
*shifted
;
2507 struct isl_tab
*cone
;
2510 static struct isl_tab
*context_gbr_detect_nonnegative_parameters(
2511 struct isl_context
*context
, struct isl_tab
*tab
)
2513 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2516 return tab_detect_nonnegative_parameters(tab
, cgbr
->tab
);
2519 static struct isl_basic_set
*context_gbr_peek_basic_set(
2520 struct isl_context
*context
)
2522 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2525 return isl_tab_peek_bset(cgbr
->tab
);
2528 static struct isl_tab
*context_gbr_peek_tab(struct isl_context
*context
)
2530 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2534 /* Initialize the "shifted" tableau of the context, which
2535 * contains the constraints of the original tableau shifted
2536 * by the sum of all negative coefficients. This ensures
2537 * that any rational point in the shifted tableau can
2538 * be rounded up to yield an integer point in the original tableau.
2540 static void gbr_init_shifted(struct isl_context_gbr
*cgbr
)
2543 struct isl_vec
*cst
;
2544 struct isl_basic_set
*bset
= isl_tab_peek_bset(cgbr
->tab
);
2545 unsigned dim
= isl_basic_set_total_dim(bset
);
2547 cst
= isl_vec_alloc(cgbr
->tab
->mat
->ctx
, bset
->n_ineq
);
2551 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2552 isl_int_set(cst
->el
[i
], bset
->ineq
[i
][0]);
2553 for (j
= 0; j
< dim
; ++j
) {
2554 if (!isl_int_is_neg(bset
->ineq
[i
][1 + j
]))
2556 isl_int_add(bset
->ineq
[i
][0], bset
->ineq
[i
][0],
2557 bset
->ineq
[i
][1 + j
]);
2561 cgbr
->shifted
= isl_tab_from_basic_set(bset
);
2563 for (i
= 0; i
< bset
->n_ineq
; ++i
)
2564 isl_int_set(bset
->ineq
[i
][0], cst
->el
[i
]);
2569 /* Check if the shifted tableau is non-empty, and if so
2570 * use the sample point to construct an integer point
2571 * of the context tableau.
2573 static struct isl_vec
*gbr_get_shifted_sample(struct isl_context_gbr
*cgbr
)
2575 struct isl_vec
*sample
;
2578 gbr_init_shifted(cgbr
);
2581 if (cgbr
->shifted
->empty
)
2582 return isl_vec_alloc(cgbr
->tab
->mat
->ctx
, 0);
2584 sample
= isl_tab_get_sample_value(cgbr
->shifted
);
2585 sample
= isl_vec_ceil(sample
);
2590 static struct isl_basic_set
*drop_constant_terms(struct isl_basic_set
*bset
)
2597 for (i
= 0; i
< bset
->n_eq
; ++i
)
2598 isl_int_set_si(bset
->eq
[i
][0], 0);
2600 for (i
= 0; i
< bset
->n_ineq
; ++i
)
2601 isl_int_set_si(bset
->ineq
[i
][0], 0);
2606 static int use_shifted(struct isl_context_gbr
*cgbr
)
2608 return cgbr
->tab
->bmap
->n_eq
== 0 && cgbr
->tab
->bmap
->n_div
== 0;
2611 static struct isl_vec
*gbr_get_sample(struct isl_context_gbr
*cgbr
)
2613 struct isl_basic_set
*bset
;
2614 struct isl_basic_set
*cone
;
2616 if (isl_tab_sample_is_integer(cgbr
->tab
))
2617 return isl_tab_get_sample_value(cgbr
->tab
);
2619 if (use_shifted(cgbr
)) {
2620 struct isl_vec
*sample
;
2622 sample
= gbr_get_shifted_sample(cgbr
);
2623 if (!sample
|| sample
->size
> 0)
2626 isl_vec_free(sample
);
2630 bset
= isl_tab_peek_bset(cgbr
->tab
);
2631 cgbr
->cone
= isl_tab_from_recession_cone(bset
, 0);
2634 if (isl_tab_track_bset(cgbr
->cone
, isl_basic_set_dup(bset
)) < 0)
2637 if (isl_tab_detect_implicit_equalities(cgbr
->cone
) < 0)
2640 if (cgbr
->cone
->n_dead
== cgbr
->cone
->n_col
) {
2641 struct isl_vec
*sample
;
2642 struct isl_tab_undo
*snap
;
2644 if (cgbr
->tab
->basis
) {
2645 if (cgbr
->tab
->basis
->n_col
!= 1 + cgbr
->tab
->n_var
) {
2646 isl_mat_free(cgbr
->tab
->basis
);
2647 cgbr
->tab
->basis
= NULL
;
2649 cgbr
->tab
->n_zero
= 0;
2650 cgbr
->tab
->n_unbounded
= 0;
2653 snap
= isl_tab_snap(cgbr
->tab
);
2655 sample
= isl_tab_sample(cgbr
->tab
);
2657 if (isl_tab_rollback(cgbr
->tab
, snap
) < 0) {
2658 isl_vec_free(sample
);
2665 cone
= isl_basic_set_dup(isl_tab_peek_bset(cgbr
->cone
));
2666 cone
= drop_constant_terms(cone
);
2667 cone
= isl_basic_set_update_from_tab(cone
, cgbr
->cone
);
2668 cone
= isl_basic_set_underlying_set(cone
);
2669 cone
= isl_basic_set_gauss(cone
, NULL
);
2671 bset
= isl_basic_set_dup(isl_tab_peek_bset(cgbr
->tab
));
2672 bset
= isl_basic_set_update_from_tab(bset
, cgbr
->tab
);
2673 bset
= isl_basic_set_underlying_set(bset
);
2674 bset
= isl_basic_set_gauss(bset
, NULL
);
2676 return isl_basic_set_sample_with_cone(bset
, cone
);
2679 static void check_gbr_integer_feasible(struct isl_context_gbr
*cgbr
)
2681 struct isl_vec
*sample
;
2686 if (cgbr
->tab
->empty
)
2689 sample
= gbr_get_sample(cgbr
);
2693 if (sample
->size
== 0) {
2694 isl_vec_free(sample
);
2695 if (isl_tab_mark_empty(cgbr
->tab
) < 0)
2700 cgbr
->tab
= isl_tab_add_sample(cgbr
->tab
, sample
);
2704 isl_tab_free(cgbr
->tab
);
2708 static struct isl_tab
*add_gbr_eq(struct isl_tab
*tab
, isl_int
*eq
)
2715 if (isl_tab_extend_cons(tab
, 2) < 0)
2718 if (isl_tab_add_eq(tab
, eq
) < 0)
2727 static void context_gbr_add_eq(struct isl_context
*context
, isl_int
*eq
,
2728 int check
, int update
)
2730 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2732 cgbr
->tab
= add_gbr_eq(cgbr
->tab
, eq
);
2734 if (cgbr
->cone
&& cgbr
->cone
->n_col
!= cgbr
->cone
->n_dead
) {
2735 if (isl_tab_extend_cons(cgbr
->cone
, 2) < 0)
2737 if (isl_tab_add_eq(cgbr
->cone
, eq
) < 0)
2742 int v
= tab_has_valid_sample(cgbr
->tab
, eq
, 1);
2746 check_gbr_integer_feasible(cgbr
);
2749 cgbr
->tab
= check_samples(cgbr
->tab
, eq
, 1);
2752 isl_tab_free(cgbr
->tab
);
2756 static void add_gbr_ineq(struct isl_context_gbr
*cgbr
, isl_int
*ineq
)
2761 if (isl_tab_extend_cons(cgbr
->tab
, 1) < 0)
2764 if (isl_tab_add_ineq(cgbr
->tab
, ineq
) < 0)
2767 if (cgbr
->shifted
&& !cgbr
->shifted
->empty
&& use_shifted(cgbr
)) {
2770 dim
= isl_basic_map_total_dim(cgbr
->tab
->bmap
);
2772 if (isl_tab_extend_cons(cgbr
->shifted
, 1) < 0)
2775 for (i
= 0; i
< dim
; ++i
) {
2776 if (!isl_int_is_neg(ineq
[1 + i
]))
2778 isl_int_add(ineq
[0], ineq
[0], ineq
[1 + i
]);
2781 if (isl_tab_add_ineq(cgbr
->shifted
, ineq
) < 0)
2784 for (i
= 0; i
< dim
; ++i
) {
2785 if (!isl_int_is_neg(ineq
[1 + i
]))
2787 isl_int_sub(ineq
[0], ineq
[0], ineq
[1 + i
]);
2791 if (cgbr
->cone
&& cgbr
->cone
->n_col
!= cgbr
->cone
->n_dead
) {
2792 if (isl_tab_extend_cons(cgbr
->cone
, 1) < 0)
2794 if (isl_tab_add_ineq(cgbr
->cone
, ineq
) < 0)
2800 isl_tab_free(cgbr
->tab
);
2804 static void context_gbr_add_ineq(struct isl_context
*context
, isl_int
*ineq
,
2805 int check
, int update
)
2807 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2809 add_gbr_ineq(cgbr
, ineq
);
2814 int v
= tab_has_valid_sample(cgbr
->tab
, ineq
, 0);
2818 check_gbr_integer_feasible(cgbr
);
2821 cgbr
->tab
= check_samples(cgbr
->tab
, ineq
, 0);
2824 isl_tab_free(cgbr
->tab
);
2828 static int context_gbr_add_ineq_wrap(void *user
, isl_int
*ineq
)
2830 struct isl_context
*context
= (struct isl_context
*)user
;
2831 context_gbr_add_ineq(context
, ineq
, 0, 0);
2832 return context
->op
->is_ok(context
) ? 0 : -1;
2835 static enum isl_tab_row_sign
context_gbr_ineq_sign(struct isl_context
*context
,
2836 isl_int
*ineq
, int strict
)
2838 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2839 return tab_ineq_sign(cgbr
->tab
, ineq
, strict
);
2842 /* Check whether "ineq" can be added to the tableau without rendering
2845 static int context_gbr_test_ineq(struct isl_context
*context
, isl_int
*ineq
)
2847 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2848 struct isl_tab_undo
*snap
;
2849 struct isl_tab_undo
*shifted_snap
= NULL
;
2850 struct isl_tab_undo
*cone_snap
= NULL
;
2856 if (isl_tab_extend_cons(cgbr
->tab
, 1) < 0)
2859 snap
= isl_tab_snap(cgbr
->tab
);
2861 shifted_snap
= isl_tab_snap(cgbr
->shifted
);
2863 cone_snap
= isl_tab_snap(cgbr
->cone
);
2864 add_gbr_ineq(cgbr
, ineq
);
2865 check_gbr_integer_feasible(cgbr
);
2868 feasible
= !cgbr
->tab
->empty
;
2869 if (isl_tab_rollback(cgbr
->tab
, snap
) < 0)
2872 if (isl_tab_rollback(cgbr
->shifted
, shifted_snap
))
2874 } else if (cgbr
->shifted
) {
2875 isl_tab_free(cgbr
->shifted
);
2876 cgbr
->shifted
= NULL
;
2879 if (isl_tab_rollback(cgbr
->cone
, cone_snap
))
2881 } else if (cgbr
->cone
) {
2882 isl_tab_free(cgbr
->cone
);
2889 /* Return the column of the last of the variables associated to
2890 * a column that has a non-zero coefficient.
2891 * This function is called in a context where only coefficients
2892 * of parameters or divs can be non-zero.
2894 static int last_non_zero_var_col(struct isl_tab
*tab
, isl_int
*p
)
2898 unsigned dim
= tab
->n_var
- tab
->n_param
- tab
->n_div
;
2900 if (tab
->n_var
== 0)
2903 for (i
= tab
->n_var
- 1; i
>= 0; --i
) {
2904 if (i
>= tab
->n_param
&& i
< tab
->n_var
- tab
->n_div
)
2906 if (tab
->var
[i
].is_row
)
2908 col
= tab
->var
[i
].index
;
2909 if (!isl_int_is_zero(p
[col
]))
2916 /* Look through all the recently added equalities in the context
2917 * to see if we can propagate any of them to the main tableau.
2919 * The newly added equalities in the context are encoded as pairs
2920 * of inequalities starting at inequality "first".
2922 * We tentatively add each of these equalities to the main tableau
2923 * and if this happens to result in a row with a final coefficient
2924 * that is one or negative one, we use it to kill a column
2925 * in the main tableau. Otherwise, we discard the tentatively
2928 static void propagate_equalities(struct isl_context_gbr
*cgbr
,
2929 struct isl_tab
*tab
, unsigned first
)
2932 struct isl_vec
*eq
= NULL
;
2934 eq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_var
);
2938 if (isl_tab_extend_cons(tab
, (cgbr
->tab
->bmap
->n_ineq
- first
)/2) < 0)
2941 isl_seq_clr(eq
->el
+ 1 + tab
->n_param
,
2942 tab
->n_var
- tab
->n_param
- tab
->n_div
);
2943 for (i
= first
; i
< cgbr
->tab
->bmap
->n_ineq
; i
+= 2) {
2946 struct isl_tab_undo
*snap
;
2947 snap
= isl_tab_snap(tab
);
2949 isl_seq_cpy(eq
->el
, cgbr
->tab
->bmap
->ineq
[i
], 1 + tab
->n_param
);
2950 isl_seq_cpy(eq
->el
+ 1 + tab
->n_var
- tab
->n_div
,
2951 cgbr
->tab
->bmap
->ineq
[i
] + 1 + tab
->n_param
,
2954 r
= isl_tab_add_row(tab
, eq
->el
);
2957 r
= tab
->con
[r
].index
;
2958 j
= last_non_zero_var_col(tab
, tab
->mat
->row
[r
] + 2 + tab
->M
);
2959 if (j
< 0 || j
< tab
->n_dead
||
2960 !isl_int_is_one(tab
->mat
->row
[r
][0]) ||
2961 (!isl_int_is_one(tab
->mat
->row
[r
][2 + tab
->M
+ j
]) &&
2962 !isl_int_is_negone(tab
->mat
->row
[r
][2 + tab
->M
+ j
]))) {
2963 if (isl_tab_rollback(tab
, snap
) < 0)
2967 if (isl_tab_pivot(tab
, r
, j
) < 0)
2969 if (isl_tab_kill_col(tab
, j
) < 0)
2972 tab
= restore_lexmin(tab
);
2980 isl_tab_free(cgbr
->tab
);
2984 static int context_gbr_detect_equalities(struct isl_context
*context
,
2985 struct isl_tab
*tab
)
2987 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2988 struct isl_ctx
*ctx
;
2990 enum isl_lp_result res
;
2993 ctx
= cgbr
->tab
->mat
->ctx
;
2996 struct isl_basic_set
*bset
= isl_tab_peek_bset(cgbr
->tab
);
2997 cgbr
->cone
= isl_tab_from_recession_cone(bset
, 0);
3000 if (isl_tab_track_bset(cgbr
->cone
, isl_basic_set_dup(bset
)) < 0)
3003 if (isl_tab_detect_implicit_equalities(cgbr
->cone
) < 0)
3006 n_ineq
= cgbr
->tab
->bmap
->n_ineq
;
3007 cgbr
->tab
= isl_tab_detect_equalities(cgbr
->tab
, cgbr
->cone
);
3008 if (cgbr
->tab
&& cgbr
->tab
->bmap
->n_ineq
> n_ineq
)
3009 propagate_equalities(cgbr
, tab
, n_ineq
);
3013 isl_tab_free(cgbr
->tab
);
3018 static int context_gbr_get_div(struct isl_context
*context
, struct isl_tab
*tab
,
3019 struct isl_vec
*div
)
3021 return get_div(tab
, context
, div
);
3024 static int context_gbr_add_div(struct isl_context
*context
, struct isl_vec
*div
)
3026 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3030 if (isl_tab_extend_cons(cgbr
->cone
, 3) < 0)
3032 if (isl_tab_extend_vars(cgbr
->cone
, 1) < 0)
3034 if (isl_tab_allocate_var(cgbr
->cone
) <0)
3037 cgbr
->cone
->bmap
= isl_basic_map_extend_dim(cgbr
->cone
->bmap
,
3038 isl_basic_map_get_dim(cgbr
->cone
->bmap
), 1, 0, 2);
3039 k
= isl_basic_map_alloc_div(cgbr
->cone
->bmap
);
3042 isl_seq_cpy(cgbr
->cone
->bmap
->div
[k
], div
->el
, div
->size
);
3043 if (isl_tab_push(cgbr
->cone
, isl_tab_undo_bmap_div
) < 0)
3046 return context_tab_add_div(cgbr
->tab
, div
,
3047 context_gbr_add_ineq_wrap
, context
);
3050 static int context_gbr_best_split(struct isl_context
*context
,
3051 struct isl_tab
*tab
)
3053 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3054 struct isl_tab_undo
*snap
;
3057 snap
= isl_tab_snap(cgbr
->tab
);
3058 r
= best_split(tab
, cgbr
->tab
);
3060 if (r
>= 0 && isl_tab_rollback(cgbr
->tab
, snap
) < 0)
3066 static int context_gbr_is_empty(struct isl_context
*context
)
3068 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3071 return cgbr
->tab
->empty
;
3074 struct isl_gbr_tab_undo
{
3075 struct isl_tab_undo
*tab_snap
;
3076 struct isl_tab_undo
*shifted_snap
;
3077 struct isl_tab_undo
*cone_snap
;
3080 static void *context_gbr_save(struct isl_context
*context
)
3082 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3083 struct isl_gbr_tab_undo
*snap
;
3085 snap
= isl_alloc_type(cgbr
->tab
->mat
->ctx
, struct isl_gbr_tab_undo
);
3089 snap
->tab_snap
= isl_tab_snap(cgbr
->tab
);
3090 if (isl_tab_save_samples(cgbr
->tab
) < 0)
3094 snap
->shifted_snap
= isl_tab_snap(cgbr
->shifted
);
3096 snap
->shifted_snap
= NULL
;
3099 snap
->cone_snap
= isl_tab_snap(cgbr
->cone
);
3101 snap
->cone_snap
= NULL
;
3109 static void context_gbr_restore(struct isl_context
*context
, void *save
)
3111 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3112 struct isl_gbr_tab_undo
*snap
= (struct isl_gbr_tab_undo
*)save
;
3115 if (isl_tab_rollback(cgbr
->tab
, snap
->tab_snap
) < 0) {
3116 isl_tab_free(cgbr
->tab
);
3120 if (snap
->shifted_snap
) {
3121 if (isl_tab_rollback(cgbr
->shifted
, snap
->shifted_snap
) < 0)
3123 } else if (cgbr
->shifted
) {
3124 isl_tab_free(cgbr
->shifted
);
3125 cgbr
->shifted
= NULL
;
3128 if (snap
->cone_snap
) {
3129 if (isl_tab_rollback(cgbr
->cone
, snap
->cone_snap
) < 0)
3131 } else if (cgbr
->cone
) {
3132 isl_tab_free(cgbr
->cone
);
3141 isl_tab_free(cgbr
->tab
);
3145 static int context_gbr_is_ok(struct isl_context
*context
)
3147 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3151 static void context_gbr_invalidate(struct isl_context
*context
)
3153 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3154 isl_tab_free(cgbr
->tab
);
3158 static void context_gbr_free(struct isl_context
*context
)
3160 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3161 isl_tab_free(cgbr
->tab
);
3162 isl_tab_free(cgbr
->shifted
);
3163 isl_tab_free(cgbr
->cone
);
3167 struct isl_context_op isl_context_gbr_op
= {
3168 context_gbr_detect_nonnegative_parameters
,
3169 context_gbr_peek_basic_set
,
3170 context_gbr_peek_tab
,
3172 context_gbr_add_ineq
,
3173 context_gbr_ineq_sign
,
3174 context_gbr_test_ineq
,
3175 context_gbr_get_div
,
3176 context_gbr_add_div
,
3177 context_gbr_detect_equalities
,
3178 context_gbr_best_split
,
3179 context_gbr_is_empty
,
3182 context_gbr_restore
,
3183 context_gbr_invalidate
,
3187 static struct isl_context
*isl_context_gbr_alloc(struct isl_basic_set
*dom
)
3189 struct isl_context_gbr
*cgbr
;
3194 cgbr
= isl_calloc_type(dom
->ctx
, struct isl_context_gbr
);
3198 cgbr
->context
.op
= &isl_context_gbr_op
;
3200 cgbr
->shifted
= NULL
;
3202 cgbr
->tab
= isl_tab_from_basic_set(dom
);
3203 cgbr
->tab
= isl_tab_init_samples(cgbr
->tab
);
3206 if (isl_tab_track_bset(cgbr
->tab
,
3207 isl_basic_set_cow(isl_basic_set_copy(dom
))) < 0)
3209 check_gbr_integer_feasible(cgbr
);
3211 return &cgbr
->context
;
3213 cgbr
->context
.op
->free(&cgbr
->context
);
3217 static struct isl_context
*isl_context_alloc(struct isl_basic_set
*dom
)
3222 if (dom
->ctx
->opt
->context
== ISL_CONTEXT_LEXMIN
)
3223 return isl_context_lex_alloc(dom
);
3225 return isl_context_gbr_alloc(dom
);
3228 /* Construct an isl_sol_map structure for accumulating the solution.
3229 * If track_empty is set, then we also keep track of the parts
3230 * of the context where there is no solution.
3231 * If max is set, then we are solving a maximization, rather than
3232 * a minimization problem, which means that the variables in the
3233 * tableau have value "M - x" rather than "M + x".
3235 static struct isl_sol_map
*sol_map_init(struct isl_basic_map
*bmap
,
3236 struct isl_basic_set
*dom
, int track_empty
, int max
)
3238 struct isl_sol_map
*sol_map
= NULL
;
3243 sol_map
= isl_calloc_type(bmap
->ctx
, struct isl_sol_map
);
3247 sol_map
->sol
.rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
3248 sol_map
->sol
.dec_level
.callback
.run
= &sol_dec_level_wrap
;
3249 sol_map
->sol
.dec_level
.sol
= &sol_map
->sol
;
3250 sol_map
->sol
.max
= max
;
3251 sol_map
->sol
.n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
3252 sol_map
->sol
.add
= &sol_map_add_wrap
;
3253 sol_map
->sol
.add_empty
= track_empty
? &sol_map_add_empty_wrap
: NULL
;
3254 sol_map
->sol
.free
= &sol_map_free_wrap
;
3255 sol_map
->map
= isl_map_alloc_dim(isl_basic_map_get_dim(bmap
), 1,
3260 sol_map
->sol
.context
= isl_context_alloc(dom
);
3261 if (!sol_map
->sol
.context
)
3265 sol_map
->empty
= isl_set_alloc_dim(isl_basic_set_get_dim(dom
),
3266 1, ISL_SET_DISJOINT
);
3267 if (!sol_map
->empty
)
3271 isl_basic_set_free(dom
);
3274 isl_basic_set_free(dom
);
3275 sol_map_free(sol_map
);
3279 /* Check whether all coefficients of (non-parameter) variables
3280 * are non-positive, meaning that no pivots can be performed on the row.
3282 static int is_critical(struct isl_tab
*tab
, int row
)
3285 unsigned off
= 2 + tab
->M
;
3287 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
3288 if (tab
->col_var
[j
] >= 0 &&
3289 (tab
->col_var
[j
] < tab
->n_param
||
3290 tab
->col_var
[j
] >= tab
->n_var
- tab
->n_div
))
3293 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ j
]))
3300 /* Check whether the inequality represented by vec is strict over the integers,
3301 * i.e., there are no integer values satisfying the constraint with
3302 * equality. This happens if the gcd of the coefficients is not a divisor
3303 * of the constant term. If so, scale the constraint down by the gcd
3304 * of the coefficients.
3306 static int is_strict(struct isl_vec
*vec
)
3312 isl_seq_gcd(vec
->el
+ 1, vec
->size
- 1, &gcd
);
3313 if (!isl_int_is_one(gcd
)) {
3314 strict
= !isl_int_is_divisible_by(vec
->el
[0], gcd
);
3315 isl_int_fdiv_q(vec
->el
[0], vec
->el
[0], gcd
);
3316 isl_seq_scale_down(vec
->el
+ 1, vec
->el
+ 1, gcd
, vec
->size
-1);
3323 /* Determine the sign of the given row of the main tableau.
3324 * The result is one of
3325 * isl_tab_row_pos: always non-negative; no pivot needed
3326 * isl_tab_row_neg: always non-positive; pivot
3327 * isl_tab_row_any: can be both positive and negative; split
3329 * We first handle some simple cases
3330 * - the row sign may be known already
3331 * - the row may be obviously non-negative
3332 * - the parametric constant may be equal to that of another row
3333 * for which we know the sign. This sign will be either "pos" or
3334 * "any". If it had been "neg" then we would have pivoted before.
3336 * If none of these cases hold, we check the value of the row for each
3337 * of the currently active samples. Based on the signs of these values
3338 * we make an initial determination of the sign of the row.
3340 * all zero -> unk(nown)
3341 * all non-negative -> pos
3342 * all non-positive -> neg
3343 * both negative and positive -> all
3345 * If we end up with "all", we are done.
3346 * Otherwise, we perform a check for positive and/or negative
3347 * values as follows.
3349 * samples neg unk pos
3355 * There is no special sign for "zero", because we can usually treat zero
3356 * as either non-negative or non-positive, whatever works out best.
3357 * However, if the row is "critical", meaning that pivoting is impossible
3358 * then we don't want to limp zero with the non-positive case, because
3359 * then we we would lose the solution for those values of the parameters
3360 * where the value of the row is zero. Instead, we treat 0 as non-negative
3361 * ensuring a split if the row can attain both zero and negative values.
3362 * The same happens when the original constraint was one that could not
3363 * be satisfied with equality by any integer values of the parameters.
3364 * In this case, we normalize the constraint, but then a value of zero
3365 * for the normalized constraint is actually a positive value for the
3366 * original constraint, so again we need to treat zero as non-negative.
3367 * In both these cases, we have the following decision tree instead:
3369 * all non-negative -> pos
3370 * all negative -> neg
3371 * both negative and non-negative -> all
3379 static enum isl_tab_row_sign
row_sign(struct isl_tab
*tab
,
3380 struct isl_sol
*sol
, int row
)
3382 struct isl_vec
*ineq
= NULL
;
3383 enum isl_tab_row_sign res
= isl_tab_row_unknown
;
3388 if (tab
->row_sign
[row
] != isl_tab_row_unknown
)
3389 return tab
->row_sign
[row
];
3390 if (is_obviously_nonneg(tab
, row
))
3391 return isl_tab_row_pos
;
3392 for (row2
= tab
->n_redundant
; row2
< tab
->n_row
; ++row2
) {
3393 if (tab
->row_sign
[row2
] == isl_tab_row_unknown
)
3395 if (identical_parameter_line(tab
, row
, row2
))
3396 return tab
->row_sign
[row2
];
3399 critical
= is_critical(tab
, row
);
3401 ineq
= get_row_parameter_ineq(tab
, row
);
3405 strict
= is_strict(ineq
);
3407 res
= sol
->context
->op
->ineq_sign(sol
->context
, ineq
->el
,
3408 critical
|| strict
);
3410 if (res
== isl_tab_row_unknown
|| res
== isl_tab_row_pos
) {
3411 /* test for negative values */
3413 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3414 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3416 feasible
= sol
->context
->op
->test_ineq(sol
->context
, ineq
->el
);
3420 res
= isl_tab_row_pos
;
3422 res
= (res
== isl_tab_row_unknown
) ? isl_tab_row_neg
3424 if (res
== isl_tab_row_neg
) {
3425 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3426 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3430 if (res
== isl_tab_row_neg
) {
3431 /* test for positive values */
3433 if (!critical
&& !strict
)
3434 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3436 feasible
= sol
->context
->op
->test_ineq(sol
->context
, ineq
->el
);
3440 res
= isl_tab_row_any
;
3447 return isl_tab_row_unknown
;
3450 static void find_solutions(struct isl_sol
*sol
, struct isl_tab
*tab
);
3452 /* Find solutions for values of the parameters that satisfy the given
3455 * We currently take a snapshot of the context tableau that is reset
3456 * when we return from this function, while we make a copy of the main
3457 * tableau, leaving the original main tableau untouched.
3458 * These are fairly arbitrary choices. Making a copy also of the context
3459 * tableau would obviate the need to undo any changes made to it later,
3460 * while taking a snapshot of the main tableau could reduce memory usage.
3461 * If we were to switch to taking a snapshot of the main tableau,
3462 * we would have to keep in mind that we need to save the row signs
3463 * and that we need to do this before saving the current basis
3464 * such that the basis has been restore before we restore the row signs.
3466 static void find_in_pos(struct isl_sol
*sol
, struct isl_tab
*tab
, isl_int
*ineq
)
3472 saved
= sol
->context
->op
->save(sol
->context
);
3474 tab
= isl_tab_dup(tab
);
3478 sol
->context
->op
->add_ineq(sol
->context
, ineq
, 0, 1);
3480 find_solutions(sol
, tab
);
3483 sol
->context
->op
->restore(sol
->context
, saved
);
3489 /* Record the absence of solutions for those values of the parameters
3490 * that do not satisfy the given inequality with equality.
3492 static void no_sol_in_strict(struct isl_sol
*sol
,
3493 struct isl_tab
*tab
, struct isl_vec
*ineq
)
3498 if (!sol
->context
|| sol
->error
)
3500 saved
= sol
->context
->op
->save(sol
->context
);
3502 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3504 sol
->context
->op
->add_ineq(sol
->context
, ineq
->el
, 1, 0);
3513 isl_int_add_ui(ineq
->el
[0], ineq
->el
[0], 1);
3515 sol
->context
->op
->restore(sol
->context
, saved
);
3521 /* Compute the lexicographic minimum of the set represented by the main
3522 * tableau "tab" within the context "sol->context_tab".
3523 * On entry the sample value of the main tableau is lexicographically
3524 * less than or equal to this lexicographic minimum.
3525 * Pivots are performed until a feasible point is found, which is then
3526 * necessarily equal to the minimum, or until the tableau is found to
3527 * be infeasible. Some pivots may need to be performed for only some
3528 * feasible values of the context tableau. If so, the context tableau
3529 * is split into a part where the pivot is needed and a part where it is not.
3531 * Whenever we enter the main loop, the main tableau is such that no
3532 * "obvious" pivots need to be performed on it, where "obvious" means
3533 * that the given row can be seen to be negative without looking at
3534 * the context tableau. In particular, for non-parametric problems,
3535 * no pivots need to be performed on the main tableau.
3536 * The caller of find_solutions is responsible for making this property
3537 * hold prior to the first iteration of the loop, while restore_lexmin
3538 * is called before every other iteration.
3540 * Inside the main loop, we first examine the signs of the rows of
3541 * the main tableau within the context of the context tableau.
3542 * If we find a row that is always non-positive for all values of
3543 * the parameters satisfying the context tableau and negative for at
3544 * least one value of the parameters, we perform the appropriate pivot
3545 * and start over. An exception is the case where no pivot can be
3546 * performed on the row. In this case, we require that the sign of
3547 * the row is negative for all values of the parameters (rather than just
3548 * non-positive). This special case is handled inside row_sign, which
3549 * will say that the row can have any sign if it determines that it can
3550 * attain both negative and zero values.
3552 * If we can't find a row that always requires a pivot, but we can find
3553 * one or more rows that require a pivot for some values of the parameters
3554 * (i.e., the row can attain both positive and negative signs), then we split
3555 * the context tableau into two parts, one where we force the sign to be
3556 * non-negative and one where we force is to be negative.
3557 * The non-negative part is handled by a recursive call (through find_in_pos).
3558 * Upon returning from this call, we continue with the negative part and
3559 * perform the required pivot.
3561 * If no such rows can be found, all rows are non-negative and we have
3562 * found a (rational) feasible point. If we only wanted a rational point
3564 * Otherwise, we check if all values of the sample point of the tableau
3565 * are integral for the variables. If so, we have found the minimal
3566 * integral point and we are done.
3567 * If the sample point is not integral, then we need to make a distinction
3568 * based on whether the constant term is non-integral or the coefficients
3569 * of the parameters. Furthermore, in order to decide how to handle
3570 * the non-integrality, we also need to know whether the coefficients
3571 * of the other columns in the tableau are integral. This leads
3572 * to the following table. The first two rows do not correspond
3573 * to a non-integral sample point and are only mentioned for completeness.
3575 * constant parameters other
3578 * int int rat | -> no problem
3580 * rat int int -> fail
3582 * rat int rat -> cut
3585 * rat rat rat | -> parametric cut
3588 * rat rat int | -> split context
3590 * If the parametric constant is completely integral, then there is nothing
3591 * to be done. If the constant term is non-integral, but all the other
3592 * coefficient are integral, then there is nothing that can be done
3593 * and the tableau has no integral solution.
3594 * If, on the other hand, one or more of the other columns have rational
3595 * coefficients, but the parameter coefficients are all integral, then
3596 * we can perform a regular (non-parametric) cut.
3597 * Finally, if there is any parameter coefficient that is non-integral,
3598 * then we need to involve the context tableau. There are two cases here.
3599 * If at least one other column has a rational coefficient, then we
3600 * can perform a parametric cut in the main tableau by adding a new
3601 * integer division in the context tableau.
3602 * If all other columns have integral coefficients, then we need to
3603 * enforce that the rational combination of parameters (c + \sum a_i y_i)/m
3604 * is always integral. We do this by introducing an integer division
3605 * q = floor((c + \sum a_i y_i)/m) and stipulating that its argument should
3606 * always be integral in the context tableau, i.e., m q = c + \sum a_i y_i.
3607 * Since q is expressed in the tableau as
3608 * c + \sum a_i y_i - m q >= 0
3609 * -c - \sum a_i y_i + m q + m - 1 >= 0
3610 * it is sufficient to add the inequality
3611 * -c - \sum a_i y_i + m q >= 0
3612 * In the part of the context where this inequality does not hold, the
3613 * main tableau is marked as being empty.
3615 static void find_solutions(struct isl_sol
*sol
, struct isl_tab
*tab
)
3617 struct isl_context
*context
;
3619 if (!tab
|| sol
->error
)
3622 context
= sol
->context
;
3626 if (context
->op
->is_empty(context
))
3629 for (; tab
&& !tab
->empty
; tab
= restore_lexmin(tab
)) {
3632 enum isl_tab_row_sign sgn
;
3636 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
3637 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
3639 sgn
= row_sign(tab
, sol
, row
);
3642 tab
->row_sign
[row
] = sgn
;
3643 if (sgn
== isl_tab_row_any
)
3645 if (sgn
== isl_tab_row_any
&& split
== -1)
3647 if (sgn
== isl_tab_row_neg
)
3650 if (row
< tab
->n_row
)
3653 struct isl_vec
*ineq
;
3655 split
= context
->op
->best_split(context
, tab
);
3658 ineq
= get_row_parameter_ineq(tab
, split
);
3662 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
3663 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
3665 if (tab
->row_sign
[row
] == isl_tab_row_any
)
3666 tab
->row_sign
[row
] = isl_tab_row_unknown
;
3668 tab
->row_sign
[split
] = isl_tab_row_pos
;
3670 find_in_pos(sol
, tab
, ineq
->el
);
3671 tab
->row_sign
[split
] = isl_tab_row_neg
;
3673 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3674 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3676 context
->op
->add_ineq(context
, ineq
->el
, 0, 1);
3684 row
= first_non_integer_row(tab
, &flags
);
3687 if (ISL_FL_ISSET(flags
, I_PAR
)) {
3688 if (ISL_FL_ISSET(flags
, I_VAR
)) {
3689 if (isl_tab_mark_empty(tab
) < 0)
3693 row
= add_cut(tab
, row
);
3694 } else if (ISL_FL_ISSET(flags
, I_VAR
)) {
3695 struct isl_vec
*div
;
3696 struct isl_vec
*ineq
;
3698 div
= get_row_split_div(tab
, row
);
3701 d
= context
->op
->get_div(context
, tab
, div
);
3705 ineq
= ineq_for_div(context
->op
->peek_basic_set(context
), d
);
3709 no_sol_in_strict(sol
, tab
, ineq
);
3710 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3711 context
->op
->add_ineq(context
, ineq
->el
, 1, 1);
3713 if (sol
->error
|| !context
->op
->is_ok(context
))
3715 tab
= set_row_cst_to_div(tab
, row
, d
);
3716 if (context
->op
->is_empty(context
))
3719 row
= add_parametric_cut(tab
, row
, context
);
3732 /* Compute the lexicographic minimum of the set represented by the main
3733 * tableau "tab" within the context "sol->context_tab".
3735 * As a preprocessing step, we first transfer all the purely parametric
3736 * equalities from the main tableau to the context tableau, i.e.,
3737 * parameters that have been pivoted to a row.
3738 * These equalities are ignored by the main algorithm, because the
3739 * corresponding rows may not be marked as being non-negative.
3740 * In parts of the context where the added equality does not hold,
3741 * the main tableau is marked as being empty.
3743 static void find_solutions_main(struct isl_sol
*sol
, struct isl_tab
*tab
)
3752 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
3756 if (tab
->row_var
[row
] < 0)
3758 if (tab
->row_var
[row
] >= tab
->n_param
&&
3759 tab
->row_var
[row
] < tab
->n_var
- tab
->n_div
)
3761 if (tab
->row_var
[row
] < tab
->n_param
)
3762 p
= tab
->row_var
[row
];
3764 p
= tab
->row_var
[row
]
3765 + tab
->n_param
- (tab
->n_var
- tab
->n_div
);
3767 eq
= isl_vec_alloc(tab
->mat
->ctx
, 1+tab
->n_param
+tab
->n_div
);
3770 get_row_parameter_line(tab
, row
, eq
->el
);
3771 isl_int_neg(eq
->el
[1 + p
], tab
->mat
->row
[row
][0]);
3772 eq
= isl_vec_normalize(eq
);
3775 no_sol_in_strict(sol
, tab
, eq
);
3777 isl_seq_neg(eq
->el
, eq
->el
, eq
->size
);
3779 no_sol_in_strict(sol
, tab
, eq
);
3780 isl_seq_neg(eq
->el
, eq
->el
, eq
->size
);
3782 sol
->context
->op
->add_eq(sol
->context
, eq
->el
, 1, 1);
3786 if (isl_tab_mark_redundant(tab
, row
) < 0)
3789 if (sol
->context
->op
->is_empty(sol
->context
))
3792 row
= tab
->n_redundant
- 1;
3795 find_solutions(sol
, tab
);
3806 static void sol_map_find_solutions(struct isl_sol_map
*sol_map
,
3807 struct isl_tab
*tab
)
3809 find_solutions_main(&sol_map
->sol
, tab
);
3812 /* Check if integer division "div" of "dom" also occurs in "bmap".
3813 * If so, return its position within the divs.
3814 * If not, return -1.
3816 static int find_context_div(struct isl_basic_map
*bmap
,
3817 struct isl_basic_set
*dom
, unsigned div
)
3820 unsigned b_dim
= isl_dim_total(bmap
->dim
);
3821 unsigned d_dim
= isl_dim_total(dom
->dim
);
3823 if (isl_int_is_zero(dom
->div
[div
][0]))
3825 if (isl_seq_first_non_zero(dom
->div
[div
] + 2 + d_dim
, dom
->n_div
) != -1)
3828 for (i
= 0; i
< bmap
->n_div
; ++i
) {
3829 if (isl_int_is_zero(bmap
->div
[i
][0]))
3831 if (isl_seq_first_non_zero(bmap
->div
[i
] + 2 + d_dim
,
3832 (b_dim
- d_dim
) + bmap
->n_div
) != -1)
3834 if (isl_seq_eq(bmap
->div
[i
], dom
->div
[div
], 2 + d_dim
))
3840 /* The correspondence between the variables in the main tableau,
3841 * the context tableau, and the input map and domain is as follows.
3842 * The first n_param and the last n_div variables of the main tableau
3843 * form the variables of the context tableau.
3844 * In the basic map, these n_param variables correspond to the
3845 * parameters and the input dimensions. In the domain, they correspond
3846 * to the parameters and the set dimensions.
3847 * The n_div variables correspond to the integer divisions in the domain.
3848 * To ensure that everything lines up, we may need to copy some of the
3849 * integer divisions of the domain to the map. These have to be placed
3850 * in the same order as those in the context and they have to be placed
3851 * after any other integer divisions that the map may have.
3852 * This function performs the required reordering.
3854 static struct isl_basic_map
*align_context_divs(struct isl_basic_map
*bmap
,
3855 struct isl_basic_set
*dom
)
3861 for (i
= 0; i
< dom
->n_div
; ++i
)
3862 if (find_context_div(bmap
, dom
, i
) != -1)
3864 other
= bmap
->n_div
- common
;
3865 if (dom
->n_div
- common
> 0) {
3866 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
3867 dom
->n_div
- common
, 0, 0);
3871 for (i
= 0; i
< dom
->n_div
; ++i
) {
3872 int pos
= find_context_div(bmap
, dom
, i
);
3874 pos
= isl_basic_map_alloc_div(bmap
);
3877 isl_int_set_si(bmap
->div
[pos
][0], 0);
3879 if (pos
!= other
+ i
)
3880 isl_basic_map_swap_div(bmap
, pos
, other
+ i
);
3884 isl_basic_map_free(bmap
);
3888 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
3889 * some obvious symmetries.
3891 * We make sure the divs in the domain are properly ordered,
3892 * because they will be added one by one in the given order
3893 * during the construction of the solution map.
3895 static __isl_give isl_map
*basic_map_partial_lexopt_base(
3896 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
3897 __isl_give isl_set
**empty
, int max
)
3899 isl_map
*result
= NULL
;
3900 struct isl_tab
*tab
;
3901 struct isl_sol_map
*sol_map
= NULL
;
3902 struct isl_context
*context
;
3905 dom
= isl_basic_set_order_divs(dom
);
3906 bmap
= align_context_divs(bmap
, dom
);
3908 sol_map
= sol_map_init(bmap
, dom
, !!empty
, max
);
3912 context
= sol_map
->sol
.context
;
3913 if (isl_basic_set_fast_is_empty(context
->op
->peek_basic_set(context
)))
3915 else if (isl_basic_map_fast_is_empty(bmap
))
3916 sol_map_add_empty_if_needed(sol_map
,
3917 isl_basic_set_copy(context
->op
->peek_basic_set(context
)));
3919 tab
= tab_for_lexmin(bmap
,
3920 context
->op
->peek_basic_set(context
), 1, max
);
3921 tab
= context
->op
->detect_nonnegative_parameters(context
, tab
);
3922 sol_map_find_solutions(sol_map
, tab
);
3924 if (sol_map
->sol
.error
)
3927 result
= isl_map_copy(sol_map
->map
);
3929 *empty
= isl_set_copy(sol_map
->empty
);
3930 sol_free(&sol_map
->sol
);
3931 isl_basic_map_free(bmap
);
3934 sol_free(&sol_map
->sol
);
3935 isl_basic_map_free(bmap
);
3939 /* Structure used during detection of parallel constraints.
3940 * n_in: number of "input" variables: isl_dim_param + isl_dim_in
3941 * n_out: number of "output" variables: isl_dim_out + isl_dim_div
3942 * val: the coefficients of the output variables
3944 struct isl_constraint_equal_info
{
3945 isl_basic_map
*bmap
;
3951 /* Check whether the coefficients of the output variables
3952 * of the constraint in "entry" are equal to info->val.
3954 static int constraint_equal(const void *entry
, const void *val
)
3956 isl_int
**row
= (isl_int
**)entry
;
3957 const struct isl_constraint_equal_info
*info
= val
;
3959 return isl_seq_eq((*row
) + 1 + info
->n_in
, info
->val
, info
->n_out
);
3962 /* Check whether "bmap" has a pair of constraints that have
3963 * the same coefficients for the output variables.
3964 * Note that the coefficients of the existentially quantified
3965 * variables need to be zero since the existentially quantified
3966 * of the result are usually not the same as those of the input.
3967 * the isl_dim_out and isl_dim_div dimensions.
3968 * If so, return 1 and return the row indices of the two constraints
3969 * in *first and *second.
3971 static int parallel_constraints(__isl_keep isl_basic_map
*bmap
,
3972 int *first
, int *second
)
3975 isl_ctx
*ctx
= isl_basic_map_get_ctx(bmap
);
3976 struct isl_hash_table
*table
= NULL
;
3977 struct isl_hash_table_entry
*entry
;
3978 struct isl_constraint_equal_info info
;
3982 ctx
= isl_basic_map_get_ctx(bmap
);
3983 table
= isl_hash_table_alloc(ctx
, bmap
->n_ineq
);
3987 info
.n_in
= isl_basic_map_dim(bmap
, isl_dim_param
) +
3988 isl_basic_map_dim(bmap
, isl_dim_in
);
3990 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
3991 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
3992 info
.n_out
= n_out
+ n_div
;
3993 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
3996 info
.val
= bmap
->ineq
[i
] + 1 + info
.n_in
;
3997 if (isl_seq_first_non_zero(info
.val
, n_out
) < 0)
3999 if (isl_seq_first_non_zero(info
.val
+ n_out
, n_div
) >= 0)
4001 hash
= isl_seq_get_hash(info
.val
, info
.n_out
);
4002 entry
= isl_hash_table_find(ctx
, table
, hash
,
4003 constraint_equal
, &info
, 1);
4008 entry
->data
= &bmap
->ineq
[i
];
4011 if (i
< bmap
->n_ineq
) {
4012 *first
= ((isl_int
**)entry
->data
) - bmap
->ineq
;
4016 isl_hash_table_free(ctx
, table
);
4018 return i
< bmap
->n_ineq
;
4020 isl_hash_table_free(ctx
, table
);
4024 /* Given a set of upper bounds on the last "input" variable m,
4025 * construct a set that assigns the minimal upper bound to m, i.e.,
4026 * construct a set that divides the space into cells where one
4027 * of the upper bounds is smaller than all the others and assign
4028 * this upper bound to m.
4030 * In particular, if there are n bounds b_i, then the result
4031 * consists of n basic sets, each one of the form
4034 * b_i <= b_j for j > i
4035 * b_i < b_j for j < i
4037 static __isl_give isl_set
*set_minimum(__isl_take isl_dim
*dim
,
4038 __isl_take isl_mat
*var
)
4041 isl_basic_set
*bset
= NULL
;
4043 isl_set
*set
= NULL
;
4048 ctx
= isl_dim_get_ctx(dim
);
4049 set
= isl_set_alloc_dim(isl_dim_copy(dim
),
4050 var
->n_row
, ISL_SET_DISJOINT
);
4052 for (i
= 0; i
< var
->n_row
; ++i
) {
4053 bset
= isl_basic_set_alloc_dim(isl_dim_copy(dim
), 0,
4055 k
= isl_basic_set_alloc_equality(bset
);
4058 isl_seq_cpy(bset
->eq
[k
], var
->row
[i
], var
->n_col
);
4059 isl_int_set_si(bset
->eq
[k
][var
->n_col
], -1);
4060 for (j
= 0; j
< var
->n_row
; ++j
) {
4063 k
= isl_basic_set_alloc_inequality(bset
);
4066 isl_seq_combine(bset
->ineq
[k
], ctx
->one
, var
->row
[j
],
4067 ctx
->negone
, var
->row
[i
],
4069 isl_int_set_si(bset
->ineq
[k
][var
->n_col
], 0);
4071 isl_int_sub_ui(bset
->ineq
[k
][0],
4072 bset
->ineq
[k
][0], 1);
4074 bset
= isl_basic_set_finalize(bset
);
4075 set
= isl_set_add_basic_set(set
, bset
);
4082 isl_basic_set_free(bset
);
4089 /* Given that the last input variable of "bmap" represents the minimum
4090 * of the bounds in "cst", check whether we need to split the domain
4091 * based on which bound attains the minimum.
4093 * A split is needed when the minimum appears in an integer division
4094 * or in an equality. Otherwise, it is only needed if it appears in
4095 * an upper bound that is different from the upper bounds on which it
4098 static int need_split_map(__isl_keep isl_basic_map
*bmap
,
4099 __isl_keep isl_mat
*cst
)
4105 pos
= cst
->n_col
- 1;
4106 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4108 for (i
= 0; i
< bmap
->n_div
; ++i
)
4109 if (!isl_int_is_zero(bmap
->div
[i
][2 + pos
]))
4112 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4113 if (!isl_int_is_zero(bmap
->eq
[i
][1 + pos
]))
4116 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4117 if (isl_int_is_nonneg(bmap
->ineq
[i
][1 + pos
]))
4119 if (!isl_int_is_negone(bmap
->ineq
[i
][1 + pos
]))
4121 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + pos
+ 1,
4122 total
- pos
- 1) >= 0)
4125 for (j
= 0; j
< cst
->n_row
; ++j
)
4126 if (isl_seq_eq(bmap
->ineq
[i
], cst
->row
[j
], cst
->n_col
))
4128 if (j
>= cst
->n_row
)
4135 static int need_split_set(__isl_keep isl_basic_set
*bset
,
4136 __isl_keep isl_mat
*cst
)
4138 return need_split_map((isl_basic_map
*)bset
, cst
);
4141 /* Given a set of which the last set variable is the minimum
4142 * of the bounds in "cst", split each basic set in the set
4143 * in pieces where one of the bounds is (strictly) smaller than the others.
4144 * This subdivision is given in "min_expr".
4145 * The variable is subsequently projected out.
4147 * We only do the split when it is needed.
4148 * For example if the last input variable m = min(a,b) and the only
4149 * constraints in the given basic set are lower bounds on m,
4150 * i.e., l <= m = min(a,b), then we can simply project out m
4151 * to obtain l <= a and l <= b, without having to split on whether
4152 * m is equal to a or b.
4154 static __isl_give isl_set
*split(__isl_take isl_set
*empty
,
4155 __isl_take isl_set
*min_expr
, __isl_take isl_mat
*cst
)
4162 if (!empty
|| !min_expr
|| !cst
)
4165 n_in
= isl_set_dim(empty
, isl_dim_set
);
4166 dim
= isl_set_get_dim(empty
);
4167 dim
= isl_dim_drop(dim
, isl_dim_set
, n_in
- 1, 1);
4168 res
= isl_set_empty(dim
);
4170 for (i
= 0; i
< empty
->n
; ++i
) {
4173 set
= isl_set_from_basic_set(isl_basic_set_copy(empty
->p
[i
]));
4174 if (need_split_set(empty
->p
[i
], cst
))
4175 set
= isl_set_intersect(set
, isl_set_copy(min_expr
));
4176 set
= isl_set_remove_dims(set
, isl_dim_set
, n_in
- 1, 1);
4178 res
= isl_set_union_disjoint(res
, set
);
4181 isl_set_free(empty
);
4182 isl_set_free(min_expr
);
4186 isl_set_free(empty
);
4187 isl_set_free(min_expr
);
4192 /* Given a map of which the last input variable is the minimum
4193 * of the bounds in "cst", split each basic set in the set
4194 * in pieces where one of the bounds is (strictly) smaller than the others.
4195 * This subdivision is given in "min_expr".
4196 * The variable is subsequently projected out.
4198 * The implementation is essentially the same as that of "split".
4200 static __isl_give isl_map
*split_domain(__isl_take isl_map
*opt
,
4201 __isl_take isl_set
*min_expr
, __isl_take isl_mat
*cst
)
4208 if (!opt
|| !min_expr
|| !cst
)
4211 n_in
= isl_map_dim(opt
, isl_dim_in
);
4212 dim
= isl_map_get_dim(opt
);
4213 dim
= isl_dim_drop(dim
, isl_dim_in
, n_in
- 1, 1);
4214 res
= isl_map_empty(dim
);
4216 for (i
= 0; i
< opt
->n
; ++i
) {
4219 map
= isl_map_from_basic_map(isl_basic_map_copy(opt
->p
[i
]));
4220 if (need_split_map(opt
->p
[i
], cst
))
4221 map
= isl_map_intersect_domain(map
,
4222 isl_set_copy(min_expr
));
4223 map
= isl_map_remove_dims(map
, isl_dim_in
, n_in
- 1, 1);
4225 res
= isl_map_union_disjoint(res
, map
);
4229 isl_set_free(min_expr
);
4234 isl_set_free(min_expr
);
4239 static __isl_give isl_map
*basic_map_partial_lexopt(
4240 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4241 __isl_give isl_set
**empty
, int max
);
4243 /* Given a basic map with at least two parallel constraints (as found
4244 * by the function parallel_constraints), first look for more constraints
4245 * parallel to the two constraint and replace the found list of parallel
4246 * constraints by a single constraint with as "input" part the minimum
4247 * of the input parts of the list of constraints. Then, recursively call
4248 * basic_map_partial_lexopt (possibly finding more parallel constraints)
4249 * and plug in the definition of the minimum in the result.
4251 * More specifically, given a set of constraints
4255 * Replace this set by a single constraint
4259 * with u a new parameter with constraints
4263 * Any solution to the new system is also a solution for the original system
4266 * a x >= -u >= -b_i(p)
4268 * Moreover, m = min_i(b_i(p)) satisfies the constraints on u and can
4269 * therefore be plugged into the solution.
4271 static __isl_give isl_map
*basic_map_partial_lexopt_symm(
4272 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4273 __isl_give isl_set
**empty
, int max
, int first
, int second
)
4277 unsigned n_in
, n_out
, n_div
;
4279 isl_vec
*var
= NULL
;
4280 isl_mat
*cst
= NULL
;
4283 isl_dim
*map_dim
, *set_dim
;
4285 map_dim
= isl_basic_map_get_dim(bmap
);
4286 set_dim
= empty
? isl_basic_set_get_dim(dom
) : NULL
;
4288 n_in
= isl_basic_map_dim(bmap
, isl_dim_param
) +
4289 isl_basic_map_dim(bmap
, isl_dim_in
);
4290 n_out
= isl_basic_map_dim(bmap
, isl_dim_all
) - n_in
;
4292 ctx
= isl_basic_map_get_ctx(bmap
);
4293 list
= isl_alloc_array(ctx
, int, bmap
->n_ineq
);
4294 var
= isl_vec_alloc(ctx
, n_out
);
4300 isl_seq_cpy(var
->el
, bmap
->ineq
[first
] + 1 + n_in
, n_out
);
4301 for (i
= second
+ 1, n
= 2; i
< bmap
->n_ineq
; ++i
) {
4302 if (isl_seq_eq(var
->el
, bmap
->ineq
[i
] + 1 + n_in
, n_out
))
4306 cst
= isl_mat_alloc(ctx
, n
, 1 + n_in
);
4310 for (i
= 0; i
< n
; ++i
)
4311 isl_seq_cpy(cst
->row
[i
], bmap
->ineq
[list
[i
]], 1 + n_in
);
4313 bmap
= isl_basic_map_cow(bmap
);
4316 for (i
= n
- 1; i
>= 0; --i
)
4317 if (isl_basic_map_drop_inequality(bmap
, list
[i
]) < 0)
4320 bmap
= isl_basic_map_add(bmap
, isl_dim_in
, 1);
4321 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
4322 k
= isl_basic_map_alloc_inequality(bmap
);
4325 isl_seq_clr(bmap
->ineq
[k
], 1 + n_in
);
4326 isl_int_set_si(bmap
->ineq
[k
][1 + n_in
], 1);
4327 isl_seq_cpy(bmap
->ineq
[k
] + 1 + n_in
+ 1, var
->el
, n_out
);
4328 bmap
= isl_basic_map_finalize(bmap
);
4330 n_div
= isl_basic_set_dim(dom
, isl_dim_div
);
4331 dom
= isl_basic_set_add(dom
, isl_dim_set
, 1);
4332 dom
= isl_basic_set_extend_constraints(dom
, 0, n
);
4333 for (i
= 0; i
< n
; ++i
) {
4334 k
= isl_basic_set_alloc_inequality(dom
);
4337 isl_seq_cpy(dom
->ineq
[k
], cst
->row
[i
], 1 + n_in
);
4338 isl_int_set_si(dom
->ineq
[k
][1 + n_in
], -1);
4339 isl_seq_clr(dom
->ineq
[k
] + 1 + n_in
+ 1, n_div
);
4342 min_expr
= set_minimum(isl_basic_set_get_dim(dom
), isl_mat_copy(cst
));
4347 opt
= basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
4350 *empty
= split(*empty
,
4351 isl_set_copy(min_expr
), isl_mat_copy(cst
));
4352 *empty
= isl_set_reset_dim(*empty
, set_dim
);
4355 opt
= split_domain(opt
, min_expr
, cst
);
4356 opt
= isl_map_reset_dim(opt
, map_dim
);
4360 isl_dim_free(map_dim
);
4361 isl_dim_free(set_dim
);
4365 isl_basic_set_free(dom
);
4366 isl_basic_map_free(bmap
);
4370 /* Recursive part of isl_tab_basic_map_partial_lexopt, after detecting
4371 * equalities and removing redundant constraints.
4373 * We first check if there are any parallel constraints (left).
4374 * If not, we are in the base case.
4375 * If there are parallel constraints, we replace them by a single
4376 * constraint in basic_map_partial_lexopt_symm and then call
4377 * this function recursively to look for more parallel constraints.
4379 static __isl_give isl_map
*basic_map_partial_lexopt(
4380 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4381 __isl_give isl_set
**empty
, int max
)
4389 if (bmap
->ctx
->opt
->pip_symmetry
)
4390 par
= parallel_constraints(bmap
, &first
, &second
);
4394 return basic_map_partial_lexopt_base(bmap
, dom
, empty
, max
);
4396 return basic_map_partial_lexopt_symm(bmap
, dom
, empty
, max
,
4399 isl_basic_set_free(dom
);
4400 isl_basic_map_free(bmap
);
4404 /* Compute the lexicographic minimum (or maximum if "max" is set)
4405 * of "bmap" over the domain "dom" and return the result as a map.
4406 * If "empty" is not NULL, then *empty is assigned a set that
4407 * contains those parts of the domain where there is no solution.
4408 * If "bmap" is marked as rational (ISL_BASIC_MAP_RATIONAL),
4409 * then we compute the rational optimum. Otherwise, we compute
4410 * the integral optimum.
4412 * We perform some preprocessing. As the PILP solver does not
4413 * handle implicit equalities very well, we first make sure all
4414 * the equalities are explicitly available.
4416 * We also add context constraints to the basic map and remove
4417 * redundant constraints. This is only needed because of the
4418 * way we handle simple symmetries. In particular, we currently look
4419 * for symmetries on the constraints, before we set up the main tableau.
4420 * It is then no good to look for symmetries on possibly redundant constraints.
4422 struct isl_map
*isl_tab_basic_map_partial_lexopt(
4423 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4424 struct isl_set
**empty
, int max
)
4431 isl_assert(bmap
->ctx
,
4432 isl_basic_map_compatible_domain(bmap
, dom
), goto error
);
4434 bmap
= isl_basic_map_intersect_domain(bmap
, isl_basic_set_copy(dom
));
4435 bmap
= isl_basic_map_detect_equalities(bmap
);
4436 bmap
= isl_basic_map_remove_redundancies(bmap
);
4438 return basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
4440 isl_basic_set_free(dom
);
4441 isl_basic_map_free(bmap
);
4445 struct isl_sol_for
{
4447 int (*fn
)(__isl_take isl_basic_set
*dom
,
4448 __isl_take isl_mat
*map
, void *user
);
4452 static void sol_for_free(struct isl_sol_for
*sol_for
)
4454 if (sol_for
->sol
.context
)
4455 sol_for
->sol
.context
->op
->free(sol_for
->sol
.context
);
4459 static void sol_for_free_wrap(struct isl_sol
*sol
)
4461 sol_for_free((struct isl_sol_for
*)sol
);
4464 /* Add the solution identified by the tableau and the context tableau.
4466 * See documentation of sol_add for more details.
4468 * Instead of constructing a basic map, this function calls a user
4469 * defined function with the current context as a basic set and
4470 * an affine matrix representing the relation between the input and output.
4471 * The number of rows in this matrix is equal to one plus the number
4472 * of output variables. The number of columns is equal to one plus
4473 * the total dimension of the context, i.e., the number of parameters,
4474 * input variables and divs. Since some of the columns in the matrix
4475 * may refer to the divs, the basic set is not simplified.
4476 * (Simplification may reorder or remove divs.)
4478 static void sol_for_add(struct isl_sol_for
*sol
,
4479 struct isl_basic_set
*dom
, struct isl_mat
*M
)
4481 if (sol
->sol
.error
|| !dom
|| !M
)
4484 dom
= isl_basic_set_simplify(dom
);
4485 dom
= isl_basic_set_finalize(dom
);
4487 if (sol
->fn(isl_basic_set_copy(dom
), isl_mat_copy(M
), sol
->user
) < 0)
4490 isl_basic_set_free(dom
);
4494 isl_basic_set_free(dom
);
4499 static void sol_for_add_wrap(struct isl_sol
*sol
,
4500 struct isl_basic_set
*dom
, struct isl_mat
*M
)
4502 sol_for_add((struct isl_sol_for
*)sol
, dom
, M
);
4505 static struct isl_sol_for
*sol_for_init(struct isl_basic_map
*bmap
, int max
,
4506 int (*fn
)(__isl_take isl_basic_set
*dom
, __isl_take isl_mat
*map
,
4510 struct isl_sol_for
*sol_for
= NULL
;
4511 struct isl_dim
*dom_dim
;
4512 struct isl_basic_set
*dom
= NULL
;
4514 sol_for
= isl_calloc_type(bmap
->ctx
, struct isl_sol_for
);
4518 dom_dim
= isl_dim_domain(isl_dim_copy(bmap
->dim
));
4519 dom
= isl_basic_set_universe(dom_dim
);
4521 sol_for
->sol
.rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
4522 sol_for
->sol
.dec_level
.callback
.run
= &sol_dec_level_wrap
;
4523 sol_for
->sol
.dec_level
.sol
= &sol_for
->sol
;
4525 sol_for
->user
= user
;
4526 sol_for
->sol
.max
= max
;
4527 sol_for
->sol
.n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4528 sol_for
->sol
.add
= &sol_for_add_wrap
;
4529 sol_for
->sol
.add_empty
= NULL
;
4530 sol_for
->sol
.free
= &sol_for_free_wrap
;
4532 sol_for
->sol
.context
= isl_context_alloc(dom
);
4533 if (!sol_for
->sol
.context
)
4536 isl_basic_set_free(dom
);
4539 isl_basic_set_free(dom
);
4540 sol_for_free(sol_for
);
4544 static void sol_for_find_solutions(struct isl_sol_for
*sol_for
,
4545 struct isl_tab
*tab
)
4547 find_solutions_main(&sol_for
->sol
, tab
);
4550 int isl_basic_map_foreach_lexopt(__isl_keep isl_basic_map
*bmap
, int max
,
4551 int (*fn
)(__isl_take isl_basic_set
*dom
, __isl_take isl_mat
*map
,
4555 struct isl_sol_for
*sol_for
= NULL
;
4557 bmap
= isl_basic_map_copy(bmap
);
4561 bmap
= isl_basic_map_detect_equalities(bmap
);
4562 sol_for
= sol_for_init(bmap
, max
, fn
, user
);
4564 if (isl_basic_map_fast_is_empty(bmap
))
4567 struct isl_tab
*tab
;
4568 struct isl_context
*context
= sol_for
->sol
.context
;
4569 tab
= tab_for_lexmin(bmap
,
4570 context
->op
->peek_basic_set(context
), 1, max
);
4571 tab
= context
->op
->detect_nonnegative_parameters(context
, tab
);
4572 sol_for_find_solutions(sol_for
, tab
);
4573 if (sol_for
->sol
.error
)
4577 sol_free(&sol_for
->sol
);
4578 isl_basic_map_free(bmap
);
4581 sol_free(&sol_for
->sol
);
4582 isl_basic_map_free(bmap
);
4586 int isl_basic_map_foreach_lexmin(__isl_keep isl_basic_map
*bmap
,
4587 int (*fn
)(__isl_take isl_basic_set
*dom
, __isl_take isl_mat
*map
,
4591 return isl_basic_map_foreach_lexopt(bmap
, 0, fn
, user
);
4594 int isl_basic_map_foreach_lexmax(__isl_keep isl_basic_map
*bmap
,
4595 int (*fn
)(__isl_take isl_basic_set
*dom
, __isl_take isl_mat
*map
,
4599 return isl_basic_map_foreach_lexopt(bmap
, 1, fn
, user
);